import { ListrEventType } from '@constants/event.constants' ; import { ListrEvent } from '@interfaces/listr.interface' ; import { ListrRenderer } from '@interfaces/renderer.interface' ; import { Task } from '@lib/task' ; import { Logger } from '@utils/logger' ; import { parseTaskTime } from '@utils/parse-time' ; export class VerboseRenderer implements ListrRenderer { /** designates whether this renderer can output to a non-tty console */ public static nonTTY = true ; /** renderer options for the verbose renderer */ public static rendererOptions : { default useIcons ? : boolean ; default logEmptyTitle ? : boolean ; default logTitleChange ? : boolean ; /**
             * show duration for all tasks
             */ showTimer ? : boolean ; } & { /**
             * inject a custom logger
             */ logger ? : new ( ... args ) => Logger ; /**
             * inject options to custom logger
             */ options ? : any ; } = { useIcons : false , logEmptyTitle : true , logTitleChange : true } ; /** per task options for the verbose renderer */ public static rendererTaskOptions : never ; private logger : Logger ; constructor ( public tasks , public options ) { if ( this . options ?. logger && this . options ?. options ) { this . logger = new this . options . logger ( this . options . options ) ; } else if ( this . options ?. logger ) { this . logger = new this . options . logger ( ) ; } else { this . logger = new Logger ( { useIcons : this . options ?. useIcons } ) ; } this . options = { ... VerboseRenderer . rendererOptions , ... this . options } ; } public render ( ) { this . verboseRenderer ( this . tasks ) ; } public end ( ) { } private verboseRenderer ( tasks ) { return tasks ?. forEach ( ( task ) => { task . subscribe ( ( event ) => { if ( task . isEnabled ( ) ) { const taskTitle = task . hasTitle ( ) ? task . title : 'Task without title.' ; if ( event . type === ListrEventType . SUBTASK && task . hasSubtasks ( ) ) { this . verboseRenderer ( task . subtasks ) ; } else if ( event . type === ListrEventType . STATE ) { if ( this . options ?. logEmptyTitle !== false || task . hasTitle ( ) ) { if ( task . isPending ( ) ) { this . logger . start ( taskTitle ) ; } else if ( task . isCompleted ( ) ) { this . logger . success ( taskTitle + ( this . options ?. showTimer && task . message ?. duration ? ` [${ parseTaskTime ( task . message . duration ) }]` : '' ) ) ; } } } else if ( event . type === ListrEventType . DATA && ! ! event . data ) { this . logger . data ( String ( event . data ) ) ; } else if ( event . type === ListrEventType . TITLE ) { if ( this . options ?. logTitleChange !== false ) { this . logger . title ( String ( event . data ) ) ; } } else if ( event . type === ListrEventType . MESSAGE ) { if ( event . data ?. error ) { this . logger . fail ( String ( event . data . error ) ) ; } else if ( event . data ?. skip ) { this . logger . skip ( String ( event . data . skip ) ) ; } else if ( event . data ?. rollback ) { this . logger . rollback ( String ( event . data . rollback ) ) ; } else if ( event . data ?. retry ) { this . logger . retry ( `[${ event . data . retry . count }] ` + String ( taskTitle ) ) ; } } } } , ( err ) => { this . logger . fail ( err ) ; } ) ; } ) ; } }