Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the callback option #158

Merged
merged 6 commits into from Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.checkImmediately();

// 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,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 checkImmediately() {
const {distTag} = this.options;
const latest = await latestVersion()(this.packageName, {version: distTag});

Expand Down
17 changes: 11 additions & 6 deletions readme.md
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,17 @@ Default: `latest`

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

### notifier.checkImmediately()

Check update immediately. This method will not trigger the checking process.
LitoMore marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -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,17 @@ test.afterEach(() => {
}, 10000);
});

test('check for update', async t => {
const update = await updateNotifier(generateSettings()).checkNpm();
test('check immediately', async t => {
const update = await updateNotifier(generateSettings()).checkImmediately();
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('check immeditely with dist-tag', async t => {
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).checkImmediately();
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