11const boxen = require ( 'boxen' )
2+ const cacache = require ( 'cacache' )
23const log = require ( 'npmlog' )
4+ const nopt = require ( 'nopt' )
5+ const path = require ( 'path' )
36const pacote = require ( 'pacote' )
47const semver = require ( 'semver' )
5- var npmconf = require ( '../config/core.js' )
6- var configDefs = npmconf . defs
8+
9+ const npm = require ( '../npm.js' )
10+ const npmconf = require ( '../config/core.js' )
11+ const configDefs = npmconf . defs
712const shorthands = configDefs . shorthands
813const types = configDefs . types
9- const nopt = require ( 'nopt' )
10- let npm = require ( '../npm.js' )
11-
1214const pacoteOpts = require ( '../config/pacote' )
1315const unsupported = require ( './unsupported.js' )
1416
@@ -25,23 +27,43 @@ exports.doCheck = function () {
2527
2628 npm . load ( conf , function ( err ) {
2729 if ( err ) return
28- if (
29- npm . config . get ( 'update-notifier' ) &&
30- ! isGlobalNpmUpdate &&
31- ! unsupported . checkVersion ( process . version ) . unsupported &&
32- ! isCI
33- ) {
34- pacote . manifest ( 'npm@latest' , pacoteOpts ( ) )
35- . then ( ( latest ) => {
36- const oldVersion = require ( '../../package.json' ) . version
37- let diffType = semver . diff ( oldVersion , latest . version )
38- if ( diffType ) {
39- console . log ( generateMessage ( oldVersion , latest . version , diffType , npm ) )
40- } else {
41- log . silly ( 'version-check' , 'we are running the latest version of npm' )
42- }
43- } )
44- }
30+
31+ isBeyondCheckInterval ( ) . then ( ( shouldCheck ) => {
32+ if ( shouldCheck ) {
33+ if (
34+ npm . config . get ( 'update-notifier' ) &&
35+ ! isGlobalNpmUpdate &&
36+ ! unsupported . checkVersion ( process . version ) . unsupported &&
37+ ! isCI
38+ ) {
39+ pacote . manifest ( 'npm@latest' , pacoteOpts ( ) )
40+ . then ( ( latest ) => {
41+ const oldVersion = require ( '../../package.json' ) . version
42+ let diffType = semver . diff ( oldVersion , latest . version )
43+ if ( diffType ) {
44+ console . log ( generateMessage ( oldVersion , latest . version , diffType , npm ) )
45+ } else {
46+ log . silly ( 'version-checker' , 'we are running the latest version of npm' )
47+ }
48+ } )
49+ }
50+ }
51+ } )
52+ } )
53+ }
54+
55+ function isBeyondCheckInterval ( ) {
56+ const cache = path . join ( npm . config . get ( 'cache' ) , '_cacache' )
57+ const ONE_DAY = 24 * 60 * 60 * 1000
58+
59+ return cacache . get ( cache , 'update-notifier:last-check' ) . then ( cacheObj => {
60+ const time = Number ( cacheObj . data . toString ( 'utf8' ) )
61+ return ( time + ONE_DAY ) < Date . now ( )
62+ } ) . catch ( ( notFound ) => {
63+ const time = Number ( Date . now ( ) ) . toString ( )
64+ return cacache . put ( cache , 'update-notifier:last-check' , time ) . then ( ( ) => {
65+ return true
66+ } )
4567 } )
4668}
4769
0 commit comments