From d032e123748fffc6b56ef2c880f6fe21e6013169 Mon Sep 17 00:00:00 2001 From: Jacob Wejendorp Date: Tue, 6 Jun 2017 17:45:57 +0200 Subject: [PATCH] Bail if notify is disabled (#110) Avoids initializing configStore and related errors if environment variable or argv disables notifier. --- index.js | 7 ++++--- test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2be8c23..a5fff01 100644 --- a/index.js +++ b/index.js @@ -35,8 +35,10 @@ class UpdateNotifier { this.updateCheckInterval = typeof options.updateCheckInterval === 'number' ? options.updateCheckInterval : ONE_DAY; this.hasCallback = typeof options.callback === 'function'; this.callback = options.callback || (() => {}); + this.disabled = 'NO_UPDATE_NOTIFIER' in process.env || + process.argv.indexOf('--no-update-notifier') !== -1; - if (!this.hasCallback) { + if (!this.disabled && !this.hasCallback) { try { const ConfigStore = configstore(); this.config = new ConfigStore(`update-notifier-${this.packageName}`, { @@ -70,8 +72,7 @@ class UpdateNotifier { if ( !this.config || this.config.get('optOut') || - 'NO_UPDATE_NOTIFIER' in process.env || - process.argv.indexOf('--no-update-notifier') !== -1 + this.disabled ) { return; } diff --git a/test.js b/test.js index 774a458..b939dfb 100644 --- a/test.js +++ b/test.js @@ -20,13 +20,17 @@ describe('updateNotifier', () => { }; }; + let argv; let configstorePath; beforeEach(() => { + argv = process.argv.slice(); configstorePath = updateNotifier(generateSettings()).config.path; }); afterEach(() => { + delete process.env.NO_UPDATE_NOTIFIER; + process.argv = argv; setTimeout(() => { fs.unlinkSync(configstorePath); }, 10000); @@ -43,6 +47,18 @@ describe('updateNotifier', () => { callback: cb })); }); + + it('should not initialize configStore when NO_UPDATE_NOTIFIER is set', () => { + process.env.NO_UPDATE_NOTIFIER = '1'; + const notifier = updateNotifier(generateSettings()); + assert.equal(notifier.config, undefined); + }); + + it('should not initialize configStore when --no-update-notifier is set', () => { + process.argv.push('--no-update-notifier'); + const notifier = updateNotifier(generateSettings()); + assert.equal(notifier.config, undefined); + }); }); describe('updateNotifier with fs error', () => {