Skip to content

Commit 80e2fcf

Browse files
committed
feat: Add update checker to notify users of new versions
1 parent 349d7ab commit 80e2fcf

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

bin/cli.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const sorter = require('../lib/sorter');
1111
const aggregator = require('../lib/aggregator');
1212
const projectDetector = require('../lib/project-detector');
1313
const pricing = require('../lib/pricing');
14+
const updateChecker = require('../lib/update-checker');
1415
const { version } = require('../package.json');
1516

1617
program
@@ -49,6 +50,9 @@ if (process.argv.slice(2).length === 0) {
4950
}
5051

5152
async function showUsage(options) {
53+
// Check for updates
54+
await updateChecker.checkForUpdates();
55+
5256
try {
5357
const { messages } = await usage.getUsage();
5458

@@ -220,6 +224,9 @@ async function showUsage(options) {
220224
}
221225

222226
async function showProjects() {
227+
// Check for updates
228+
await updateChecker.checkForUpdates();
229+
223230
const spinner = createSpinner('Fetching project list...').start();
224231
try {
225232
const { projects, messageCount } = await usage.getProjects();
@@ -241,6 +248,9 @@ async function showProjects() {
241248
}
242249

243250
async function showModels() {
251+
// Check for updates
252+
await updateChecker.checkForUpdates();
253+
244254
const spinner = createSpinner('Fetching model pricing data...').start();
245255
try {
246256
const pricingData = await pricing.fetchModelPricing();

lib/update-checker.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
const https = require('https');
3+
const chalk = require('chalk');
4+
const { createSpinner } = require('nanospinner');
5+
const { name, version } = require('../package.json');
6+
7+
const NPM_REGISTRY_URL = `https://registry.npmjs.org/${name}`;
8+
9+
function fetchLatestVersion() {
10+
return new Promise((resolve, reject) => {
11+
https.get(NPM_REGISTRY_URL, (res) => {
12+
let data = '';
13+
res.on('data', (chunk) => {
14+
data += chunk;
15+
});
16+
res.on('end', () => {
17+
try {
18+
const json = JSON.parse(data);
19+
resolve(json['dist-tags'].latest);
20+
} catch (e) {
21+
reject(e);
22+
}
23+
});
24+
}).on('error', (e) => {
25+
reject(e);
26+
});
27+
});
28+
}
29+
30+
async function checkForUpdates() {
31+
const spinner = createSpinner('Checking for updates...').start();
32+
33+
try {
34+
const latestVersion = await fetchLatestVersion();
35+
36+
if (version !== latestVersion) {
37+
spinner.warn({ text: `New version available! Run: ${chalk.bgYellow.black.bold(` npm install -g ${name} `)}` });
38+
console.log('');
39+
} else {
40+
spinner.success({ text: `You're using the latest version (${version})` });
41+
}
42+
} catch (e) {
43+
spinner.stop();
44+
// Silently ignore errors - we don't want to interrupt the main functionality
45+
}
46+
}
47+
48+
module.exports = { checkForUpdates };
49+

0 commit comments

Comments
 (0)