spinner.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = createSpinner;
  6. var _ora = _interopRequireDefault(require("next/dist/compiled/ora"));
  7. function createSpinner(text, options = {}, logFn = console.log) {
  8. let spinner;
  9. let prefixText = text && typeof text === "object" && text.prefixText;
  10. if (process.stdout.isTTY) {
  11. spinner = (0, _ora).default({
  12. text: typeof text === "string" ? text : undefined,
  13. prefixText: typeof prefixText === "string" ? prefixText : undefined,
  14. spinner: dotsSpinner,
  15. stream: process.stdout,
  16. ...options
  17. }).start();
  18. // Add capturing of console.log/warn/error to allow pausing
  19. // the spinner before logging and then restarting spinner after
  20. const origLog = console.log;
  21. const origWarn = console.warn;
  22. const origError = console.error;
  23. const origStop = spinner.stop.bind(spinner);
  24. const origStopAndPersist = spinner.stopAndPersist.bind(spinner);
  25. const logHandle = (method, args)=>{
  26. origStop();
  27. method(...args);
  28. spinner.start();
  29. };
  30. console.log = (...args)=>logHandle(origLog, args);
  31. console.warn = (...args)=>logHandle(origWarn, args);
  32. console.error = (...args)=>logHandle(origError, args);
  33. const resetLog = ()=>{
  34. console.log = origLog;
  35. console.warn = origWarn;
  36. console.error = origError;
  37. };
  38. spinner.stop = ()=>{
  39. origStop();
  40. resetLog();
  41. return spinner;
  42. };
  43. spinner.stopAndPersist = ()=>{
  44. origStopAndPersist();
  45. resetLog();
  46. return spinner;
  47. };
  48. } else if (prefixText || text) {
  49. logFn(prefixText ? prefixText + "..." : text);
  50. }
  51. return spinner;
  52. }
  53. function _interopRequireDefault(obj) {
  54. return obj && obj.__esModule ? obj : {
  55. default: obj
  56. };
  57. }
  58. const dotsSpinner = {
  59. frames: [
  60. ".",
  61. "..",
  62. "..."
  63. ],
  64. interval: 200
  65. };
  66. //# sourceMappingURL=spinner.js.map