import ora from 'ora' ; import chalk from 'chalk' ; import eraseLines from './erase-lines' ; export interface StopSpinner { ( ) : void ; text : string ; } export default function wait ( msg , delay = 300 , _ora = ora ) { let spinner = null ; const timeout = setTimeout ( ( ) => { spinner = _ora ( chalk . gray ( msg ) ) ; spinner . color = 'gray' ; spinner . start ( ) ; } , delay ) ; const stop = ( ) => { clearTimeout ( timeout ) ; if ( spinner ) { spinner . stop ( ) ; spinner = null ; process . stderr . write ( eraseLines ( 1 ) ) ; } } ; stop . text = msg ; Object . defineProperty ( stop , 'text' , { get ( ) { return msg ; } , set ( v ) { msg = v ; if ( spinner ) { spinner . text = chalk . gray ( v ) ; } } , } ) ; process . once ( 'nowExit' , stop ) ; return stop ; }