2018-06-14 08:09:02 +00:00
const { PerformanceObserver , performance } = require ( 'perf_hooks' ) ;
require ( 'colors' ) ;
2018-07-24 12:29:06 +00:00
const utils = require ( '../utils/utils.js' ) ;
2018-07-20 18:14:52 +00:00
const i18n = require ( '../../core/i18n/i18n.js' ) ;
2018-06-14 08:09:02 +00:00
i18n . setOrDetectLocale ( 'en' ) ;
class NpmTimer {
constructor ( options ) {
2018-06-14 23:37:52 +00:00
this . _logger = ( options . logger && typeof options . logger . info == 'function' ) ? options . logger : console ;
2018-06-14 08:09:02 +00:00
this . _packageName = options . packageName ;
this . _version = options . version ;
this . _showSpinner = options . showSpinner || false ;
this . _spinnerStyle = options . spinnerStyle || 'dots' ;
this . _interval = options . interval || 750 ;
2018-07-20 18:55:20 +00:00
2018-06-14 08:09:02 +00:00
// define mark and measurement names
this . _startMark = 'downloadStart' + this . _packageName + this . _version ;
this . _ongoingMark = 'downloadOngoingMark' + this . _packageName + this . _version ;
this . _downloadOngoing = 'downloadOngoing' + this . _packageName + this . _version ;
this . _endMark = 'downloadEnd' + this . _packageName + this . _version ;
this . _downloadComplete = 'downloadComplete' + this . _packageName + this . _version ;
2018-07-20 18:55:20 +00:00
this . observer . observe ( { entryTypes : [ 'measure' ] } ) ;
2018-06-14 08:09:02 +00:00
}
get observer ( ) {
if ( typeof this . _observer == 'undefined' ) {
this . _observer = new PerformanceObserver ( ( items ) => {
let entry ;
let strDuration ;
2018-07-20 18:55:20 +00:00
2018-06-14 08:09:02 +00:00
// find any download ongoing measurements we've made
2018-07-24 12:29:06 +00:00
entry = utils . last ( items . getEntries ( ) . filter ( entry => entry . name === this . _downloadOngoing ) ) ;
2018-06-14 08:09:02 +00:00
if ( entry ) {
// ongoing performance mark
strDuration = _ _ ( 'Downloading and installing {{packageName}} {{version}}... ({{duration}}ms elapsed)' , { packageName : this . _packageName , version : this . _version , duration : entry . duration } ) ;
if ( this . _spinner ) this . _spinner . text = strDuration ;
}
else {
// otherwise, find our download complete measurement
2018-07-24 12:29:06 +00:00
entry = utils . last ( items . getEntries ( ) . filter ( entry => entry . name === this . _downloadComplete ) ) ;
2018-06-14 08:09:02 +00:00
if ( entry ) {
strDuration = _ _ ( 'Finished downloading and installing {{packageName}} {{version}} in {{duration}}ms' , { packageName : this . _packageName , version : this . _version , duration : entry . duration } ) ;
performance . clearMarks ( ) ;
if ( this . _spinner ) this . _spinner . succeed ( strDuration ) ;
}
}
2018-07-20 18:55:20 +00:00
2018-06-14 08:09:02 +00:00
// log our measurement and make it red if it has taken too long
if ( ! this . _showSpinner && entry && strDuration ) {
if ( entry . duration > 4000 ) {
strDuration = strDuration . red ;
}
2018-06-14 23:37:52 +00:00
this . _logger . info ( strDuration ) ;
2018-06-14 08:09:02 +00:00
}
2018-07-20 18:55:20 +00:00
2018-06-14 08:09:02 +00:00
} ) ;
}
return this . _observer ;
}
2018-07-20 18:55:20 +00:00
2018-06-14 08:09:02 +00:00
start ( ) {
let self = this ;
const strDownloadStart = _ _ ( "Downloading and installing {{packageName}} {{version}}..." , { packageName : this . _packageName , version : this . _version } ) ;
if ( this . _showSpinner ) {
const ora = require ( 'ora' ) ;
this . _spinner = ora ( {
spinner : this . _spinnerStyle ,
text : strDownloadStart
} ) . start ( ) ;
} else {
2018-06-14 23:37:52 +00:00
this . _logger . info ( strDownloadStart ) ;
2018-06-14 08:09:02 +00:00
}
// mark our start time
performance . mark ( this . _startMark ) ;
2018-07-20 18:55:20 +00:00
2018-06-14 08:09:02 +00:00
// function that continually updates the console to show user that we're downloading a library
this . _intOngoingDownload = setInterval (
function ( ) {
performance . mark ( self . _ongoingMark ) ;
performance . measure ( self . _downloadOngoing , self . _startMark , self . _ongoingMark ) ;
} , this . _interval ) ;
}
end ( ) {
// stop updating console for ongoing download
clearInterval ( this . _intOngoingDownload ) ;
performance . mark ( this . _endMark ) ;
performance . measure ( this . _downloadComplete , this . _startMark , this . _endMark ) ;
}
}
module . exports = NpmTimer ;