11'use strict' ;
2- const spawn = require ( 'child_process' ) . spawn ;
2+ const { spawn} = require ( 'child_process' ) ;
33const path = require ( 'path' ) ;
4- const format = require ( 'util' ) . format ;
4+ const { format} = require ( 'util' ) ;
55const importLazy = require ( 'import-lazy' ) ( require ) ;
66
77const configstore = importLazy ( 'configstore' ) ;
@@ -13,11 +13,11 @@ const isInstalledGlobally = importLazy('is-installed-globally');
1313const boxen = importLazy ( 'boxen' ) ;
1414const xdgBasedir = importLazy ( 'xdg-basedir' ) ;
1515const isCi = importLazy ( 'is-ci' ) ;
16+
1617const ONE_DAY = 1000 * 60 * 60 * 24 ;
1718
1819class UpdateNotifier {
19- constructor ( options ) {
20- options = options || { } ;
20+ constructor ( options = { } ) {
2121 this . options = options ;
2222 options . pkg = options . pkg || { } ;
2323
@@ -38,7 +38,7 @@ class UpdateNotifier {
3838 this . hasCallback = typeof options . callback === 'function' ;
3939 this . callback = options . callback || ( ( ) => { } ) ;
4040 this . disabled = 'NO_UPDATE_NOTIFIER' in process . env ||
41- process . argv . indexOf ( '--no-update-notifier' ) !== - 1 ||
41+ process . argv . includes ( '--no-update-notifier' ) ||
4242 isCi ( ) ;
4343 this . shouldNotifyInNpmScript = options . shouldNotifyInNpmScript ;
4444
@@ -51,25 +51,31 @@ class UpdateNotifier {
5151 // after the set interval, so not to bother users right away
5252 lastUpdateCheck : Date . now ( )
5353 } ) ;
54- } catch ( err ) {
54+ } catch ( error ) {
5555 // Expecting error code EACCES or EPERM
56- const msg =
56+ const message =
5757 chalk ( ) . yellow ( format ( ' %s update check failed ' , options . pkg . name ) ) +
5858 format ( '\n Try running with %s or get access ' , chalk ( ) . cyan ( 'sudo' ) ) +
5959 '\n to the local update config store via \n' +
6060 chalk ( ) . cyan ( format ( ' sudo chown -R $USER:$(id -gn $USER) %s ' , xdgBasedir ( ) . config ) ) ;
6161
6262 process . on ( 'exit' , ( ) => {
63- console . error ( '\n' + boxen ( ) ( msg , { align : 'center' } ) ) ;
63+ console . error ( '\n' + boxen ( ) ( message , { align : 'center' } ) ) ;
6464 } ) ;
6565 }
6666 }
6767 }
68+
6869 check ( ) {
6970 if ( this . hasCallback ) {
70- this . checkNpm ( )
71- . then ( update => this . callback ( null , update ) )
72- . catch ( err => this . callback ( err ) ) ;
71+ ( async ( ) => {
72+ try {
73+ this . callback ( null , await this . checkNpm ( ) ) ;
74+ } catch ( error ) {
75+ this . callback ( error ) ;
76+ }
77+ } ) ( ) ;
78+
7379 return ;
7480 }
7581
@@ -98,38 +104,43 @@ class UpdateNotifier {
98104 stdio : 'ignore'
99105 } ) . unref ( ) ;
100106 }
101- checkNpm ( ) {
102- return latestVersion ( ) ( this . packageName ) . then ( latestVersion => {
103- return {
104- latest : latestVersion ,
105- current : this . packageVersion ,
106- type : semverDiff ( ) ( this . packageVersion , latestVersion ) || 'latest' ,
107- name : this . packageName
108- } ;
109- } ) ;
107+
108+ async checkNpm ( ) {
109+ const latest = await latestVersion ( ) ( this . packageName ) ;
110+
111+ return {
112+ latest,
113+ current : this . packageVersion ,
114+ type : semverDiff ( ) ( this . packageVersion , latest ) || 'latest' ,
115+ name : this . packageName
116+ } ;
110117 }
111- notify ( opts ) {
112- const suppressForNpm = ! this . shouldNotifyInNpmScript && isNpm ( ) ;
118+
119+ notify ( options ) {
120+ const suppressForNpm = ! this . shouldNotifyInNpmScript && isNpm ( ) . isNpm ;
113121 if ( ! process . stdout . isTTY || suppressForNpm || ! this . update ) {
114122 return this ;
115123 }
116124
117- opts = Object . assign ( { isGlobal : isInstalledGlobally ( ) } , opts ) ;
125+ options = {
126+ isGlobal : isInstalledGlobally ( ) ,
127+ ...options
128+ } ;
118129
119- opts . message = opts . message || 'Update available ' + chalk ( ) . dim ( this . update . current ) + chalk ( ) . reset ( ' → ' ) +
120- chalk ( ) . green ( this . update . latest ) + ' \nRun ' + chalk ( ) . cyan ( 'npm i ' + ( opts . isGlobal ? '-g ' : '' ) + this . packageName ) + ' to update' ;
130+ options . message = options . message || 'Update available ' + chalk ( ) . dim ( this . update . current ) + chalk ( ) . reset ( ' → ' ) +
131+ chalk ( ) . green ( this . update . latest ) + ' \nRun ' + chalk ( ) . cyan ( 'npm i ' + ( options . isGlobal ? '-g ' : '' ) + this . packageName ) + ' to update' ;
121132
122- opts . boxenOpts = opts . boxenOpts || {
133+ options . boxenOpts = options . boxenOpts || {
123134 padding : 1 ,
124135 margin : 1 ,
125136 align : 'center' ,
126137 borderColor : 'yellow' ,
127138 borderStyle : 'round'
128139 } ;
129140
130- const message = '\n' + boxen ( ) ( opts . message , opts . boxenOpts ) ;
141+ const message = '\n' + boxen ( ) ( options . message , options . boxenOpts ) ;
131142
132- if ( opts . defer === false ) {
143+ if ( options . defer === false ) {
133144 console . error ( message ) ;
134145 } else {
135146 process . on ( 'exit' , ( ) => {
0 commit comments