Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions check.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ updateNotifier = new updateNotifier.UpdateNotifier(options);
// Exit process when offline
setTimeout(process.exit, 1000 * 30);

const update = await updateNotifier.checkNpm();
const diff = await updateNotifier.getSemverDiff();

// Only update the last update check time on success
updateNotifier.config.set('lastUpdateCheck', Date.now());

if (update.type && update.type !== 'latest') {
updateNotifier.config.set('update', update);
if (diff.type && diff.type !== 'latest') {
updateNotifier.config.set('update', diff);
}

// Call process exit explicitly to terminate the child process,
Expand Down
18 changes: 2 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ class UpdateNotifier {
this.packageName = options.pkg.name;
this.packageVersion = options.pkg.version;
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.includes('--no-update-notifier') ||
isCi();
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;

if (!this.disabled && !this.hasCallback) {
if (!this.disabled) {
try {
const ConfigStore = configstore();
this.config = new ConfigStore(`update-notifier-${this.packageName}`, {
Expand All @@ -70,18 +68,6 @@ class UpdateNotifier {
}

check() {
if (this.hasCallback) {
(async () => {
try {
this.callback(null, await this.checkNpm());
} catch (error) {
this.callback(error);
}
})();

return;
}

if (
!this.config ||
this.config.get('optOut') ||
Expand All @@ -108,7 +94,7 @@ class UpdateNotifier {
}).unref();
}

async checkNpm() {
async getSemverDiff() {
const {distTag} = this.options;
const latest = await latestVersion()(this.packageName, {version: distTag});

Expand Down
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ Default: `1000 * 60 * 60 * 24` *(1 day)*

How often to check for updates.

#### callback(error, update)

Type: `Function`

Passing a callback here will make it check for an update directly and report right away. Not recommended as you won't get the benefits explained in [`How`](#how). `update` is equal to `notifier.update`.

#### shouldNotifyInNpmScript

Type: `boolean`<br>
Expand All @@ -144,6 +138,10 @@ Default: `latest`

Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version.

### notifier.getSemverDiff()

Get semver difference directly. This method will not trigger the checking process.

### notifier.notify([options])

Convenience method to display a notification message. *(See screenshot)*
Expand Down
20 changes: 4 additions & 16 deletions test/update-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const generateSettings = (options = {}) => {
name: 'update-notifier-tester',
version: '0.0.2'
},
callback: options.callback,
distTag: options.distTag
};
};
Expand All @@ -34,27 +33,16 @@ test.afterEach(() => {
}, 10000);
});

test('check for update', async t => {
const update = await updateNotifier(generateSettings()).checkNpm();
test('get semver diff', async t => {
const update = await updateNotifier(generateSettings()).getSemverDiff();
t.is(update.latest, '0.0.2');
});

test('check for update with dist-tag', async t => {
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).checkNpm();
test('get semver diff with dist-tag', async t => {
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).getSemverDiff();
t.is(update.latest, '0.0.3-rc1');
});

test.cb('check for update with callback', t => {
t.plan(1);

updateNotifier(generateSettings({
callback: () => {
t.pass();
t.end();
}
}));
});

test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
process.env.NO_UPDATE_NOTIFIER = '1';
const notifier = updateNotifier(generateSettings());
Expand Down