update-notifier
Advanced tools
Comparing version 2.5.0 to 3.0.0
14
check.js
@@ -9,3 +9,8 @@ /* eslint-disable unicorn/no-process-exit */ | ||
updateNotifier.checkNpm().then(update => { | ||
(async () => { | ||
// Exit process when offline | ||
setTimeout(process.exit, 1000 * 30); | ||
const update = await updateNotifier.checkNpm(); | ||
// Only update the last update check time on success | ||
@@ -18,7 +23,8 @@ updateNotifier.config.set('lastUpdateCheck', Date.now()); | ||
// Call process exit explicitly to terminate the child process | ||
// Otherwise the child process will run forever, according to the Node.js docs | ||
// Call process exit explicitly to terminate the child process, | ||
// otherwise the child process will run forever, according to the Node.js docs | ||
process.exit(); | ||
}).catch(() => { | ||
})().catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
82
index.js
'use strict'; | ||
const spawn = require('child_process').spawn; | ||
const {spawn} = require('child_process'); | ||
const path = require('path'); | ||
const format = require('util').format; | ||
const {format} = require('util'); | ||
const importLazy = require('import-lazy')(require); | ||
@@ -13,12 +13,15 @@ | ||
const isInstalledGlobally = importLazy('is-installed-globally'); | ||
const isYarnGlobal = importLazy('is-yarn-global'); | ||
const hasYarn = importLazy('has-yarn'); | ||
const boxen = importLazy('boxen'); | ||
const xdgBasedir = importLazy('xdg-basedir'); | ||
const isCi = importLazy('is-ci'); | ||
const ONE_DAY = 1000 * 60 * 60 * 24; | ||
class UpdateNotifier { | ||
constructor(options) { | ||
options = options || {}; | ||
constructor(options = {}) { | ||
this.options = options; | ||
options.pkg = options.pkg || {}; | ||
options.distTag = options.distTag || 'latest'; | ||
@@ -42,3 +45,3 @@ // Reduce pkg to the essential keys. with fallback to deprecated options | ||
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env || | ||
process.argv.indexOf('--no-update-notifier') !== -1 || | ||
process.argv.includes('--no-update-notifier') || | ||
isCi(); | ||
@@ -56,5 +59,5 @@ this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript; | ||
}); | ||
} catch (err) { | ||
} catch (error) { | ||
// Expecting error code EACCES or EPERM | ||
const msg = | ||
const message = | ||
chalk().yellow(format(' %s update check failed ', options.pkg.name)) + | ||
@@ -66,3 +69,3 @@ format('\n Try running with %s or get access ', chalk().cyan('sudo')) + | ||
process.on('exit', () => { | ||
console.error('\n' + boxen()(msg, {align: 'center'})); | ||
console.error('\n' + boxen()(message, {align: 'center'})); | ||
}); | ||
@@ -72,7 +75,13 @@ } | ||
} | ||
check() { | ||
if (this.hasCallback) { | ||
this.checkNpm() | ||
.then(update => this.callback(null, update)) | ||
.catch(err => this.callback(err)); | ||
(async () => { | ||
try { | ||
this.callback(null, await this.checkNpm()); | ||
} catch (error) { | ||
this.callback(error); | ||
} | ||
})(); | ||
return; | ||
@@ -106,14 +115,17 @@ } | ||
} | ||
checkNpm() { | ||
return latestVersion()(this.packageName).then(latestVersion => { | ||
return { | ||
latest: latestVersion, | ||
current: this.packageVersion, | ||
type: semverDiff()(this.packageVersion, latestVersion) || 'latest', | ||
name: this.packageName | ||
}; | ||
}); | ||
async checkNpm() { | ||
const {distTag} = this.options; | ||
const latest = await latestVersion()(this.packageName, {version: distTag}); | ||
return { | ||
latest, | ||
current: this.packageVersion, | ||
type: semverDiff()(this.packageVersion, latest) || distTag, | ||
name: this.packageName | ||
}; | ||
} | ||
notify(opts) { | ||
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm(); | ||
notify(options) { | ||
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm().isNpm; | ||
if (!process.stdout.isTTY || suppressForNpm || !this.update) { | ||
@@ -123,8 +135,22 @@ return this; | ||
opts = Object.assign({isGlobal: isInstalledGlobally()}, opts); | ||
options = { | ||
isGlobal: isInstalledGlobally(), | ||
isYarnGlobal: isYarnGlobal()(), | ||
...options | ||
}; | ||
opts.message = opts.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') + | ||
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan('npm i ' + (opts.isGlobal ? '-g ' : '') + this.packageName) + ' to update'; | ||
let installCommand; | ||
opts.boxenOpts = opts.boxenOpts || { | ||
if (options.isYarnGlobal) { | ||
installCommand = `yarn global add ${this.packageName}`; | ||
} else if (hasYarn()()) { | ||
installCommand = `yarn add ${this.packageName}`; | ||
} else { | ||
installCommand = `npm i ${options.isGlobal ? '-g ' : ''}${this.packageName}`; | ||
} | ||
options.message = options.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') + | ||
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan(installCommand) + ' to update'; | ||
options.boxenOpts = options.boxenOpts || { | ||
padding: 1, | ||
@@ -137,5 +163,5 @@ margin: 1, | ||
const message = '\n' + boxen()(opts.message, opts.boxenOpts); | ||
const message = '\n' + boxen()(options.message, options.boxenOpts); | ||
if (opts.defer === false) { | ||
if (options.defer === false) { | ||
console.error(message); | ||
@@ -142,0 +168,0 @@ } else { |
108
package.json
{ | ||
"name": "update-notifier", | ||
"version": "2.5.0", | ||
"description": "Update notifications for your CLI app", | ||
"license": "BSD-2-Clause", | ||
"repository": "yeoman/update-notifier", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava --timeout=20s" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"check.js" | ||
], | ||
"keywords": [ | ||
"npm", | ||
"update", | ||
"updater", | ||
"notify", | ||
"notifier", | ||
"check", | ||
"checker", | ||
"cli", | ||
"module", | ||
"package", | ||
"version" | ||
], | ||
"dependencies": { | ||
"boxen": "^1.2.1", | ||
"chalk": "^2.0.1", | ||
"configstore": "^3.0.0", | ||
"import-lazy": "^2.1.0", | ||
"is-ci": "^1.0.10", | ||
"is-installed-globally": "^0.1.0", | ||
"is-npm": "^1.0.0", | ||
"latest-version": "^3.0.0", | ||
"semver-diff": "^2.0.0", | ||
"xdg-basedir": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"clear-module": "^2.1.0", | ||
"fixture-stdout": "^0.2.1", | ||
"mock-require": "^2.0.2", | ||
"strip-ansi": "^4.0.0", | ||
"xo": "^0.18.2" | ||
} | ||
"name": "update-notifier", | ||
"version": "3.0.0", | ||
"description": "Update notifications for your CLI app", | ||
"license": "BSD-2-Clause", | ||
"repository": "yeoman/update-notifier", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava --timeout=20s -s" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"check.js" | ||
], | ||
"keywords": [ | ||
"npm", | ||
"update", | ||
"updater", | ||
"notify", | ||
"notifier", | ||
"check", | ||
"checker", | ||
"cli", | ||
"module", | ||
"package", | ||
"version" | ||
], | ||
"dependencies": { | ||
"boxen": "^3.0.0", | ||
"chalk": "^2.0.1", | ||
"configstore": "^4.0.0", | ||
"has-yarn": "^2.1.0", | ||
"import-lazy": "^2.1.0", | ||
"is-ci": "^2.0.0", | ||
"is-installed-globally": "^0.1.0", | ||
"is-npm": "^3.0.0", | ||
"is-yarn-global": "^0.3.0", | ||
"latest-version": "^5.0.0", | ||
"semver-diff": "^2.0.0", | ||
"xdg-basedir": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"clear-module": "^3.1.0", | ||
"fixture-stdout": "^0.2.1", | ||
"mock-require": "^3.0.3", | ||
"strip-ansi": "^5.2.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -83,2 +83,17 @@ # update-notifier [![Build Status](https://travis-ci.org/yeoman/update-notifier.svg?branch=master)](https://travis-ci.org/yeoman/update-notifier) | ||
--- | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-update_notifier?utm_source=npm-update-notifier&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> | ||
--- | ||
## API | ||
@@ -88,3 +103,3 @@ | ||
Checks if there is an available update. Accepts options defined below. Returns an instance with an `.update` property there is an available update, otherwise `undefined`. | ||
Checks if there is an available update. Accepts options defined below. Returns an instance with an `.update` property if there is an available update, otherwise `undefined`. | ||
@@ -120,2 +135,16 @@ ### options | ||
#### shouldNotifyInNpmScript | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Allows notification to be shown when running as an npm script. | ||
#### distTag | ||
Type: `string`<br> | ||
Default: `latest` | ||
Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version. | ||
### notifier.notify([options]) | ||
@@ -148,3 +177,3 @@ | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Default: auto-detect | ||
@@ -160,9 +189,3 @@ Include the `-g` argument in the default message's `npm i` recommendation. You may want to change this if your CLI package can be installed as a dependency of another project, and don't want to recommend a global installation. This option is ignored if you supply your own `message` (see above). | ||
##### shouldNotifyInNpmScript | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Allows notification to be shown when running as an npm script. | ||
### User settings | ||
@@ -193,7 +216,12 @@ | ||
[And 1600+ more…](https://www.npmjs.org/browse/depended/update-notifier) | ||
[And 2700+ more…](https://www.npmjs.org/browse/depended/update-notifier) | ||
## Security | ||
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. | ||
## License | ||
BSD-2-Clause © Google |
14864
173
222
12
+ Addedhas-yarn@^2.1.0
+ Addedis-yarn-global@^0.3.0
+ Added@sindresorhus/is@0.14.0(transitive)
+ Added@szmarczak/http-timer@1.1.2(transitive)
+ Addedansi-align@3.0.1(transitive)
+ Addedansi-regex@4.1.15.0.1(transitive)
+ Addedboxen@3.2.0(transitive)
+ Addedcacheable-request@6.1.0(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedci-info@2.0.0(transitive)
+ Addedcli-boxes@2.2.1(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addedconfigstore@4.0.0(transitive)
+ Addeddecompress-response@3.3.0(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addedemoji-regex@7.0.38.0.0(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedget-stream@4.1.05.2.0(transitive)
+ Addedgot@9.6.0(transitive)
+ Addedhas-yarn@2.1.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedis-ci@2.0.0(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedis-npm@3.0.0(transitive)
+ Addedis-yarn-global@0.3.0(transitive)
+ Addedjson-buffer@3.0.0(transitive)
+ Addedkeyv@3.1.0(transitive)
+ Addedlatest-version@5.1.0(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addedmimic-response@1.0.1(transitive)
+ Addednormalize-url@4.5.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@1.1.0(transitive)
+ Addedpackage-json@6.5.0(transitive)
+ Addedprepend-http@2.0.0(transitive)
+ Addedpump@3.0.0(transitive)
+ Addedregistry-auth-token@4.2.2(transitive)
+ Addedregistry-url@5.1.0(transitive)
+ Addedresponselike@1.0.2(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedstring-width@3.1.04.2.3(transitive)
+ Addedstrip-ansi@5.2.06.0.1(transitive)
+ Addedto-readable-stream@1.0.0(transitive)
+ Addedtype-fest@0.3.1(transitive)
+ Addedurl-parse-lax@3.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedansi-align@2.0.0(transitive)
- Removedboxen@1.3.0(transitive)
- Removedcamelcase@4.1.0(transitive)
- Removedcapture-stack-trace@1.0.2(transitive)
- Removedci-info@1.6.0(transitive)
- Removedcli-boxes@1.0.0(transitive)
- Removedconfigstore@3.1.5(transitive)
- Removedcreate-error-class@3.0.2(transitive)
- Removedgot@6.7.1(transitive)
- Removedis-ci@1.2.1(transitive)
- Removedis-npm@1.0.0(transitive)
- Removedis-redirect@1.0.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedlatest-version@3.1.0(transitive)
- Removedpackage-json@4.0.1(transitive)
- Removedprepend-http@1.0.4(transitive)
- Removedregistry-auth-token@3.4.0(transitive)
- Removedregistry-url@3.1.0(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedtimed-out@4.0.1(transitive)
- Removedunzip-response@2.0.1(transitive)
- Removedurl-parse-lax@1.0.0(transitive)
Updatedboxen@^3.0.0
Updatedconfigstore@^4.0.0
Updatedis-ci@^2.0.0
Updatedis-npm@^3.0.0
Updatedlatest-version@^5.0.0