Browse Source

Check for update using a package

master
Leo Lamprecht 8 years ago
parent
commit
d01bf29690
No known key found for this signature in database GPG Key ID: B08517883D5E0E10
  1. 13
      bin/now
  2. 97
      lib/check-update.js
  3. 3
      package.json

13
bin/now

@ -8,19 +8,26 @@ const {resolve} = require('path')
// Packages
const nodeVersion = require('node-version')
const isAsyncSupported = require('is-async-supported')
const updateNotifier = require('update-notifier')
// Ours
const {error} = require('../lib/error')
const pkg = require('../package')
if (!isAsyncSupported()) {
require('async-to-gen/register')
}
// Ours
const {error} = require('../lib/error')
if (nodeVersion.major < 6) {
error('Now requires at least version 6 of Node. Please upgrade!')
process.exit(1)
}
// Only check for updates in the npm version
if (!process.pkg) {
updateNotifier({pkg}).notify()
}
const defaultCommand = 'deploy'
const commands = new Set([

97
lib/check-update.js

@ -1,97 +0,0 @@
// Packages
const ms = require('ms')
const fetch = require('node-fetch')
const chalk = require('chalk')
const compare = require('semver-compare')
// Ours
const pkg = require('../package')
const isTTY = process.stdout.isTTY
// if we're not in a tty the update checker
// will always return a resolved promise
const resolvedPromise = new Promise(resolve => resolve())
/**
* Configures auto updates.
* Sets up a `exit` listener to report them.
*/
function checkUpdate(opts = {}) {
if (!isTTY) {
// don't attempt to check for updates
// if the user is piping or redirecting
return resolvedPromise
}
let updateData
const update = check(opts).then(data => {
updateData = data
// forces the `exit` event upon Ctrl + C
process.on('SIGINT', () => {
// clean up output after ^C
process.stdout.write('\n')
process.exit(1)
})
}, err => console.error(err.stack))
process.on('exit', () => {
if (updateData) {
const {current, latest, at} = updateData
const ago = ms(Date.now() - at)
console.log(`> ${chalk.white.bgRed('UPDATE NEEDED')} ` +
`Current: ${current}` +
`Latest ${chalk.bold(latest)} (released ${ago} ago)`)
console.log('> Run `npm install -g now` to update')
}
})
return update
}
function check({debug = false}) {
return new Promise(resolve => {
if (debug) {
console.log('> [debug] Checking for updates.')
}
fetch('https://registry.npmjs.org/now').then(res => {
if (res.status !== 200) {
if (debug) {
console.log(`> [debug] Update check error. NPM ${res.status}.`)
}
resolve(false)
return
}
res.json().then(data => {
const {latest} = data['dist-tags']
const current = pkg.version
if (compare(latest, pkg.version) === 1) {
if (debug) {
console.log(`> [debug] Needs update. Current ${current}, latest ${latest}`)
}
resolve({
latest,
current,
at: new Date(data.time[latest])
})
} else {
if (debug) {
console.log(`> [debug] Up to date (${pkg.version}).`)
}
resolve(false)
}
}, () => resolve(false))
}, () => resolve(false))
})
}
module.exports = checkUpdate

3
package.json

@ -89,7 +89,8 @@
"spdy": "^3.4.4",
"split-array": "^1.0.1",
"text-table": "^0.2.0",
"tmp-promise": "^1.0.3"
"tmp-promise": "^1.0.3",
"update-notifier": "^1.0.3"
},
"devDependencies": {
"alpha-sort": "^2.0.0",

Loading…
Cancel
Save