| 8 | } |
| 9 | |
| 10 | export default function wait( |
| 11 | opts: ora.Options, |
| 12 | delay: number = 300 |
| 13 | ): StopSpinner { |
| 14 | let text = opts.text; |
| 15 | let spinner: ora.Ora | null = null; |
| 16 | |
| 17 | if (typeof text !== 'string') { |
| 18 | throw new Error(`"text" is required for Ora spinner`); |
| 19 | } |
| 20 | |
| 21 | const timeout = setTimeout(() => { |
| 22 | spinner = ora(opts); |
| 23 | spinner.text = chalk.gray(text); |
| 24 | spinner.color = 'gray'; |
| 25 | spinner.start(); |
| 26 | }, delay); |
| 27 | |
| 28 | const stop = () => { |
| 29 | clearTimeout(timeout); |
| 30 | if (spinner) { |
| 31 | spinner.stop(); |
| 32 | spinner = null; |
| 33 | process.stderr.write(eraseLines(1)); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | stop.text = text; |
| 38 | |
| 39 | // Allow `text` property to update the text while the spinner is in action |
| 40 | Object.defineProperty(stop, 'text', { |
| 41 | get() { |
| 42 | return text; |
| 43 | }, |
| 44 | |
| 45 | set(v: string) { |
| 46 | text = v; |
| 47 | if (spinner) { |
| 48 | spinner.text = chalk.gray(v); |
| 49 | } |
| 50 | }, |
| 51 | }); |
| 52 | |
| 53 | return stop; |
| 54 | } |