spinner.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const ora = require('ora')
  2. const chalk = require('chalk')
  3. const spinner = ora()
  4. let lastMsg = null
  5. let isPaused = false
  6. exports.logWithSpinner = (symbol, msg) => {
  7. if (!msg) {
  8. msg = symbol
  9. symbol = chalk.green('✔')
  10. }
  11. if (lastMsg) {
  12. spinner.stopAndPersist({
  13. symbol: lastMsg.symbol,
  14. text: lastMsg.text
  15. })
  16. }
  17. spinner.text = ' ' + msg
  18. lastMsg = {
  19. symbol: symbol + ' ',
  20. text: msg
  21. }
  22. spinner.start()
  23. }
  24. exports.stopSpinner = (persist) => {
  25. if (!spinner.isSpinning) {
  26. return
  27. }
  28. if (lastMsg && persist !== false) {
  29. spinner.stopAndPersist({
  30. symbol: lastMsg.symbol,
  31. text: lastMsg.text
  32. })
  33. } else {
  34. spinner.stop()
  35. }
  36. lastMsg = null
  37. }
  38. exports.pauseSpinner = () => {
  39. if (spinner.isSpinning) {
  40. spinner.stop()
  41. isPaused = true
  42. }
  43. }
  44. exports.resumeSpinner = () => {
  45. if (isPaused) {
  46. spinner.start()
  47. isPaused = false
  48. }
  49. }
  50. exports.failSpinner = (text) => {
  51. spinner.fail(text)
  52. }
  53. // silent all logs except errors during tests and keep record
  54. if (process.env.VUE_CLI_TEST) {
  55. require('./_silence')('spinner', exports)
  56. }