Skip to content

Commit

Permalink
Remove the callback option (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore authored and sindresorhus committed Dec 12, 2019
1 parent 39682de commit fb5161c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 39 deletions.
2 changes: 1 addition & 1 deletion check.js
Expand Up @@ -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());
Expand Down
18 changes: 2 additions & 16 deletions index.js
Expand Up @@ -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}`, {
Expand All @@ -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') ||
Expand Down Expand Up @@ -113,7 +99,7 @@ class UpdateNotifier {
}).unref();
}

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

Expand Down
17 changes: 11 additions & 6 deletions readme.md
Expand Up @@ -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`\
Expand All @@ -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)*
Expand Down
21 changes: 5 additions & 16 deletions test/update-notifier.js
Expand Up @@ -12,7 +12,6 @@ const generateSettings = (options = {}) => {
name: 'update-notifier-tester',
version: '0.0.2'
},
callback: options.callback,
distTag: options.distTag
};
};
Expand All @@ -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());
Expand Down

0 comments on commit fb5161c

Please sign in to comment.