2022-08-04 13:16:16 +02:00
|
|
|
import execa, { Options } from 'execa';
|
2022-08-02 23:08:42 +10:00
|
|
|
import chalk from 'chalk';
|
|
|
|
|
|
|
|
const logger = console;
|
|
|
|
|
2022-08-04 13:16:16 +02:00
|
|
|
type StepOptions = {
|
|
|
|
startMessage?: string;
|
|
|
|
errorMessage?: string;
|
|
|
|
dryRun?: boolean;
|
|
|
|
debug?: boolean;
|
|
|
|
};
|
|
|
|
|
2022-08-02 23:08:42 +10:00
|
|
|
export const exec = async (
|
|
|
|
command: string,
|
2022-08-04 13:16:16 +02:00
|
|
|
options: Options = {},
|
|
|
|
{ startMessage, errorMessage, dryRun, debug }: StepOptions = {}
|
|
|
|
): Promise<void> => {
|
|
|
|
logger.info();
|
2022-08-02 23:08:42 +10:00
|
|
|
if (startMessage) logger.info(startMessage);
|
|
|
|
|
|
|
|
if (dryRun) {
|
|
|
|
logger.info(`\n> ${command}\n`);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug(command);
|
2022-08-04 13:16:16 +02:00
|
|
|
const defaultOptions: Options = {
|
|
|
|
stdout: debug ? 'inherit' : 'ignore',
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
await execa.command(command, { ...defaultOptions, ...options });
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(chalk.red(`An error occurred while executing: \`${command}\``));
|
|
|
|
logger.log(errorMessage);
|
2022-08-05 20:48:52 +02:00
|
|
|
throw err;
|
2022-08-04 13:16:16 +02:00
|
|
|
}
|
2022-08-02 23:08:42 +10:00
|
|
|
|
2022-08-04 13:16:16 +02:00
|
|
|
return undefined;
|
2022-08-02 23:08:42 +10:00
|
|
|
};
|