11#!/usr/bin/env node
22
33import fs from 'fs/promises'
4- import hyperparamPackage from '../package.json' with { type : 'json' }
4+ import packageJson from '../package.json' with { type : 'json' }
55import { chat } from './chat.js'
66import { serve } from './serve.js'
77
8+ const updateCheck = checkForUpdates ( )
9+
810const arg = process . argv [ 2 ]
911if ( arg === 'chat' ) {
12+ await updateCheck // wait for update check to finish before chat
1013 chat ( )
1114} else if ( arg === '--help' || arg === '-H' || arg === '-h' ) {
1215 console . log ( 'Usage:' )
@@ -17,7 +20,7 @@ if (arg === 'chat') {
1720 console . log ( ' hyperparam -h, --help, give this help list' )
1821 console . log ( ' hyperparam -v, --version print program version' )
1922} else if ( arg === '--version' || arg === '-V' || arg === '-v' ) {
20- console . log ( hyperparamPackage . version )
23+ console . log ( packageJson . version )
2124} else if ( ! arg ) {
2225 serve ( process . cwd ( ) , undefined ) // current directory
2326} else if ( arg . match ( / ^ h t t p s ? : \/ \/ / ) ) {
@@ -38,3 +41,22 @@ if (arg === 'chat') {
3841 process . exit ( 1 )
3942 } )
4043}
44+
45+ /**
46+ * Check for updates and notify user if a newer version is available.
47+ * Runs in the background.
48+ * @returns {Promise<void> }
49+ */
50+ function checkForUpdates ( ) {
51+ const currentVersion = packageJson . version
52+ return fetch ( 'https://registry.npmjs.org/hyperparam/latest' )
53+ . then ( response => response . json ( ) )
54+ . then ( data => {
55+ const latestVersion = data . version
56+ if ( latestVersion && latestVersion !== currentVersion ) {
57+ console . log ( `\x1b[33mA newer version of hyperparam is available: ${ latestVersion } (current: ${ currentVersion } )\x1b[0m` )
58+ console . log ( '\x1b[33mRun \'npm install -g hyperparam\' to update\x1b[0m' )
59+ }
60+ } )
61+ . catch ( ( ) => { } ) // ignore errors
62+ }
0 commit comments