diff --git a/check.js b/check.js index 11dba33..fc0ee9c 100644 --- a/check.js +++ b/check.js @@ -10,7 +10,7 @@ updateNotifier = new updateNotifier.UpdateNotifier(options); // Exit process when offline setTimeout(process.exit, 1000 * 30); - const update = await updateNotifier.checkNpm(); + const update = await updateNotifier.fetchInfo(); // Only update the last update check time on success updateNotifier.config.set('lastUpdateCheck', Date.now()); diff --git a/index.js b/index.js index 4c5973d..456de03 100644 --- a/index.js +++ b/index.js @@ -38,15 +38,13 @@ 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.env.NODE_ENV === 'test' || 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}`, { @@ -71,18 +69,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') || @@ -113,7 +99,7 @@ class UpdateNotifier { }).unref(); } - async checkNpm() { + async fetchInfo() { const {distTag} = this.options; const latest = await latestVersion()(this.packageName, {version: distTag}); diff --git a/readme.md b/readme.md index 3e5b756..56edf3e 100644 --- a/readme.md +++ b/readme.md @@ -107,12 +107,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`\ @@ -127,6 +121,17 @@ Default: `'latest'` Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version. +### notifier.fetchInfo() + +Check update information. + +Returns an `object` with: + +- `latest` _(String)_ - Latest version. +- `current` _(String)_ - Current version. +- `type` _(String)_ - Type of current update. Possible values: `latest`, `major`, `minor`, `patch`, `prerelease`, `build`. +- `name` _(String)_ - Package name. + ### notifier.notify(options?) Convenience method to display a notification message. *(See screenshot)* diff --git a/test/update-notifier.js b/test/update-notifier.js index 704e6e1..08a284e 100644 --- a/test/update-notifier.js +++ b/test/update-notifier.js @@ -12,7 +12,6 @@ const generateSettings = (options = {}) => { name: 'update-notifier-tester', version: '0.0.2' }, - callback: options.callback, distTag: options.distTag }; }; @@ -36,27 +35,17 @@ test.afterEach(() => { }, 10000); }); -test('check for update', async t => { - const update = await updateNotifier(generateSettings()).checkNpm(); +test('fetch info', async t => { + const update = await updateNotifier(generateSettings()).fetchInfo(); + console.log(update); 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('fetch info with dist-tag', async t => { + const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).fetchInfo(); 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());