Skip to content

Commit df5c338

Browse files
christsoclaude
andauthored
feat(cli): skip self-update when already on latest version (#1130)
Check npm registry before running install. If the current version matches the latest published version, print "Already up to date" and exit without running npm/bun install. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8279c34 commit df5c338

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

apps/cli/src/commands/self/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { command, flag, subcommands } from 'cmd-ts';
22
import packageJson from '../../../package.json' with { type: 'json' };
3-
import { detectPackageManager, performSelfUpdate } from '../../self-update.js';
3+
import { detectPackageManager, fetchLatestVersion, performSelfUpdate } from '../../self-update.js';
44

55
// Re-export for existing tests
66
export { detectPackageManagerFromPath } from '../../self-update.js';
@@ -29,6 +29,17 @@ const updateCommand = command({
2929

3030
const currentVersion = packageJson.version;
3131
console.log(`Current version: ${currentVersion}`);
32+
console.log('Checking for updates...');
33+
34+
const latestVersion = await fetchLatestVersion();
35+
if (latestVersion && latestVersion === currentVersion) {
36+
console.log(`Already up to date (${currentVersion}).`);
37+
return;
38+
}
39+
40+
if (latestVersion) {
41+
console.log(`Update available: ${currentVersion}${latestVersion}`);
42+
}
3243
console.log(`Updating agentv using ${pm}...\n`);
3344

3445
const result = await performSelfUpdate({ pm, currentVersion });

apps/cli/src/self-update.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717

1818
import { spawn } from 'node:child_process';
19+
import { get } from 'node:https';
20+
21+
const NPM_REGISTRY_URL = 'https://registry.npmjs.org/agentv/latest';
1922

2023
/**
2124
* Detect package manager from the script path.
@@ -47,6 +50,39 @@ function runCommand(cmd: string, args: string[]): Promise<{ exitCode: number; st
4750
});
4851
}
4952

53+
/**
54+
* Fetch the latest published version of agentv from the npm registry.
55+
* Returns null on network errors or timeouts (best-effort).
56+
*/
57+
export function fetchLatestVersion(): Promise<string | null> {
58+
return new Promise((resolve) => {
59+
const req = get(NPM_REGISTRY_URL, { timeout: 5000 }, (res) => {
60+
if (res.statusCode !== 200) {
61+
res.resume();
62+
resolve(null);
63+
return;
64+
}
65+
let body = '';
66+
res.on('data', (chunk: Buffer) => {
67+
body += chunk.toString();
68+
});
69+
res.on('end', () => {
70+
try {
71+
const version = JSON.parse(body).version;
72+
resolve(typeof version === 'string' ? version : null);
73+
} catch {
74+
resolve(null);
75+
}
76+
});
77+
});
78+
req.on('error', () => resolve(null));
79+
req.on('timeout', () => {
80+
req.destroy();
81+
resolve(null);
82+
});
83+
});
84+
}
85+
5086
function getInstallArgs(pm: 'bun' | 'npm', versionSpec: string): string[] {
5187
const pkg = `agentv@${versionSpec}`;
5288
return pm === 'npm' ? ['install', '-g', pkg] : ['add', '-g', pkg];

0 commit comments

Comments
 (0)