50 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-08-09 17:16:51 +10:00
/* eslint-disable no-await-in-loop, no-restricted-syntax */
import execa, { Options } from 'execa';
import chalk from 'chalk';
const logger = console;
type StepOptions = {
startMessage?: string;
errorMessage?: string;
dryRun?: boolean;
debug?: boolean;
};
export const exec = async (
2022-08-09 17:16:51 +10:00
command: string | string[],
options: Options = {},
{ startMessage, errorMessage, dryRun, debug }: StepOptions = {}
): Promise<void> => {
logger.info();
if (startMessage) logger.info(startMessage);
if (dryRun) {
logger.info(`\n> ${command}\n`);
return undefined;
}
const defaultOptions: Options = {
shell: true,
stdout: debug ? 'inherit' : 'ignore',
};
try {
2022-08-09 17:16:51 +10:00
if (typeof command === 'string') {
logger.debug(`> ${command}`);
await execa.command(command, { ...defaultOptions, ...options });
} else {
for (const subcommand of command) {
logger.debug(`> ${subcommand}`);
await execa.command(subcommand, { ...defaultOptions, ...options });
}
}
} catch (err) {
logger.error(chalk.red(`An error occurred while executing: \`${command}\``));
logger.error(err);
logger.log(errorMessage);
throw err;
}
return undefined;
};