File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ const sorter = require('../lib/sorter');
1111const aggregator = require ( '../lib/aggregator' ) ;
1212const projectDetector = require ( '../lib/project-detector' ) ;
1313const pricing = require ( '../lib/pricing' ) ;
14+ const updateChecker = require ( '../lib/update-checker' ) ;
1415const { version } = require ( '../package.json' ) ;
1516
1617program
@@ -49,6 +50,9 @@ if (process.argv.slice(2).length === 0) {
4950}
5051
5152async 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
222226async 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
243250async 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 ( ) ;
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments