useSpinner.ts 435 B

12345678910111213141516171819
  1. import ora from 'ora';
  2. type FnToSpin<T> = (options: T) => Promise<void>;
  3. export const useSpinner = <T>(spinnerLabel: string, fn: FnToSpin<T>, killProcess = true) => {
  4. return async (options: T) => {
  5. const spinner = ora(spinnerLabel);
  6. spinner.start();
  7. try {
  8. await fn(options);
  9. spinner.succeed();
  10. } catch (e) {
  11. spinner.fail(e);
  12. if (killProcess) {
  13. process.exit(1);
  14. }
  15. }
  16. };
  17. };