選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. 'use strict';
  2. const readline = require('readline');
  3. const chalk = require('chalk');
  4. const cliCursor = require('cli-cursor');
  5. const cliSpinners = require('cli-spinners');
  6. const logSymbols = require('log-symbols');
  7. const stripAnsi = require('strip-ansi');
  8. const wcwidth = require('wcwidth');
  9. const isInteractive = require('is-interactive');
  10. const isUnicodeSupported = require('is-unicode-supported');
  11. const {BufferListStream} = require('bl');
  12. const TEXT = Symbol('text');
  13. const PREFIX_TEXT = Symbol('prefixText');
  14. const ASCII_ETX_CODE = 0x03; // Ctrl+C emits this code
  15. class StdinDiscarder {
  16. constructor() {
  17. this.requests = 0;
  18. this.mutedStream = new BufferListStream();
  19. this.mutedStream.pipe(process.stdout);
  20. const self = this; // eslint-disable-line unicorn/no-this-assignment
  21. this.ourEmit = function (event, data, ...args) {
  22. const {stdin} = process;
  23. if (self.requests > 0 || stdin.emit === self.ourEmit) {
  24. if (event === 'keypress') { // Fixes readline behavior
  25. return;
  26. }
  27. if (event === 'data' && data.includes(ASCII_ETX_CODE)) {
  28. process.emit('SIGINT');
  29. }
  30. Reflect.apply(self.oldEmit, this, [event, data, ...args]);
  31. } else {
  32. Reflect.apply(process.stdin.emit, this, [event, data, ...args]);
  33. }
  34. };
  35. }
  36. start() {
  37. this.requests++;
  38. if (this.requests === 1) {
  39. this.realStart();
  40. }
  41. }
  42. stop() {
  43. if (this.requests <= 0) {
  44. throw new Error('`stop` called more times than `start`');
  45. }
  46. this.requests--;
  47. if (this.requests === 0) {
  48. this.realStop();
  49. }
  50. }
  51. realStart() {
  52. // No known way to make it work reliably on Windows
  53. if (process.platform === 'win32') {
  54. return;
  55. }
  56. this.rl = readline.createInterface({
  57. input: process.stdin,
  58. output: this.mutedStream
  59. });
  60. this.rl.on('SIGINT', () => {
  61. if (process.listenerCount('SIGINT') === 0) {
  62. process.emit('SIGINT');
  63. } else {
  64. this.rl.close();
  65. process.kill(process.pid, 'SIGINT');
  66. }
  67. });
  68. }
  69. realStop() {
  70. if (process.platform === 'win32') {
  71. return;
  72. }
  73. this.rl.close();
  74. this.rl = undefined;
  75. }
  76. }
  77. let stdinDiscarder;
  78. class Ora {
  79. constructor(options) {
  80. if (!stdinDiscarder) {
  81. stdinDiscarder = new StdinDiscarder();
  82. }
  83. if (typeof options === 'string') {
  84. options = {
  85. text: options
  86. };
  87. }
  88. this.options = {
  89. text: '',
  90. color: 'cyan',
  91. stream: process.stderr,
  92. discardStdin: true,
  93. ...options
  94. };
  95. this.spinner = this.options.spinner;
  96. this.color = this.options.color;
  97. this.hideCursor = this.options.hideCursor !== false;
  98. this.interval = this.options.interval || this.spinner.interval || 100;
  99. this.stream = this.options.stream;
  100. this.id = undefined;
  101. this.isEnabled = typeof this.options.isEnabled === 'boolean' ? this.options.isEnabled : isInteractive({stream: this.stream});
  102. this.isSilent = typeof this.options.isSilent === 'boolean' ? this.options.isSilent : false;
  103. // Set *after* `this.stream`
  104. this.text = this.options.text;
  105. this.prefixText = this.options.prefixText;
  106. this.linesToClear = 0;
  107. this.indent = this.options.indent;
  108. this.discardStdin = this.options.discardStdin;
  109. this.isDiscardingStdin = false;
  110. }
  111. get indent() {
  112. return this._indent;
  113. }
  114. set indent(indent = 0) {
  115. if (!(indent >= 0 && Number.isInteger(indent))) {
  116. throw new Error('The `indent` option must be an integer from 0 and up');
  117. }
  118. this._indent = indent;
  119. }
  120. _updateInterval(interval) {
  121. if (interval !== undefined) {
  122. this.interval = interval;
  123. }
  124. }
  125. get spinner() {
  126. return this._spinner;
  127. }
  128. set spinner(spinner) {
  129. this.frameIndex = 0;
  130. if (typeof spinner === 'object') {
  131. if (spinner.frames === undefined) {
  132. throw new Error('The given spinner must have a `frames` property');
  133. }
  134. this._spinner = spinner;
  135. } else if (!isUnicodeSupported()) {
  136. this._spinner = cliSpinners.line;
  137. } else if (spinner === undefined) {
  138. // Set default spinner
  139. this._spinner = cliSpinners.dots;
  140. } else if (spinner !== 'default' && cliSpinners[spinner]) {
  141. this._spinner = cliSpinners[spinner];
  142. } else {
  143. throw new Error(`There is no built-in spinner named '${spinner}'. See https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json for a full list.`);
  144. }
  145. this._updateInterval(this._spinner.interval);
  146. }
  147. get text() {
  148. return this[TEXT];
  149. }
  150. set text(value) {
  151. this[TEXT] = value;
  152. this.updateLineCount();
  153. }
  154. get prefixText() {
  155. return this[PREFIX_TEXT];
  156. }
  157. set prefixText(value) {
  158. this[PREFIX_TEXT] = value;
  159. this.updateLineCount();
  160. }
  161. get isSpinning() {
  162. return this.id !== undefined;
  163. }
  164. getFullPrefixText(prefixText = this[PREFIX_TEXT], postfix = ' ') {
  165. if (typeof prefixText === 'string') {
  166. return prefixText + postfix;
  167. }
  168. if (typeof prefixText === 'function') {
  169. return prefixText() + postfix;
  170. }
  171. return '';
  172. }
  173. updateLineCount() {
  174. const columns = this.stream.columns || 80;
  175. const fullPrefixText = this.getFullPrefixText(this.prefixText, '-');
  176. this.lineCount = 0;
  177. for (const line of stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n')) {
  178. this.lineCount += Math.max(1, Math.ceil(wcwidth(line) / columns));
  179. }
  180. }
  181. get isEnabled() {
  182. return this._isEnabled && !this.isSilent;
  183. }
  184. set isEnabled(value) {
  185. if (typeof value !== 'boolean') {
  186. throw new TypeError('The `isEnabled` option must be a boolean');
  187. }
  188. this._isEnabled = value;
  189. }
  190. get isSilent() {
  191. return this._isSilent;
  192. }
  193. set isSilent(value) {
  194. if (typeof value !== 'boolean') {
  195. throw new TypeError('The `isSilent` option must be a boolean');
  196. }
  197. this._isSilent = value;
  198. }
  199. frame() {
  200. const {frames} = this.spinner;
  201. let frame = frames[this.frameIndex];
  202. if (this.color) {
  203. frame = chalk[this.color](frame);
  204. }
  205. this.frameIndex = ++this.frameIndex % frames.length;
  206. const fullPrefixText = (typeof this.prefixText === 'string' && this.prefixText !== '') ? this.prefixText + ' ' : '';
  207. const fullText = typeof this.text === 'string' ? ' ' + this.text : '';
  208. return fullPrefixText + frame + fullText;
  209. }
  210. clear() {
  211. if (!this.isEnabled || !this.stream.isTTY) {
  212. return this;
  213. }
  214. for (let i = 0; i < this.linesToClear; i++) {
  215. if (i > 0) {
  216. this.stream.moveCursor(0, -1);
  217. }
  218. this.stream.clearLine();
  219. this.stream.cursorTo(this.indent);
  220. }
  221. this.linesToClear = 0;
  222. return this;
  223. }
  224. render() {
  225. if (this.isSilent) {
  226. return this;
  227. }
  228. this.clear();
  229. this.stream.write(this.frame());
  230. this.linesToClear = this.lineCount;
  231. return this;
  232. }
  233. start(text) {
  234. if (text) {
  235. this.text = text;
  236. }
  237. if (this.isSilent) {
  238. return this;
  239. }
  240. if (!this.isEnabled) {
  241. if (this.text) {
  242. this.stream.write(`- ${this.text}\n`);
  243. }
  244. return this;
  245. }
  246. if (this.isSpinning) {
  247. return this;
  248. }
  249. if (this.hideCursor) {
  250. cliCursor.hide(this.stream);
  251. }
  252. if (this.discardStdin && process.stdin.isTTY) {
  253. this.isDiscardingStdin = true;
  254. stdinDiscarder.start();
  255. }
  256. this.render();
  257. this.id = setInterval(this.render.bind(this), this.interval);
  258. return this;
  259. }
  260. stop() {
  261. if (!this.isEnabled) {
  262. return this;
  263. }
  264. clearInterval(this.id);
  265. this.id = undefined;
  266. this.frameIndex = 0;
  267. this.clear();
  268. if (this.hideCursor) {
  269. cliCursor.show(this.stream);
  270. }
  271. if (this.discardStdin && process.stdin.isTTY && this.isDiscardingStdin) {
  272. stdinDiscarder.stop();
  273. this.isDiscardingStdin = false;
  274. }
  275. return this;
  276. }
  277. succeed(text) {
  278. return this.stopAndPersist({symbol: logSymbols.success, text});
  279. }
  280. fail(text) {
  281. return this.stopAndPersist({symbol: logSymbols.error, text});
  282. }
  283. warn(text) {
  284. return this.stopAndPersist({symbol: logSymbols.warning, text});
  285. }
  286. info(text) {
  287. return this.stopAndPersist({symbol: logSymbols.info, text});
  288. }
  289. stopAndPersist(options = {}) {
  290. if (this.isSilent) {
  291. return this;
  292. }
  293. const prefixText = options.prefixText || this.prefixText;
  294. const text = options.text || this.text;
  295. const fullText = (typeof text === 'string') ? ' ' + text : '';
  296. this.stop();
  297. this.stream.write(`${this.getFullPrefixText(prefixText, ' ')}${options.symbol || ' '}${fullText}\n`);
  298. return this;
  299. }
  300. }
  301. const oraFactory = function (options) {
  302. return new Ora(options);
  303. };
  304. module.exports = oraFactory;
  305. module.exports.promise = (action, options) => {
  306. // eslint-disable-next-line promise/prefer-await-to-then
  307. if (typeof action.then !== 'function') {
  308. throw new TypeError('Parameter `action` must be a Promise');
  309. }
  310. const spinner = new Ora(options);
  311. spinner.start();
  312. (async () => {
  313. try {
  314. await action;
  315. spinner.succeed();
  316. } catch {
  317. spinner.fail();
  318. }
  319. })();
  320. return spinner;
  321. };