|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import { Command } from 'commander'; |
| 3 | +import https from 'node:https'; |
| 4 | + |
| 5 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 6 | +const pkg = require('../package.json') as { version: string }; |
| 7 | +const LOCAL_VERSION = pkg.version; |
3 | 8 |
|
4 | 9 | import { loginCommand } from './commands/login.js'; |
5 | 10 | import { logoutCommand } from './commands/logout.js'; |
6 | 11 | import { initCommand } from './commands/init.js'; |
7 | 12 | import { pushCommand } from './commands/push.js'; |
8 | 13 | import { addCommand } from './commands/add.js'; |
9 | 14 | import { pullCommand } from './commands/pull.js'; |
| 15 | +import { updateCommand } from './commands/update.js'; |
10 | 16 | import { printCliError, resolveDebugFlag } from './core/cliError.js'; |
11 | 17 |
|
12 | 18 | const program = new Command(); |
13 | 19 |
|
14 | 20 | program |
15 | 21 | .name('ensemble') |
16 | 22 | .description('Ensemble CLI for logging in and configuring Ensemble apps.') |
17 | | - .version('0.1.0') |
| 23 | + .version(LOCAL_VERSION) |
18 | 24 | .option('--debug', 'Print full debug information and stack traces', false); |
19 | 25 |
|
20 | 26 | program |
@@ -91,6 +97,59 @@ program |
91 | 97 | await addCommand(normalizedKind, name); |
92 | 98 | }); |
93 | 99 |
|
| 100 | +program |
| 101 | + .command('update') |
| 102 | + .description('Update the Ensemble CLI to the latest version.') |
| 103 | + .action(async () => { |
| 104 | + await updateCommand(); |
| 105 | + }); |
| 106 | + |
| 107 | +function checkForUpdates(): void { |
| 108 | + const req = https.request( |
| 109 | + 'https://npm.pkg.github.com/@ensembleui%2Fcli', |
| 110 | + { |
| 111 | + method: 'GET', |
| 112 | + headers: { |
| 113 | + Accept: 'application/vnd.npm.install-v1+json', |
| 114 | + }, |
| 115 | + }, |
| 116 | + (res) => { |
| 117 | + if (res.statusCode !== 200) { |
| 118 | + res.resume(); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + let body = ''; |
| 123 | + res.on('data', (chunk) => { |
| 124 | + body += chunk; |
| 125 | + }); |
| 126 | + res.on('end', () => { |
| 127 | + try { |
| 128 | + const data = JSON.parse(body) as { 'dist-tags'?: { latest?: string } }; |
| 129 | + const latest = data['dist-tags']?.latest; |
| 130 | + if (!latest || latest === LOCAL_VERSION) return; |
| 131 | + |
| 132 | + // eslint-disable-next-line no-console |
| 133 | + console.warn( |
| 134 | + `A new version of @ensembleui/cli is available (${LOCAL_VERSION} → ${latest}).\n` + |
| 135 | + `Run "ensemble update" to upgrade.`, |
| 136 | + ); |
| 137 | + } catch { |
| 138 | + // ignore JSON/parse errors |
| 139 | + } |
| 140 | + }); |
| 141 | + }, |
| 142 | + ); |
| 143 | + |
| 144 | + req.on('error', () => { |
| 145 | + // Silently ignore network errors; CLI behavior should not depend on this check. |
| 146 | + }); |
| 147 | + |
| 148 | + req.end(); |
| 149 | +} |
| 150 | + |
| 151 | +checkForUpdates(); |
| 152 | + |
94 | 153 | program.parseAsync(process.argv).catch((err) => { |
95 | 154 | const globalOptions = program.opts<{ debug?: boolean }>(); |
96 | 155 | const debugEnabled = resolveDebugFlag(globalOptions.debug); |
|
0 commit comments