mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-24 08:12:24 +02:00
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import * as execa from 'execa';
|
|
import commandToString from './commandToString';
|
|
import splitCommandString from './splitCommandString';
|
|
import { stdout } from 'process';
|
|
|
|
interface ExecCommandOptions {
|
|
showInput?: boolean;
|
|
showStdout?: boolean;
|
|
showStderr?: boolean;
|
|
quiet?: boolean;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
|
env?: Record<string, any>;
|
|
}
|
|
|
|
export default async (command: string | string[], options: ExecCommandOptions | null = null): Promise<string> => {
|
|
options = {
|
|
showInput: true,
|
|
showStdout: true,
|
|
showStderr: true,
|
|
quiet: false,
|
|
env: {},
|
|
...options,
|
|
};
|
|
|
|
if (options.quiet) {
|
|
options.showInput = false;
|
|
options.showStdout = false;
|
|
options.showStderr = false;
|
|
}
|
|
|
|
if (options.showInput) {
|
|
if (typeof command === 'string') {
|
|
stdout.write(`> ${command}\n`);
|
|
} else {
|
|
stdout.write(`> ${commandToString(command[0], command.slice(1))}\n`);
|
|
}
|
|
}
|
|
|
|
const args: string[] = typeof command === 'string' ? splitCommandString(command) : command as string[];
|
|
const executableName = args[0];
|
|
args.splice(0, 1);
|
|
const promise = execa(executableName, args, { env: options.env });
|
|
if (options.showStdout && promise.stdout) promise.stdout.pipe(process.stdout);
|
|
if (options.showStderr && promise.stderr) promise.stderr.pipe(process.stderr);
|
|
const result = await promise;
|
|
return result.stdout.trim();
|
|
};
|