useSpinner.ts 460 B

1234567891011121314151617181920
  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 = new ora(spinnerLabel);
  6. spinner.start();
  7. try {
  8. await fn(options);
  9. spinner.succeed();
  10. } catch (e) {
  11. spinner.fail();
  12. console.log(e);
  13. if (killProcess) {
  14. process.exit(1);
  15. }
  16. }
  17. };
  18. };