版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

418 satır
9.1 KiB

  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 wcwidth from 'wcwidth';
  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 = 0;
  16. #options;
  17. #spinner;
  18. #stream;
  19. #id;
  20. #initialInterval;
  21. #isEnabled;
  22. #isSilent;
  23. #indent;
  24. #text;
  25. #prefixText;
  26. #suffixText;
  27. color;
  28. constructor(options) {
  29. if (typeof options === 'string') {
  30. options = {
  31. text: options,
  32. };
  33. }
  34. this.#options = {
  35. color: 'cyan',
  36. stream: process.stderr,
  37. discardStdin: true,
  38. hideCursor: true,
  39. ...options,
  40. };
  41. // Public
  42. this.color = this.#options.color;
  43. // It's important that these use the public setters.
  44. this.spinner = this.#options.spinner;
  45. this.#initialInterval = this.#options.interval;
  46. this.#stream = this.#options.stream;
  47. this.#isEnabled = typeof this.#options.isEnabled === 'boolean' ? this.#options.isEnabled : isInteractive({stream: this.#stream});
  48. this.#isSilent = typeof this.#options.isSilent === 'boolean' ? this.#options.isSilent : false;
  49. // Set *after* `this.#stream`.
  50. // It's important that these use the public setters.
  51. this.text = this.#options.text;
  52. this.prefixText = this.#options.prefixText;
  53. this.suffixText = this.#options.suffixText;
  54. this.indent = this.#options.indent;
  55. if (process.env.NODE_ENV === 'test') {
  56. this._stream = this.#stream;
  57. this._isEnabled = this.#isEnabled;
  58. Object.defineProperty(this, '_linesToClear', {
  59. get() {
  60. return this.#linesToClear;
  61. },
  62. set(newValue) {
  63. this.#linesToClear = newValue;
  64. },
  65. });
  66. Object.defineProperty(this, '_frameIndex', {
  67. get() {
  68. return this.#frameIndex;
  69. },
  70. });
  71. Object.defineProperty(this, '_lineCount', {
  72. get() {
  73. return this.#lineCount;
  74. },
  75. });
  76. }
  77. }
  78. get indent() {
  79. return this.#indent;
  80. }
  81. set indent(indent = 0) {
  82. if (!(indent >= 0 && Number.isInteger(indent))) {
  83. throw new Error('The `indent` option must be an integer from 0 and up');
  84. }
  85. this.#indent = indent;
  86. this.updateLineCount();
  87. }
  88. get interval() {
  89. return this.#initialInterval || this.#spinner.interval || 100;
  90. }
  91. get spinner() {
  92. return this.#spinner;
  93. }
  94. set spinner(spinner) {
  95. this.#frameIndex = 0;
  96. this.#initialInterval = undefined;
  97. if (typeof spinner === 'object') {
  98. if (spinner.frames === undefined) {
  99. throw new Error('The given spinner must have a `frames` property');
  100. }
  101. this.#spinner = spinner;
  102. } else if (!isUnicodeSupported()) {
  103. this.#spinner = cliSpinners.line;
  104. } else if (spinner === undefined) {
  105. // Set default spinner
  106. this.#spinner = cliSpinners.dots;
  107. } else if (spinner !== 'default' && cliSpinners[spinner]) {
  108. this.#spinner = cliSpinners[spinner];
  109. } else {
  110. 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.`);
  111. }
  112. }
  113. get text() {
  114. return this.#text;
  115. }
  116. set text(value) {
  117. this.#text = value || '';
  118. this.updateLineCount();
  119. }
  120. get prefixText() {
  121. return this.#prefixText;
  122. }
  123. set prefixText(value) {
  124. this.#prefixText = value || '';
  125. this.updateLineCount();
  126. }
  127. get suffixText() {
  128. return this.#suffixText;
  129. }
  130. set suffixText(value) {
  131. this.#suffixText = value || '';
  132. this.updateLineCount();
  133. }
  134. get isSpinning() {
  135. return this.#id !== undefined;
  136. }
  137. // TODO: Use private methods when targeting Node.js 14.
  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(wcwidth(line) / 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. const {frames} = this.#spinner;
  186. let frame = frames[this.#frameIndex];
  187. if (this.color) {
  188. frame = chalk[this.color](frame);
  189. }
  190. this.#frameIndex = ++this.#frameIndex % frames.length;
  191. const fullPrefixText = (typeof this.#prefixText === 'string' && this.#prefixText !== '') ? this.#prefixText + ' ' : '';
  192. const fullText = typeof this.text === 'string' ? ' ' + this.text : '';
  193. const fullSuffixText = (typeof this.#suffixText === 'string' && this.#suffixText !== '') ? ' ' + this.#suffixText : '';
  194. return fullPrefixText + frame + fullText + fullSuffixText;
  195. }
  196. clear() {
  197. if (!this.#isEnabled || !this.#stream.isTTY) {
  198. return this;
  199. }
  200. this.#stream.cursorTo(0);
  201. for (let index = 0; index < this.#linesToClear; index++) {
  202. if (index > 0) {
  203. this.#stream.moveCursor(0, -1);
  204. }
  205. this.#stream.clearLine(1);
  206. }
  207. if (this.#indent || this.lastIndent !== this.#indent) {
  208. this.#stream.cursorTo(this.#indent);
  209. }
  210. this.lastIndent = this.#indent;
  211. this.#linesToClear = 0;
  212. return this;
  213. }
  214. render() {
  215. if (this.#isSilent) {
  216. return this;
  217. }
  218. this.clear();
  219. this.#stream.write(this.frame());
  220. this.#linesToClear = this.#lineCount;
  221. return this;
  222. }
  223. start(text) {
  224. if (text) {
  225. this.text = text;
  226. }
  227. if (this.#isSilent) {
  228. return this;
  229. }
  230. if (!this.#isEnabled) {
  231. if (this.text) {
  232. this.#stream.write(`- ${this.text}\n`);
  233. }
  234. return this;
  235. }
  236. if (this.isSpinning) {
  237. return this;
  238. }
  239. if (this.#options.hideCursor) {
  240. cliCursor.hide(this.#stream);
  241. }
  242. if (this.#options.discardStdin && process.stdin.isTTY) {
  243. this.#isDiscardingStdin = true;
  244. stdinDiscarder.start();
  245. }
  246. this.render();
  247. this.#id = setInterval(this.render.bind(this), this.interval);
  248. return this;
  249. }
  250. stop() {
  251. if (!this.#isEnabled) {
  252. return this;
  253. }
  254. clearInterval(this.#id);
  255. this.#id = undefined;
  256. this.#frameIndex = 0;
  257. this.clear();
  258. if (this.#options.hideCursor) {
  259. cliCursor.show(this.#stream);
  260. }
  261. if (this.#options.discardStdin && process.stdin.isTTY && this.#isDiscardingStdin) {
  262. stdinDiscarder.stop();
  263. this.#isDiscardingStdin = false;
  264. }
  265. return this;
  266. }
  267. succeed(text) {
  268. return this.stopAndPersist({symbol: logSymbols.success, text});
  269. }
  270. fail(text) {
  271. return this.stopAndPersist({symbol: logSymbols.error, text});
  272. }
  273. warn(text) {
  274. return this.stopAndPersist({symbol: logSymbols.warning, text});
  275. }
  276. info(text) {
  277. return this.stopAndPersist({symbol: logSymbols.info, text});
  278. }
  279. stopAndPersist(options = {}) {
  280. if (this.#isSilent) {
  281. return this;
  282. }
  283. const prefixText = options.prefixText ?? this.#prefixText;
  284. const fullPrefixText = this.getFullPrefixText(prefixText, ' ');
  285. const symbolText = options.symbol ?? ' ';
  286. const text = options.text ?? this.text;
  287. const fullText = (typeof text === 'string') ? ' ' + text : '';
  288. const suffixText = options.suffixText ?? this.#suffixText;
  289. const fullSuffixText = this.getFullSuffixText(suffixText, ' ');
  290. const textToWrite = fullPrefixText + symbolText + fullText + fullSuffixText + '\n';
  291. this.stop();
  292. this.#stream.write(textToWrite);
  293. return this;
  294. }
  295. }
  296. export default function ora(options) {
  297. return new Ora(options);
  298. }
  299. export async function oraPromise(action, options) {
  300. const actionIsFunction = typeof action === 'function';
  301. const actionIsPromise = typeof action.then === 'function';
  302. if (!actionIsFunction && !actionIsPromise) {
  303. throw new TypeError('Parameter `action` must be a Function or a Promise');
  304. }
  305. const {successText, failText} = typeof options === 'object'
  306. ? options
  307. : {successText: undefined, failText: undefined};
  308. const spinner = ora(options).start();
  309. try {
  310. const promise = actionIsFunction ? action(spinner) : action;
  311. const result = await promise;
  312. spinner.succeed(
  313. successText === undefined
  314. ? undefined
  315. : (typeof successText === 'string' ? successText : successText(result)),
  316. );
  317. return result;
  318. } catch (error) {
  319. spinner.fail(
  320. failText === undefined
  321. ? undefined
  322. : (typeof failText === 'string' ? failText : failText(error)),
  323. );
  324. throw error;
  325. }
  326. }
  327. export {default as spinners} from 'cli-spinners';