index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. const chalk = require('chalk');
  3. const cliCursor = require('cli-cursor');
  4. const cliSpinners = require('cli-spinners');
  5. const logSymbols = require('log-symbols');
  6. class Ora {
  7. constructor(options) {
  8. if (typeof options === 'string') {
  9. options = {
  10. text: options
  11. };
  12. }
  13. this.options = Object.assign({
  14. text: '',
  15. color: 'cyan',
  16. stream: process.stderr
  17. }, options);
  18. const sp = this.options.spinner;
  19. this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line no-nested-ternary
  20. if (this.spinner.frames === undefined) {
  21. throw new Error('Spinner must define `frames`');
  22. }
  23. this.text = this.options.text;
  24. this.color = this.options.color;
  25. this.interval = this.options.interval || this.spinner.interval || 100;
  26. this.stream = this.options.stream;
  27. this.id = null;
  28. this.frameIndex = 0;
  29. this.enabled = typeof this.options.enabled === 'boolean' ? this.options.enabled : ((this.stream && this.stream.isTTY) && !process.env.CI);
  30. }
  31. frame() {
  32. const frames = this.spinner.frames;
  33. let frame = frames[this.frameIndex];
  34. if (this.color) {
  35. frame = chalk[this.color](frame);
  36. }
  37. this.frameIndex = ++this.frameIndex % frames.length;
  38. return frame + ' ' + this.text;
  39. }
  40. clear() {
  41. if (!this.enabled) {
  42. return this;
  43. }
  44. this.stream.clearLine();
  45. this.stream.cursorTo(0);
  46. return this;
  47. }
  48. render() {
  49. this.clear();
  50. this.stream.write(this.frame());
  51. return this;
  52. }
  53. start(text) {
  54. if (text) {
  55. this.text = text;
  56. }
  57. if (!this.enabled || this.id) {
  58. return this;
  59. }
  60. cliCursor.hide(this.stream);
  61. this.render();
  62. this.id = setInterval(this.render.bind(this), this.interval);
  63. return this;
  64. }
  65. stop() {
  66. if (!this.enabled) {
  67. return this;
  68. }
  69. clearInterval(this.id);
  70. this.id = null;
  71. this.frameIndex = 0;
  72. this.clear();
  73. cliCursor.show(this.stream);
  74. return this;
  75. }
  76. succeed(text) {
  77. return this.stopAndPersist({symbol: logSymbols.success, text});
  78. }
  79. fail(text) {
  80. return this.stopAndPersist({symbol: logSymbols.error, text});
  81. }
  82. warn(text) {
  83. return this.stopAndPersist({symbol: logSymbols.warning, text});
  84. }
  85. info(text) {
  86. return this.stopAndPersist({symbol: logSymbols.info, text});
  87. }
  88. stopAndPersist(options) {
  89. if (!this.enabled) {
  90. return this;
  91. }
  92. // Legacy argument
  93. // TODO: Deprecate sometime in the future
  94. if (typeof options === 'string') {
  95. options = {
  96. symbol: options
  97. };
  98. }
  99. options = options || {};
  100. this.stop();
  101. this.stream.write(`${options.symbol || ' '} ${options.text || this.text}\n`);
  102. return this;
  103. }
  104. }
  105. module.exports = function (opts) {
  106. return new Ora(opts);
  107. };
  108. module.exports.promise = (action, options) => {
  109. if (typeof action.then !== 'function') {
  110. throw new TypeError('Parameter `action` must be a Promise');
  111. }
  112. const spinner = new Ora(options);
  113. spinner.start();
  114. action.then(
  115. () => {
  116. spinner.succeed();
  117. },
  118. () => {
  119. spinner.fail();
  120. }
  121. );
  122. return spinner;
  123. };