2022-07-25 22:43:46 +10:00
|
|
|
import { getCommand, OptionSpecifier, OptionValues } from './options';
|
2022-08-02 23:08:42 +10:00
|
|
|
import { exec } from './exec';
|
2022-07-25 22:43:46 +10:00
|
|
|
|
2022-07-26 16:12:07 +10:00
|
|
|
const cliExecutable = require.resolve('../../code/lib/cli/bin/index.js');
|
2022-07-25 22:43:46 +10:00
|
|
|
|
2022-07-27 22:09:36 +10:00
|
|
|
export type CLIStep<TOptions extends OptionSpecifier> = {
|
2022-07-25 22:43:46 +10:00
|
|
|
command: string;
|
|
|
|
description: string;
|
|
|
|
hasArgument?: boolean;
|
|
|
|
icon: string;
|
|
|
|
// It would be kind of great to be able to share these with `lib/cli/src/generate.ts`
|
2022-07-27 22:09:36 +10:00
|
|
|
options: TOptions;
|
2022-07-25 22:43:46 +10:00
|
|
|
};
|
|
|
|
|
2022-07-27 22:09:36 +10:00
|
|
|
export async function executeCLIStep<TOptions extends OptionSpecifier>(
|
|
|
|
cliStep: CLIStep<TOptions>,
|
2022-07-25 22:43:46 +10:00
|
|
|
options: {
|
|
|
|
argument?: string;
|
2022-07-27 22:17:39 +10:00
|
|
|
optionValues?: Partial<OptionValues<TOptions>>;
|
2022-07-25 22:43:46 +10:00
|
|
|
cwd: string;
|
2022-07-26 16:20:49 +10:00
|
|
|
dryRun?: boolean;
|
2022-07-25 22:43:46 +10:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
if (cliStep.hasArgument && !options.argument)
|
|
|
|
throw new Error(`Argument required for ${cliStep.command} command.`);
|
|
|
|
|
|
|
|
const prefix = `node ${cliExecutable} ${cliStep.command}`;
|
|
|
|
const command = getCommand(
|
|
|
|
cliStep.hasArgument ? `${prefix} ${options.argument}` : prefix,
|
|
|
|
cliStep.options,
|
2022-07-27 22:17:39 +10:00
|
|
|
options.optionValues || {}
|
2022-07-25 22:43:46 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
await exec(
|
|
|
|
command,
|
|
|
|
{ cwd: options.cwd },
|
|
|
|
{
|
|
|
|
startMessage: `${cliStep.icon} ${cliStep.description}`,
|
|
|
|
errorMessage: `🚨 ${cliStep.description} failed`,
|
2022-07-26 16:20:49 +10:00
|
|
|
dryRun: options.dryRun,
|
2022-07-25 22:43:46 +10:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|