import { ToolingLog } from '@osd/dev-utils' ; const SECOND = 1000 ; export class Progress { private total ? : number ; private complete ? : number ; private loggingInterval ? : NodeJS . Timer ; getTotal ( ) { return this . total ; } getComplete ( ) { return this . complete ; } getPercent ( ) { if ( this . complete === undefined || this . total === undefined ) { return 0 ; } return Math . round ( ( this . complete / this . total ) * 100 ) ; } isActive ( ) { return ! ! this . loggingInterval ; } activate ( log ) { if ( this . loggingInterval ) { throw new Error ( 'Progress is already active' ) ; } this . loggingInterval = setInterval ( ( ) => { if ( this . complete === undefined ) { return ; } if ( this . total === undefined ) { log . info ( 'progress: %d' , this . getComplete ( ) ) ; return ; } log . info ( 'progress: %d/%d (%d%)' , this . getComplete ( ) , this . getTotal ( ) , this . getPercent ( ) ) ; } , 10 * SECOND ) ; } deactivate ( ) { if ( ! this . loggingInterval ) { throw new Error ( 'Progress is not active' ) ; } clearInterval ( this . loggingInterval ) ; this . loggingInterval = undefined ; } addToTotal ( n ) { this . total = this . total === undefined ? n : this . total + n ; } addToComplete ( n ) { this . complete = this . complete === undefined ? n : this . complete + n ; } }