tis-cli前端项目快速搭建命令行工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
6.3 KiB

4 years ago
  1. import {SpinnerName} from 'cli-spinners';
  2. export interface Spinner {
  3. readonly interval?: number;
  4. readonly frames: string[];
  5. }
  6. export type Color =
  7. | 'black'
  8. | 'red'
  9. | 'green'
  10. | 'yellow'
  11. | 'blue'
  12. | 'magenta'
  13. | 'cyan'
  14. | 'white'
  15. | 'gray';
  16. export type PrefixTextGenerator = () => string;
  17. export interface Options {
  18. /**
  19. Text to display after the spinner.
  20. */
  21. readonly text?: string;
  22. /**
  23. Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string.
  24. */
  25. readonly prefixText?: string | PrefixTextGenerator;
  26. /**
  27. Name of one of the provided spinners. See [`example.js`](https://github.com/BendingBender/ora/blob/main/example.js) in this repo if you want to test out different spinners. On Windows, it will always use the line spinner as the Windows command-line doesn't have proper Unicode support.
  28. @default 'dots'
  29. Or an object like:
  30. @example
  31. ```
  32. {
  33. interval: 80, // Optional
  34. frames: ['-', '+', '-']
  35. }
  36. ```
  37. */
  38. readonly spinner?: SpinnerName | Spinner;
  39. /**
  40. Color of the spinner.
  41. @default 'cyan'
  42. */
  43. readonly color?: Color;
  44. /**
  45. Set to `false` to stop Ora from hiding the cursor.
  46. @default true
  47. */
  48. readonly hideCursor?: boolean;
  49. /**
  50. Indent the spinner with the given number of spaces.
  51. @default 0
  52. */
  53. readonly indent?: number;
  54. /**
  55. Interval between each frame.
  56. Spinners provide their own recommended interval, so you don't really need to specify this.
  57. Default: Provided by the spinner or `100`.
  58. */
  59. readonly interval?: number;
  60. /**
  61. Stream to write the output.
  62. You could for example set this to `process.stdout` instead.
  63. @default process.stderr
  64. */
  65. readonly stream?: NodeJS.WritableStream;
  66. /**
  67. Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment.
  68. Note that `{isEnabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text.
  69. */
  70. readonly isEnabled?: boolean;
  71. /**
  72. Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
  73. @default false
  74. */
  75. readonly isSilent?: boolean;
  76. /**
  77. Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on `Enter` key presses, and prevents buffering of input while the spinner is running.
  78. This has no effect on Windows as there's no good way to implement discarding stdin properly there.
  79. @default true
  80. */
  81. readonly discardStdin?: boolean;
  82. }
  83. export interface PersistOptions {
  84. /**
  85. Symbol to replace the spinner with.
  86. @default ' '
  87. */
  88. readonly symbol?: string;
  89. /**
  90. Text to be persisted after the symbol.
  91. Default: Current `text`.
  92. */
  93. readonly text?: string;
  94. /**
  95. Text or a function that returns text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
  96. Default: Current `prefixText`.
  97. */
  98. readonly prefixText?: string | PrefixTextGenerator;
  99. }
  100. export interface PromiseOptions<T> extends Options {
  101. /**
  102. The new text of the spinner when the promise is resolved.
  103. Keeps the existing text if `undefined`.
  104. */
  105. successText?: string | ((result: T) => string) | undefined;
  106. /**
  107. The new text of the spinner when the promise is rejected.
  108. Keeps the existing text if `undefined`.
  109. */
  110. failText?: string | ((error: Error) => string) | undefined;
  111. }
  112. export interface Ora {
  113. /**
  114. A boolean of whether the instance is currently spinning.
  115. */
  116. readonly isSpinning: boolean;
  117. /**
  118. Change the text after the spinner.
  119. */
  120. text: string;
  121. /**
  122. Change the text or function that returns text before the spinner. No prefix text will be displayed if set to an empty string.
  123. */
  124. prefixText: string | PrefixTextGenerator;
  125. /**
  126. Change the spinner color.
  127. */
  128. color: Color;
  129. /**
  130. Change the spinner.
  131. */
  132. spinner: SpinnerName | Spinner;
  133. /**
  134. Change the spinner indent.
  135. */
  136. indent: number;
  137. /**
  138. Start the spinner.
  139. @param text - Set the current text.
  140. @returns The spinner instance.
  141. */
  142. start(text?: string): Ora;
  143. /**
  144. Stop and clear the spinner.
  145. @returns The spinner instance.
  146. */
  147. stop(): Ora;
  148. /**
  149. Stop the spinner, change it to a green `` and persist the current text, or `text` if provided.
  150. @param text - Will persist text if provided.
  151. @returns The spinner instance.
  152. */
  153. succeed(text?: string): Ora;
  154. /**
  155. Stop the spinner, change it to a red `` and persist the current text, or `text` if provided.
  156. @param text - Will persist text if provided.
  157. @returns The spinner instance.
  158. */
  159. fail(text?: string): Ora;
  160. /**
  161. Stop the spinner, change it to a yellow `` and persist the current text, or `text` if provided.
  162. @param text - Will persist text if provided.
  163. @returns The spinner instance.
  164. */
  165. warn(text?: string): Ora;
  166. /**
  167. Stop the spinner, change it to a blue `` and persist the current text, or `text` if provided.
  168. @param text - Will persist text if provided.
  169. @returns The spinner instance.
  170. */
  171. info(text?: string): Ora;
  172. /**
  173. Stop the spinner and change the symbol or text.
  174. @returns The spinner instance.
  175. */
  176. stopAndPersist(options?: PersistOptions): Ora;
  177. /**
  178. Clear the spinner.
  179. @returns The spinner instance.
  180. */
  181. clear(): Ora;
  182. /**
  183. Manually render a new frame.
  184. @returns The spinner instance.
  185. */
  186. render(): Ora;
  187. /**
  188. Get a new frame.
  189. @returns The spinner instance text.
  190. */
  191. frame(): string;
  192. }
  193. /**
  194. Elegant terminal spinner.
  195. @param options - If a string is provided, it is treated as a shortcut for `options.text`.
  196. @example
  197. ```
  198. import ora from 'ora';
  199. const spinner = ora('Loading unicorns').start();
  200. setTimeout(() => {
  201. spinner.color = 'yellow';
  202. spinner.text = 'Loading rainbows';
  203. }, 1000);
  204. ```
  205. */
  206. export default function ora(options?: string | Options): Ora;
  207. /**
  208. Starts a spinner for a promise or promise-returning function. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects.
  209. @param action - The promise to start the spinner for or a promise-returning function.
  210. @param options - If a string is provided, it is treated as a shortcut for `options.text`.
  211. @returns The given promise.
  212. @example
  213. ```
  214. import {oraPromise} from 'ora';
  215. await oraPromise(somePromise);
  216. ```
  217. */
  218. export function oraPromise<T>(
  219. action: PromiseLike<T> | ((spinner: Ora) => PromiseLike<T>),
  220. options?: string | PromiseOptions<T>
  221. ): Promise<T>;