1import chalk from 'chalk';
2import ora from 'ora';
3
4export type StepOptions = ora.Options;
5
6export async function newStep<Result>(
7  title: string,
8  action: (step: ora.Ora) => Promise<Result> | Result,
9  options: StepOptions = {}
10): Promise<Result> {
11  const disabled = process.env.CI || process.env.EXPO_DEBUG;
12  const step = ora({
13    text: chalk.bold(title),
14    isEnabled: !disabled,
15    stream: disabled ? process.stdout : process.stderr,
16    ...options,
17  });
18
19  step.start();
20
21  try {
22    return await action(step);
23  } catch (error) {
24    step.fail();
25    console.error(error);
26    process.exit(1);
27  }
28}
29