index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. const stripAnsi = require('strip-ansi');
  7. const wcwidth = require('wcwidth');
  8. const TEXT = Symbol('text');
  9. class Ora {
  10. constructor(options) {
  11. if (typeof options === 'string') {
  12. options = {
  13. text: options
  14. };
  15. }
  16. this.options = Object.assign({
  17. text: '',
  18. color: 'cyan',
  19. stream: process.stderr
  20. }, options);
  21. const sp = this.options.spinner;
  22. this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line no-nested-ternary
  23. if (this.spinner.frames === undefined) {
  24. throw new Error('Spinner must define `frames`');
  25. }
  26. this.color = this.options.color;
  27. this.hideCursor = this.options.hideCursor !== false;
  28. this.interval = this.options.interval || this.spinner.interval || 100;
  29. this.stream = this.options.stream;
  30. this.id = null;
  31. this.frameIndex = 0;
  32. this.isEnabled = typeof this.options.isEnabled === 'boolean' ? this.options.isEnabled : ((this.stream && this.stream.isTTY) && !process.env.CI);
  33. // Set *after* `this.stream`
  34. this.text = this.options.text;
  35. this.linesToClear = 0;
  36. }
  37. get text() {
  38. return this[TEXT];
  39. }
  40. get isSpinning() {
  41. return this.id !== null;
  42. }
  43. set text(value) {
  44. this[TEXT] = value;
  45. const columns = this.stream.columns || 80;
  46. this.lineCount = stripAnsi('--' + value).split('\n').reduce((count, line) => {
  47. return count + Math.max(1, Math.ceil(wcwidth(line) / columns));
  48. }, 0);
  49. }
  50. frame() {
  51. const {frames} = this.spinner;
  52. let frame = frames[this.frameIndex];
  53. if (this.color) {
  54. frame = chalk[this.color](frame);
  55. }
  56. this.frameIndex = ++this.frameIndex % frames.length;
  57. return frame + ' ' + this.text;
  58. }
  59. clear() {
  60. if (!this.isEnabled || !this.stream.isTTY) {
  61. return this;
  62. }
  63. for (let i = 0; i < this.linesToClear; i++) {
  64. if (i > 0) {
  65. this.stream.moveCursor(0, -1);
  66. }
  67. this.stream.clearLine();
  68. this.stream.cursorTo(0);
  69. }
  70. this.linesToClear = 0;
  71. return this;
  72. }
  73. render() {
  74. this.clear();
  75. this.stream.write(this.frame());
  76. this.linesToClear = this.lineCount;
  77. return this;
  78. }
  79. start(text) {
  80. if (text) {
  81. this.text = text;
  82. }
  83. if (!this.isEnabled) {
  84. this.stream.write(`- ${this.text}\n`);
  85. return this;
  86. }
  87. if (this.isSpinning) {
  88. return this;
  89. }
  90. if (this.hideCursor) {
  91. cliCursor.hide(this.stream);
  92. }
  93. this.render();
  94. this.id = setInterval(this.render.bind(this), this.interval);
  95. return this;
  96. }
  97. stop() {
  98. if (!this.isEnabled) {
  99. return this;
  100. }
  101. clearInterval(this.id);
  102. this.id = null;
  103. this.frameIndex = 0;
  104. this.clear();
  105. if (this.hideCursor) {
  106. cliCursor.show(this.stream);
  107. }
  108. return this;
  109. }
  110. succeed(text) {
  111. return this.stopAndPersist({symbol: logSymbols.success, text});
  112. }
  113. fail(text) {
  114. return this.stopAndPersist({symbol: logSymbols.error, text});
  115. }
  116. warn(text) {
  117. return this.stopAndPersist({symbol: logSymbols.warning, text});
  118. }
  119. info(text) {
  120. return this.stopAndPersist({symbol: logSymbols.info, text});
  121. }
  122. stopAndPersist(options = {}) {
  123. this.stop();
  124. this.stream.write(`${options.symbol || ' '} ${options.text || this.text}\n`);
  125. return this;
  126. }
  127. }
  128. module.exports = function (opts) {
  129. return new Ora(opts);
  130. };
  131. module.exports.promise = (action, options) => {
  132. if (typeof action.then !== 'function') {
  133. throw new TypeError('Parameter `action` must be a Promise');
  134. }
  135. const spinner = new Ora(options);
  136. spinner.start();
  137. action.then(
  138. () => {
  139. spinner.succeed();
  140. },
  141. () => {
  142. spinner.fail();
  143. }
  144. );
  145. return spinner;
  146. };