Skip to content

Commit

Permalink
version-checker: use cacache to only check for new versions once per …
Browse files Browse the repository at this point in the history
…24 hours
  • Loading branch information
olore committed Sep 1, 2018
1 parent 9902590 commit a40ed02
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions lib/utils/version-checker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const boxen = require('boxen')
const cacache = require('cacache')
const log = require('npmlog')
const nopt = require('nopt')
const path = require('path')
const pacote = require('pacote')
const semver = require('semver')
var npmconf = require('../config/core.js')
var configDefs = npmconf.defs

const npm = require('../npm.js')
const npmconf = require('../config/core.js')
const configDefs = npmconf.defs
const shorthands = configDefs.shorthands
const types = configDefs.types
const nopt = require('nopt')
let npm = require('../npm.js')

const pacoteOpts = require('../config/pacote')
const unsupported = require('./unsupported.js')

Expand All @@ -25,23 +27,43 @@ exports.doCheck = function () {

npm.load(conf, function (err) {
if (err) return
if (
npm.config.get('update-notifier') &&
!isGlobalNpmUpdate &&
!unsupported.checkVersion(process.version).unsupported &&
!isCI
) {
pacote.manifest('npm@latest', pacoteOpts())
.then((latest) => {
const oldVersion = require('../../package.json').version
let diffType = semver.diff(oldVersion, latest.version)
if (diffType) {
console.log(generateMessage(oldVersion, latest.version, diffType, npm))
} else {
log.silly('version-check', 'we are running the latest version of npm')
}
})
}

isBeyondCheckInterval().then((shouldCheck) => {
if (shouldCheck) {
if (
npm.config.get('update-notifier') &&
!isGlobalNpmUpdate &&
!unsupported.checkVersion(process.version).unsupported &&
!isCI
) {
pacote.manifest('npm@latest', pacoteOpts())
.then((latest) => {
const oldVersion = require('../../package.json').version
let diffType = semver.diff(oldVersion, latest.version)
if (diffType) {
console.log(generateMessage(oldVersion, latest.version, diffType, npm))
} else {
log.silly('version-checker', 'we are running the latest version of npm')
}
})
}
}
})
})
}

function isBeyondCheckInterval () {
const cache = path.join(npm.config.get('cache'), '_cacache')
const ONE_DAY = 24 * 60 * 60 * 1000

return cacache.get(cache, 'update-notifier:last-check').then(cacheObj => {
const time = Number(cacheObj.data.toString('utf8'))
return (time + ONE_DAY) < Date.now()
}).catch((notFound) => {
const time = Number(Date.now()).toString()
return cacache.put(cache, 'update-notifier:last-check', time).then(() => {
return true
})
})
}

Expand Down

0 comments on commit a40ed02

Please sign in to comment.