Skip to content

Commit

Permalink
Add ability to bypass isNpm check with shouldNotifyInNpmScript opti…
Browse files Browse the repository at this point in the history
…on (#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
  • Loading branch information
alexccl authored and SBoudrias committed Apr 14, 2018
1 parent edbe3d2 commit ac0d3cb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -40,6 +40,7 @@ class UpdateNotifier {
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env ||
process.argv.indexOf('--no-update-notifier') !== -1 ||
isCi();
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;

if (!this.disabled && !this.hasCallback) {
try {
Expand Down Expand Up @@ -108,7 +109,8 @@ class UpdateNotifier {
});
}
notify(opts) {
if (!process.stdout.isTTY || isNpm() || !this.update) {
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm();
if (!process.stdout.isTTY || suppressForNpm || !this.update) {
return this;
}

Expand Down
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -153,6 +153,13 @@ Default: `{padding: 1, margin: 1, align: 'center', borderColor: 'yellow', border

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

##### shouldNotifyInNpmScript

Type: `boolean`<br>
Default: `false`

Allows notification to be shown when running as an npm script.

### User settings

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`.
Expand Down
45 changes: 33 additions & 12 deletions test/notify.js
Expand Up @@ -3,40 +3,41 @@ import clearModule from 'clear-module';
import FixtureStdout from 'fixture-stdout';
import stripAnsi from 'strip-ansi';
import test from 'ava';
import mock from 'mock-require';

const stderr = new FixtureStdout({
stream: process.stderr
});
let updateNotifier = require('..');

test.before(() => {
['.', 'is-npm'].forEach(clearModule);
['npm_config_username', 'npm_package_name', 'npm_config_heading'].forEach(name => {
delete process.env[name];
});
process.stdout.isTTY = true;
updateNotifier = require('..');
});

function Control() {
function Control(shouldNotifyInNpmScript) {
this.packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
};
this.shouldNotifyInNpmScript = shouldNotifyInNpmScript;
}
util.inherits(Control, updateNotifier.UpdateNotifier);

const setupTest = isNpmReturnValue => {
['.', 'is-npm'].forEach(clearModule);
process.stdout.isTTY = true;
mock('is-npm', isNpmReturnValue || false);
const updateNotifier = require('..');
util.inherits(Control, updateNotifier.UpdateNotifier);
};

let errorLogs = '';

test.beforeEach(() => {
setupTest();
stderr.capture(s => {
errorLogs += s;
return false;
});
});

test.afterEach(() => {
mock.stopAll();
stderr.release();
errorLogs = '';
});
Expand All @@ -62,3 +63,23 @@ test('exclude -g argument when `isGlobal` option is `false`', t => {
notifier.notify({defer: false, isGlobal: false});
t.not(stripAnsi(errorLogs).indexOf('Run npm i update-notifier-tester to update'), -1);
});

test('shouldNotifyInNpmScript should default to false', t => {
const notifier = new Control();
notifier.notify({defer: false});
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
});

test('suppress output when running as npm script', t => {
setupTest(true);
const notifier = new Control();
notifier.notify({defer: false});
t.is(stripAnsi(errorLogs).indexOf('Update available'), -1);
});

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

0 comments on commit ac0d3cb

Please sign in to comment.