Skip to content

Commit ac0d3cb

Browse files
alexcclSBoudrias
authored andcommittedApr 14, 2018
Add ability to bypass isNpm check with shouldNotifyInNpmScript option (#127)
* Added ability to bypass isNpm with 'shouldNotifyInNpmScript' option * Updated readme with option * Fixed grammatical error in readme * Rename skipIsNpmCheck to shouldNotifyInNpmScript * Refactored test to use renamed shouldNotifyInNpmScript property
1 parent edbe3d2 commit ac0d3cb

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed
 

‎index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class UpdateNotifier {
4040
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env ||
4141
process.argv.indexOf('--no-update-notifier') !== -1 ||
4242
isCi();
43+
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;
4344

4445
if (!this.disabled && !this.hasCallback) {
4546
try {
@@ -108,7 +109,8 @@ class UpdateNotifier {
108109
});
109110
}
110111
notify(opts) {
111-
if (!process.stdout.isTTY || isNpm() || !this.update) {
112+
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm();
113+
if (!process.stdout.isTTY || suppressForNpm || !this.update) {
112114
return this;
113115
}
114116

‎readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ Default: `{padding: 1, margin: 1, align: 'center', borderColor: 'yellow', border
153153

154154
Options object that will be passed to [`boxen`](https://github.com/sindresorhus/boxen).
155155

156+
##### shouldNotifyInNpmScript
157+
158+
Type: `boolean`<br>
159+
Default: `false`
160+
161+
Allows notification to be shown when running as an npm script.
162+
156163
### User settings
157164

158165
Users of your module have the ability to opt-out of the update notifier by changing the `optOut` property to `true` in `~/.config/configstore/update-notifier-[your-module-name].json`. The path is available in `notifier.config.path`.

‎test/notify.js

+33-12
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@ import clearModule from 'clear-module';
33
import FixtureStdout from 'fixture-stdout';
44
import stripAnsi from 'strip-ansi';
55
import test from 'ava';
6+
import mock from 'mock-require';
67

78
const stderr = new FixtureStdout({
89
stream: process.stderr
910
});
10-
let updateNotifier = require('..');
1111

12-
test.before(() => {
13-
['.', 'is-npm'].forEach(clearModule);
14-
['npm_config_username', 'npm_package_name', 'npm_config_heading'].forEach(name => {
15-
delete process.env[name];
16-
});
17-
process.stdout.isTTY = true;
18-
updateNotifier = require('..');
19-
});
20-
21-
function Control() {
12+
function Control(shouldNotifyInNpmScript) {
2213
this.packageName = 'update-notifier-tester';
2314
this.update = {
2415
current: '0.0.2',
2516
latest: '1.0.0'
2617
};
18+
this.shouldNotifyInNpmScript = shouldNotifyInNpmScript;
2719
}
28-
util.inherits(Control, updateNotifier.UpdateNotifier);
20+
21+
const setupTest = isNpmReturnValue => {
22+
['.', 'is-npm'].forEach(clearModule);
23+
process.stdout.isTTY = true;
24+
mock('is-npm', isNpmReturnValue || false);
25+
const updateNotifier = require('..');
26+
util.inherits(Control, updateNotifier.UpdateNotifier);
27+
};
2928

3029
let errorLogs = '';
3130

3231
test.beforeEach(() => {
32+
setupTest();
3333
stderr.capture(s => {
3434
errorLogs += s;
3535
return false;
3636
});
3737
});
3838

3939
test.afterEach(() => {
40+
mock.stopAll();
4041
stderr.release();
4142
errorLogs = '';
4243
});
@@ -62,3 +63,23 @@ test('exclude -g argument when `isGlobal` option is `false`', t => {
6263
notifier.notify({defer: false, isGlobal: false});
6364
t.not(stripAnsi(errorLogs).indexOf('Run npm i update-notifier-tester to update'), -1);
6465
});
66+
67+
test('shouldNotifyInNpmScript should default to false', t => {
68+
const notifier = new Control();
69+
notifier.notify({defer: false});
70+
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
71+
});
72+
73+
test('suppress output when running as npm script', t => {
74+
setupTest(true);
75+
const notifier = new Control();
76+
notifier.notify({defer: false});
77+
t.is(stripAnsi(errorLogs).indexOf('Update available'), -1);
78+
});
79+
80+
test('should ouput if running as npm script and shouldNotifyInNpmScript option set', t => {
81+
setupTest(true);
82+
const notifier = new Control(true);
83+
notifier.notify({defer: false});
84+
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
85+
});

0 commit comments

Comments
 (0)
Please sign in to comment.