2019-03-05 15:56:29 +08:00
|
|
|
import ora from 'ora';
|
|
|
|
|
|
|
|
type FnToSpin<T> = (options: T) => Promise<void>;
|
|
|
|
|
|
|
|
export const useSpinner = <T>(spinnerLabel: string, fn: FnToSpin<T>, killProcess = true) => {
|
|
|
|
return async (options: T) => {
|
2019-03-12 03:54:11 +08:00
|
|
|
const spinner = ora(spinnerLabel);
|
2019-03-05 15:56:29 +08:00
|
|
|
spinner.start();
|
|
|
|
try {
|
|
|
|
await fn(options);
|
|
|
|
spinner.succeed();
|
|
|
|
} catch (e) {
|
2019-03-18 20:31:57 +08:00
|
|
|
spinner.fail(e);
|
2019-03-05 15:56:29 +08:00
|
|
|
if (killProcess) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|