diff --git a/index.js b/index.js index c12eb3c..38ff01e 100644 --- a/index.js +++ b/index.js @@ -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 { @@ -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; } diff --git a/readme.md b/readme.md index 6910949..ef6a1a8 100644 --- a/readme.md +++ b/readme.md @@ -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`
+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`. diff --git a/test/notify.js b/test/notify.js index ab24d5d..609bb79 100644 --- a/test/notify.js +++ b/test/notify.js @@ -3,33 +3,33 @@ 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; @@ -37,6 +37,7 @@ test.beforeEach(() => { }); test.afterEach(() => { + mock.stopAll(); stderr.release(); errorLogs = ''; }); @@ -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); +});