Skip to content

Commit aafd8a0

Browse files
committed
Require Node.js 8
1 parent 0d49f51 commit aafd8a0

9 files changed

+111
-100
lines changed

.gitattributes

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
3-
- '6'
4-
- '4'
3+
- '10'
4+
- '8'

check.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ const options = JSON.parse(process.argv[2]);
66

77
updateNotifier = new updateNotifier.UpdateNotifier(options);
88

9-
updateNotifier.checkNpm().then(update => {
9+
(async () => {
10+
const update = await updateNotifier.checkNpm();
11+
1012
// Only update the last update check time on success
1113
updateNotifier.config.set('lastUpdateCheck', Date.now());
1214

1315
if (update.type && update.type !== 'latest') {
1416
updateNotifier.config.set('update', update);
1517
}
1618

17-
// Call process exit explicitly to terminate the child process
18-
// Otherwise the child process will run forever, according to the Node.js docs
19+
// Call process exit explicitly to terminate the child process,
20+
// otherwise the child process will run forever, according to the Node.js docs
1921
process.exit();
20-
}).catch(() => {
22+
})().catch(error => {
23+
console.error(error);
2124
process.exit(1);
2225
});

index.js

+39-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2-
const spawn = require('child_process').spawn;
2+
const {spawn} = require('child_process');
33
const path = require('path');
4-
const format = require('util').format;
4+
const {format} = require('util');
55
const importLazy = require('import-lazy')(require);
66

77
const configstore = importLazy('configstore');
@@ -13,11 +13,11 @@ const isInstalledGlobally = importLazy('is-installed-globally');
1313
const boxen = importLazy('boxen');
1414
const xdgBasedir = importLazy('xdg-basedir');
1515
const isCi = importLazy('is-ci');
16+
1617
const ONE_DAY = 1000 * 60 * 60 * 24;
1718

1819
class UpdateNotifier {
19-
constructor(options) {
20-
options = options || {};
20+
constructor(options = {}) {
2121
this.options = options;
2222
options.pkg = options.pkg || {};
2323

@@ -38,7 +38,7 @@ class UpdateNotifier {
3838
this.hasCallback = typeof options.callback === 'function';
3939
this.callback = options.callback || (() => {});
4040
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env ||
41-
process.argv.indexOf('--no-update-notifier') !== -1 ||
41+
process.argv.includes('--no-update-notifier') ||
4242
isCi();
4343
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;
4444

@@ -51,25 +51,31 @@ class UpdateNotifier {
5151
// after the set interval, so not to bother users right away
5252
lastUpdateCheck: Date.now()
5353
});
54-
} catch (err) {
54+
} catch (error) {
5555
// Expecting error code EACCES or EPERM
56-
const msg =
56+
const message =
5757
chalk().yellow(format(' %s update check failed ', options.pkg.name)) +
5858
format('\n Try running with %s or get access ', chalk().cyan('sudo')) +
5959
'\n to the local update config store via \n' +
6060
chalk().cyan(format(' sudo chown -R $USER:$(id -gn $USER) %s ', xdgBasedir().config));
6161

6262
process.on('exit', () => {
63-
console.error('\n' + boxen()(msg, {align: 'center'}));
63+
console.error('\n' + boxen()(message, {align: 'center'}));
6464
});
6565
}
6666
}
6767
}
68+
6869
check() {
6970
if (this.hasCallback) {
70-
this.checkNpm()
71-
.then(update => this.callback(null, update))
72-
.catch(err => this.callback(err));
71+
(async () => {
72+
try {
73+
this.callback(null, await this.checkNpm());
74+
} catch (error) {
75+
this.callback(error);
76+
}
77+
})();
78+
7379
return;
7480
}
7581

@@ -98,38 +104,43 @@ class UpdateNotifier {
98104
stdio: 'ignore'
99105
}).unref();
100106
}
101-
checkNpm() {
102-
return latestVersion()(this.packageName).then(latestVersion => {
103-
return {
104-
latest: latestVersion,
105-
current: this.packageVersion,
106-
type: semverDiff()(this.packageVersion, latestVersion) || 'latest',
107-
name: this.packageName
108-
};
109-
});
107+
108+
async checkNpm() {
109+
const latest = await latestVersion()(this.packageName);
110+
111+
return {
112+
latest,
113+
current: this.packageVersion,
114+
type: semverDiff()(this.packageVersion, latest) || 'latest',
115+
name: this.packageName
116+
};
110117
}
111-
notify(opts) {
112-
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm();
118+
119+
notify(options) {
120+
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm().isNpm;
113121
if (!process.stdout.isTTY || suppressForNpm || !this.update) {
114122
return this;
115123
}
116124

117-
opts = Object.assign({isGlobal: isInstalledGlobally()}, opts);
125+
options = {
126+
isGlobal: isInstalledGlobally(),
127+
...options
128+
};
118129

119-
opts.message = opts.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') +
120-
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan('npm i ' + (opts.isGlobal ? '-g ' : '') + this.packageName) + ' to update';
130+
options.message = options.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') +
131+
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan('npm i ' + (options.isGlobal ? '-g ' : '') + this.packageName) + ' to update';
121132

122-
opts.boxenOpts = opts.boxenOpts || {
133+
options.boxenOpts = options.boxenOpts || {
123134
padding: 1,
124135
margin: 1,
125136
align: 'center',
126137
borderColor: 'yellow',
127138
borderStyle: 'round'
128139
};
129140

130-
const message = '\n' + boxen()(opts.message, opts.boxenOpts);
141+
const message = '\n' + boxen()(options.message, options.boxenOpts);
131142

132-
if (opts.defer === false) {
143+
if (options.defer === false) {
133144
console.error(message);
134145
} else {
135146
process.on('exit', () => {

package.json

+53-53
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
{
2-
"name": "update-notifier",
3-
"version": "2.5.0",
4-
"description": "Update notifications for your CLI app",
5-
"license": "BSD-2-Clause",
6-
"repository": "yeoman/update-notifier",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "https://sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=4"
14-
},
15-
"scripts": {
16-
"test": "xo && ava --timeout=20s"
17-
},
18-
"files": [
19-
"index.js",
20-
"check.js"
21-
],
22-
"keywords": [
23-
"npm",
24-
"update",
25-
"updater",
26-
"notify",
27-
"notifier",
28-
"check",
29-
"checker",
30-
"cli",
31-
"module",
32-
"package",
33-
"version"
34-
],
35-
"dependencies": {
36-
"boxen": "^1.2.1",
37-
"chalk": "^2.0.1",
38-
"configstore": "^3.0.0",
39-
"import-lazy": "^2.1.0",
40-
"is-ci": "^1.0.10",
41-
"is-installed-globally": "^0.1.0",
42-
"is-npm": "^1.0.0",
43-
"latest-version": "^3.0.0",
44-
"semver-diff": "^2.0.0",
45-
"xdg-basedir": "^3.0.0"
46-
},
47-
"devDependencies": {
48-
"ava": "*",
49-
"clear-module": "^2.1.0",
50-
"fixture-stdout": "^0.2.1",
51-
"mock-require": "^2.0.2",
52-
"strip-ansi": "^4.0.0",
53-
"xo": "^0.18.2"
54-
}
2+
"name": "update-notifier",
3+
"version": "2.5.0",
4+
"description": "Update notifications for your CLI app",
5+
"license": "BSD-2-Clause",
6+
"repository": "yeoman/update-notifier",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "sindresorhus@gmail.com",
10+
"url": "https://sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava --timeout=20s"
17+
},
18+
"files": [
19+
"index.js",
20+
"check.js"
21+
],
22+
"keywords": [
23+
"npm",
24+
"update",
25+
"updater",
26+
"notify",
27+
"notifier",
28+
"check",
29+
"checker",
30+
"cli",
31+
"module",
32+
"package",
33+
"version"
34+
],
35+
"dependencies": {
36+
"boxen": "^3.0.0",
37+
"chalk": "^2.0.1",
38+
"configstore": "^4.0.0",
39+
"import-lazy": "^2.1.0",
40+
"is-ci": "^2.0.0",
41+
"is-installed-globally": "^0.1.0",
42+
"is-npm": "^3.0.0",
43+
"latest-version": "^5.0.0",
44+
"semver-diff": "^2.0.0",
45+
"xdg-basedir": "^3.0.0"
46+
},
47+
"devDependencies": {
48+
"ava": "^1.3.1",
49+
"clear-module": "^3.1.0",
50+
"fixture-stdout": "^0.2.1",
51+
"mock-require": "^3.0.3",
52+
"strip-ansi": "^5.2.0",
53+
"xo": "^0.24.0"
54+
}
5555
}

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ The first time the user runs your app, it will check for an update, and even if
9999

100100
### notifier = updateNotifier(options)
101101

102-
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`.
102+
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`.
103103

104104
### options
105105

@@ -201,7 +201,7 @@ There are a bunch projects using it:
201201
- [Pageres](https://github.com/sindresorhus/pageres) - Capture website screenshots
202202
- [Node GH](http://nodegh.io) - GitHub command line tool
203203

204-
[And 1600+ more…](https://www.npmjs.org/browse/depended/update-notifier)
204+
[And 2700+ more…](https://www.npmjs.org/browse/depended/update-notifier)
205205

206206

207207
## Security

test/fs-error.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import test from 'ava';
44
let updateNotifier;
55

66
test.before(() => {
7-
['.', 'configstore', 'xdg-basedir'].forEach(clearModule);
8-
// Set configstore.config to something
9-
// that requires root access
7+
['..', 'configstore', 'xdg-basedir'].forEach(clearModule);
8+
// Set configstore.config to something that requires root access
109
process.env.XDG_CONFIG_HOME = '/usr';
1110
updateNotifier = require('..');
1211
});

test/notify.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Control(shouldNotifyInNpmScript) {
1919
}
2020

2121
const setupTest = isNpmReturnValue => {
22-
['.', 'is-npm'].forEach(clearModule);
22+
['..', 'is-npm'].forEach(clearModule);
2323
process.stdout.isTTY = true;
2424
mock('is-npm', isNpmReturnValue || false);
2525
const updateNotifier = require('..');
@@ -74,12 +74,12 @@ test('suppress output when running as npm script', t => {
7474
setupTest(true);
7575
const notifier = new Control();
7676
notifier.notify({defer: false});
77-
t.is(stripAnsi(errorLogs).indexOf('Update available'), -1);
77+
t.false(stripAnsi(errorLogs).includes('Update available'));
7878
});
7979

8080
test('should ouput if running as npm script and shouldNotifyInNpmScript option set', t => {
8181
setupTest(true);
8282
const notifier = new Control(true);
8383
notifier.notify({defer: false});
84-
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
84+
t.true(stripAnsi(errorLogs).includes('Update available'));
8585
});

test/update-notifier.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ mockRequire('is-ci', false);
77
// eslint-disable-next-line import/first
88
import updateNotifier from '..';
99

10-
const generateSettings = options => {
11-
options = options || {};
10+
const generateSettings = (options = {}) => {
1211
return {
1312
pkg: {
1413
name: 'update-notifier-tester',
1514
version: '0.0.2'
1615
},
17-
callback: options.callback || null
16+
callback: options.callback
1817
};
1918
};
2019

0 commit comments

Comments
 (0)