Skip to content

Commit

Permalink
Bail if notify is disabled (#110)
Browse files Browse the repository at this point in the history
Avoids initializing configStore and related errors if environment variable or argv disables notifier.
  • Loading branch information
wejendorp authored and SBoudrias committed Jun 6, 2017
1 parent 1055d57 commit d032e12
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, {
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', () => {
Expand Down

0 comments on commit d032e12

Please sign in to comment.