index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import process from 'node:process';
  2. import chalk from 'chalk';
  3. import cliCursor from 'cli-cursor';
  4. import cliSpinners from 'cli-spinners';
  5. import logSymbols from 'log-symbols';
  6. import stripAnsi from 'strip-ansi';
  7. import stringWidth from 'string-width';
  8. import isInteractive from 'is-interactive';
  9. import isUnicodeSupported from 'is-unicode-supported';
  10. import stdinDiscarder from 'stdin-discarder';
  11. class Ora {
  12. #linesToClear = 0;
  13. #isDiscardingStdin = false;
  14. #lineCount = 0;
  15. #frameIndex = -1;
  16. #lastSpinnerFrameTime = 0;
  17. #options;
  18. #spinner;
  19. #stream;
  20. #id;
  21. #initialInterval;
  22. #isEnabled;
  23. #isSilent;
  24. #indent;
  25. #text;
  26. #prefixText;
  27. #suffixText;
  28. color;
  29. constructor(options) {
  30. if (typeof options === 'string') {
  31. options = {
  32. text: options,
  33. };
  34. }
  35. this.#options = {
  36. color: 'cyan',
  37. stream: process.stderr,
  38. discardStdin: true,
  39. hideCursor: true,
  40. ...options,
  41. };
  42. // Public
  43. this.color = this.#options.color;
  44. // It's important that these use the public setters.
  45. this.spinner = this.#options.spinner;
  46. this.#initialInterval = this.#options.interval;
  47. this.#stream = this.#options.stream;
  48. this.#isEnabled = typeof this.#options.isEnabled === 'boolean' ? this.#options.isEnabled : isInteractive({stream: this.#stream});
  49. this.#isSilent = typeof this.#options.isSilent === 'boolean' ? this.#options.isSilent : false;
  50. // Set *after* `this.#stream`.
  51. // It's important that these use the public setters.
  52. this.text = this.#options.text;
  53. this.prefixText = this.#options.prefixText;
  54. this.suffixText = this.#options.suffixText;
  55. this.indent = this.#options.indent;
  56. if (process.env.NODE_ENV === 'test') {
  57. this._stream = this.#stream;
  58. this._isEnabled = this.#isEnabled;
  59. Object.defineProperty(this, '_linesToClear', {
  60. get() {
  61. return this.#linesToClear;
  62. },
  63. set(newValue) {
  64. this.#linesToClear = newValue;
  65. },
  66. });
  67. Object.defineProperty(this, '_frameIndex', {
  68. get() {
  69. return this.#frameIndex;
  70. },
  71. });
  72. Object.defineProperty(this, '_lineCount', {
  73. get() {
  74. return this.#lineCount;
  75. },
  76. });
  77. }
  78. }
  79. get indent() {
  80. return this.#indent;
  81. }
  82. set indent(indent = 0) {
  83. if (!(indent >= 0 && Number.isInteger(indent))) {
  84. throw new Error('The `indent` option must be an integer from 0 and up');
  85. }
  86. this.#indent = indent;
  87. this.#updateLineCount();
  88. }
  89. get interval() {
  90. return this.#initialInterval ?? this.#spinner.interval ?? 100;
  91. }
  92. get spinner() {
  93. return this.#spinner;
  94. }
  95. set spinner(spinner) {
  96. this.#frameIndex = -1;
  97. this.#initialInterval = undefined;
  98. if (typeof spinner === 'object') {
  99. if (spinner.frames === undefined) {
  100. throw new Error('The given spinner must have a `frames` property');
  101. }
  102. this.#spinner = spinner;
  103. } else if (!isUnicodeSupported()) {
  104. this.#spinner = cliSpinners.line;
  105. } else if (spinner === undefined) {
  106. // Set default spinner
  107. this.#spinner = cliSpinners.dots;
  108. } else if (spinner !== 'default' && cliSpinners[spinner]) {
  109. this.#spinner = cliSpinners[spinner];
  110. } else {
  111. 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.`);
  112. }
  113. }
  114. get text() {
  115. return this.#text;
  116. }
  117. set text(value = '') {
  118. this.#text = value;
  119. this.#updateLineCount();
  120. }
  121. get prefixText() {
  122. return this.#prefixText;
  123. }
  124. set prefixText(value = '') {
  125. this.#prefixText = value;
  126. this.#updateLineCount();
  127. }
  128. get suffixText() {
  129. return this.#suffixText;
  130. }
  131. set suffixText(value = '') {
  132. this.#suffixText = value;
  133. this.#updateLineCount();
  134. }
  135. get isSpinning() {
  136. return this.#id !== undefined;
  137. }
  138. #getFullPrefixText(prefixText = this.#prefixText, postfix = ' ') {
  139. if (typeof prefixText === 'string' && prefixText !== '') {
  140. return prefixText + postfix;
  141. }
  142. if (typeof prefixText === 'function') {
  143. return prefixText() + postfix;
  144. }
  145. return '';
  146. }
  147. #getFullSuffixText(suffixText = this.#suffixText, prefix = ' ') {
  148. if (typeof suffixText === 'string' && suffixText !== '') {
  149. return prefix + suffixText;
  150. }
  151. if (typeof suffixText === 'function') {
  152. return prefix + suffixText();
  153. }
  154. return '';
  155. }
  156. #updateLineCount() {
  157. const columns = this.#stream.columns ?? 80;
  158. const fullPrefixText = this.#getFullPrefixText(this.#prefixText, '-');
  159. const fullSuffixText = this.#getFullSuffixText(this.#suffixText, '-');
  160. const fullText = ' '.repeat(this.#indent) + fullPrefixText + '--' + this.#text + '--' + fullSuffixText;
  161. this.#lineCount = 0;
  162. for (const line of stripAnsi(fullText).split('\n')) {
  163. this.#lineCount += Math.max(1, Math.ceil(stringWidth(line, {countAnsiEscapeCodes: true}) / columns));
  164. }
  165. }
  166. get isEnabled() {
  167. return this.#isEnabled && !this.#isSilent;
  168. }
  169. set isEnabled(value) {
  170. if (typeof value !== 'boolean') {
  171. throw new TypeError('The `isEnabled` option must be a boolean');
  172. }
  173. this.#isEnabled = value;
  174. }
  175. get isSilent() {
  176. return this.#isSilent;
  177. }
  178. set isSilent(value) {
  179. if (typeof value !== 'boolean') {
  180. throw new TypeError('The `isSilent` option must be a boolean');
  181. }
  182. this.#isSilent = value;
  183. }
  184. frame() {
  185. // Ensure we only update the spinner frame at the wanted interval,
  186. // even if the render method is called more often.
  187. const now = Date.now();
  188. if (this.#frameIndex === -1 || now - this.#lastSpinnerFrameTime >= this.interval) {
  189. this.#frameIndex = ++this.#frameIndex % this.#spinner.frames.length;
  190. this.#lastSpinnerFrameTime = now;
  191. }
  192. const {frames} = this.#spinner;
  193. let frame = frames[this.#frameIndex];
  194. if (this.color) {
  195. frame = chalk[this.color](frame);
  196. }
  197. const fullPrefixText = (typeof this.#prefixText === 'string' && this.#prefixText !== '') ? this.#prefixText + ' ' : '';
  198. const fullText = typeof this.text === 'string' ? ' ' + this.text : '';
  199. const fullSuffixText = (typeof this.#suffixText === 'string' && this.#suffixText !== '') ? ' ' + this.#suffixText : '';
  200. return fullPrefixText + frame + fullText + fullSuffixText;
  201. }
  202. clear() {
  203. if (!this.#isEnabled || !this.#stream.isTTY) {
  204. return this;
  205. }
  206. this.#stream.cursorTo(0);
  207. for (let index = 0; index < this.#linesToClear; index++) {
  208. if (index > 0) {
  209. this.#stream.moveCursor(0, -1);
  210. }
  211. this.#stream.clearLine(1);
  212. }
  213. if (this.#indent || this.lastIndent !== this.#indent) {
  214. this.#stream.cursorTo(this.#indent);
  215. }
  216. this.lastIndent = this.#indent;
  217. this.#linesToClear = 0;
  218. return this;
  219. }
  220. render() {
  221. if (this.#isSilent) {
  222. return this;
  223. }
  224. this.clear();
  225. this.#stream.write(this.frame());
  226. this.#linesToClear = this.#lineCount;
  227. return this;
  228. }
  229. start(text) {
  230. if (text) {
  231. this.text = text;
  232. }
  233. if (this.#isSilent) {
  234. return this;
  235. }
  236. if (!this.#isEnabled) {
  237. if (this.text) {
  238. this.#stream.write(`- ${this.text}\n`);
  239. }
  240. return this;
  241. }
  242. if (this.isSpinning) {
  243. return this;
  244. }
  245. if (this.#options.hideCursor) {
  246. cliCursor.hide(this.#stream);
  247. }
  248. if (this.#options.discardStdin && process.stdin.isTTY) {
  249. this.#isDiscardingStdin = true;
  250. stdinDiscarder.start();
  251. }
  252. this.render();
  253. this.#id = setInterval(this.render.bind(this), this.interval);
  254. return this;
  255. }
  256. stop() {
  257. if (!this.#isEnabled) {
  258. return this;
  259. }
  260. clearInterval(this.#id);
  261. this.#id = undefined;
  262. this.#frameIndex = 0;
  263. this.clear();
  264. if (this.#options.hideCursor) {
  265. cliCursor.show(this.#stream);
  266. }
  267. if (this.#options.discardStdin && process.stdin.isTTY && this.#isDiscardingStdin) {
  268. stdinDiscarder.stop();
  269. this.#isDiscardingStdin = false;
  270. }
  271. return this;
  272. }
  273. succeed(text) {
  274. return this.stopAndPersist({symbol: logSymbols.success, text});
  275. }
  276. fail(text) {
  277. return this.stopAndPersist({symbol: logSymbols.error, text});
  278. }
  279. warn(text) {
  280. return this.stopAndPersist({symbol: logSymbols.warning, text});
  281. }
  282. info(text) {
  283. return this.stopAndPersist({symbol: logSymbols.info, text});
  284. }
  285. stopAndPersist(options = {}) {
  286. if (this.#isSilent) {
  287. return this;
  288. }
  289. const prefixText = options.prefixText ?? this.#prefixText;
  290. const fullPrefixText = this.#getFullPrefixText(prefixText, ' ');
  291. const symbolText = options.symbol ?? ' ';
  292. const text = options.text ?? this.text;
  293. const separatorText = symbolText ? ' ' : '';
  294. const fullText = (typeof text === 'string') ? separatorText + text : '';
  295. const suffixText = options.suffixText ?? this.#suffixText;
  296. const fullSuffixText = this.#getFullSuffixText(suffixText, ' ');
  297. const textToWrite = fullPrefixText + symbolText + fullText + fullSuffixText + '\n';
  298. this.stop();
  299. this.#stream.write(textToWrite);
  300. return this;
  301. }
  302. }
  303. export default function ora(options) {
  304. return new Ora(options);
  305. }
  306. export async function oraPromise(action, options) {
  307. const actionIsFunction = typeof action === 'function';
  308. const actionIsPromise = typeof action.then === 'function';
  309. if (!actionIsFunction && !actionIsPromise) {
  310. throw new TypeError('Parameter `action` must be a Function or a Promise');
  311. }
  312. const {successText, failText} = typeof options === 'object'
  313. ? options
  314. : {successText: undefined, failText: undefined};
  315. const spinner = ora(options).start();
  316. try {
  317. const promise = actionIsFunction ? action(spinner) : action;
  318. const result = await promise;
  319. spinner.succeed(
  320. successText === undefined
  321. ? undefined
  322. : (typeof successText === 'string' ? successText : successText(result)),
  323. );
  324. return result;
  325. } catch (error) {
  326. spinner.fail(
  327. failText === undefined
  328. ? undefined
  329. : (typeof failText === 'string' ? failText : failText(error)),
  330. );
  331. throw error;
  332. }
  333. }
  334. export {default as spinners} from 'cli-spinners';