2023-03-19 17:37:07 +02:00
|
|
|
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;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-10-30 13:32:14 +02:00
|
|
|
env?: Record<string, any>;
|
2023-03-19 17:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async (command: string | string[], options: ExecCommandOptions | null = null): Promise<string> => {
|
|
|
|
options = {
|
|
|
|
showInput: true,
|
|
|
|
showStdout: true,
|
|
|
|
showStderr: true,
|
|
|
|
quiet: false,
|
2023-10-30 13:32:14 +02:00
|
|
|
env: {},
|
2023-03-19 17:37:07 +02:00
|
|
|
...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);
|
2023-10-30 13:32:14 +02:00
|
|
|
const promise = execa(executableName, args, { env: options.env });
|
2023-03-19 17:37:07 +02:00
|
|
|
if (options.showStdout && promise.stdout) promise.stdout.pipe(process.stdout);
|
2023-04-06 12:11:45 +02:00
|
|
|
if (options.showStderr && promise.stderr) promise.stderr.pipe(process.stderr);
|
2023-03-19 17:37:07 +02:00
|
|
|
const result = await promise;
|
|
|
|
return result.stdout.trim();
|
|
|
|
};
|