Skip to content

Commit fb5161c

Browse files
LitoMoresindresorhus
authored andcommitted
Remove the callback option (sindresorhus#158)
1 parent 39682de commit fb5161c

File tree

4 files changed

+19
-39
lines changed

4 files changed

+19
-39
lines changed

check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ updateNotifier = new updateNotifier.UpdateNotifier(options);
1010
// Exit process when offline
1111
setTimeout(process.exit, 1000 * 30);
1212

13-
const update = await updateNotifier.checkNpm();
13+
const update = await updateNotifier.fetchInfo();
1414

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

index.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ class UpdateNotifier {
3838
this.packageName = options.pkg.name;
3939
this.packageVersion = options.pkg.version;
4040
this.updateCheckInterval = typeof options.updateCheckInterval === 'number' ? options.updateCheckInterval : ONE_DAY;
41-
this.hasCallback = typeof options.callback === 'function';
42-
this.callback = options.callback || (() => {});
4341
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env ||
4442
process.env.NODE_ENV === 'test' ||
4543
process.argv.includes('--no-update-notifier') ||
4644
isCi();
4745
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;
4846

49-
if (!this.disabled && !this.hasCallback) {
47+
if (!this.disabled) {
5048
try {
5149
const ConfigStore = configstore();
5250
this.config = new ConfigStore(`update-notifier-${this.packageName}`, {
@@ -71,18 +69,6 @@ class UpdateNotifier {
7169
}
7270

7371
check() {
74-
if (this.hasCallback) {
75-
(async () => {
76-
try {
77-
this.callback(null, await this.checkNpm());
78-
} catch (error) {
79-
this.callback(error);
80-
}
81-
})();
82-
83-
return;
84-
}
85-
8672
if (
8773
!this.config ||
8874
this.config.get('optOut') ||
@@ -113,7 +99,7 @@ class UpdateNotifier {
11399
}).unref();
114100
}
115101

116-
async checkNpm() {
102+
async fetchInfo() {
117103
const {distTag} = this.options;
118104
const latest = await latestVersion()(this.packageName, {version: distTag});
119105

readme.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ Default: `1000 * 60 * 60 * 24` *(1 day)*
107107

108108
How often to check for updates.
109109

110-
#### callback(error, update)
111-
112-
Type: `Function`
113-
114-
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`.
115-
116110
#### shouldNotifyInNpmScript
117111

118112
Type: `boolean`\
@@ -127,6 +121,17 @@ Default: `'latest'`
127121

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

124+
### notifier.fetchInfo()
125+
126+
Check update information.
127+
128+
Returns an `object` with:
129+
130+
- `latest` _(String)_ - Latest version.
131+
- `current` _(String)_ - Current version.
132+
- `type` _(String)_ - Type of current update. Possible values: `latest`, `major`, `minor`, `patch`, `prerelease`, `build`.
133+
- `name` _(String)_ - Package name.
134+
130135
### notifier.notify(options?)
131136

132137
Convenience method to display a notification message. *(See screenshot)*

test/update-notifier.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const generateSettings = (options = {}) => {
1212
name: 'update-notifier-tester',
1313
version: '0.0.2'
1414
},
15-
callback: options.callback,
1615
distTag: options.distTag
1716
};
1817
};
@@ -36,27 +35,17 @@ test.afterEach(() => {
3635
}, 10000);
3736
});
3837

39-
test('check for update', async t => {
40-
const update = await updateNotifier(generateSettings()).checkNpm();
38+
test('fetch info', async t => {
39+
const update = await updateNotifier(generateSettings()).fetchInfo();
40+
console.log(update);
4141
t.is(update.latest, '0.0.2');
4242
});
4343

44-
test('check for update with dist-tag', async t => {
45-
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).checkNpm();
44+
test('fetch info with dist-tag', async t => {
45+
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).fetchInfo();
4646
t.is(update.latest, '0.0.3-rc1');
4747
});
4848

49-
test.cb('check for update with callback', t => {
50-
t.plan(1);
51-
52-
updateNotifier(generateSettings({
53-
callback: () => {
54-
t.pass();
55-
t.end();
56-
}
57-
}));
58-
});
59-
6049
test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
6150
process.env.NO_UPDATE_NOTIFIER = '1';
6251
const notifier = updateNotifier(generateSettings());

0 commit comments

Comments
 (0)