import { Listr } from './listr' ; import { ListrError } from '@interfaces/listr-error.interface' ; import { ListrBaseClassOptions , ListrContext , ListrSubClassOptions , ListrTask } from '@interfaces/listr.interface' ; import { ListrGetRendererClassFromValue , ListrRendererValue } from '@interfaces/renderer.interface' ; /**
 * Creates a new Listr2 task manager.
 *
 * Useful for creating a single instace of Listr2 with pre-set settings.
 */ export class Manager < Ctx = ListrContext , Renderer extends ListrRendererValue = 'default' , FallbackRenderer extends ListrRendererValue = 'verbose' > { public err : ListrError [ ] = [ ] ; private tasks : ListrTask < ListrContext , ListrGetRendererClassFromValue < Renderer > > [ ] = [ ] ; constructor ( public options ? ) { } set ctx ( ctx ) { this . options . ctx = ctx ; } public add < InjectCtx = Ctx > ( tasks , options ? ) { options = { ... this . options , ... options } as ListrSubClassOptions < InjectCtx , Renderer > ; this . tasks = [ ... this . tasks , this . indent < InjectCtx > ( tasks , options ) ] ; } public async runAll < InjectCtx = Ctx > ( options ? ) { options = { ... this . options , ... options } as ListrBaseClassOptions < InjectCtx , Renderer , FallbackRenderer > ; const ctx = await this . run < InjectCtx > ( this . tasks , options ) ; this . tasks = [ ] ; return ctx ; } public newListr < InjectCtx , InjectRenderer extends ListrRendererValue = Renderer , InjectFallbackRenderer extends ListrRendererValue = FallbackRenderer > ( tasks , options ? ) { return new Listr < InjectCtx , InjectRenderer , InjectFallbackRenderer > ( tasks , options ) ; } public indent < InjectCtx = Ctx > ( tasks , options ? , taskOptions ? ) { options = { ... this . options , ... options } as ListrBaseClassOptions < InjectCtx , Renderer , FallbackRenderer > ; let newTask ; if ( typeof tasks === 'function' ) { newTask = { ... taskOptions , task : ( ctx ) : Listr < InjectCtx , Renderer , FallbackRenderer > => this . newListr < InjectCtx , Renderer , FallbackRenderer > ( tasks ( ctx ) , options ) } ; } else { newTask = { ... taskOptions , task : ( ) : Listr < InjectCtx , Renderer , FallbackRenderer > => this . newListr < InjectCtx , Renderer , FallbackRenderer > ( tasks , options ) } ; } return newTask ; } public async run < InjectCtx = Ctx > ( tasks , options ? ) { options = { ... this . options , ... options } as ListrBaseClassOptions < InjectCtx , Renderer , FallbackRenderer > ; const task = this . newListr < InjectCtx , Renderer , FallbackRenderer > ( tasks , options ) ; const ctx = await task . run ( ) ; this . err = task . err ; return ctx ; } public getRuntime ( pipetime ) { return `${ Math . round ( Date . now ( ) - pipetime ) / 1000 }s` ; } }