Skip to content

Commit a40ed02

Browse files
committed
version-checker: use cacache to only check for new versions once per 24 hours
1 parent 9902590 commit a40ed02

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

lib/utils/version-checker.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const boxen = require('boxen')
2+
const cacache = require('cacache')
23
const log = require('npmlog')
4+
const nopt = require('nopt')
5+
const path = require('path')
36
const pacote = require('pacote')
47
const 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
712
const shorthands = configDefs.shorthands
813
const types = configDefs.types
9-
const nopt = require('nopt')
10-
let npm = require('../npm.js')
11-
1214
const pacoteOpts = require('../config/pacote')
1315
const 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

Comments
 (0)