Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion apps/cli/src/commands/self/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { command, flag, subcommands } from 'cmd-ts';
import packageJson from '../../../package.json' with { type: 'json' };
import { detectPackageManager, performSelfUpdate } from '../../self-update.js';
import { detectPackageManager, fetchLatestVersion, performSelfUpdate } from '../../self-update.js';

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

const currentVersion = packageJson.version;
console.log(`Current version: ${currentVersion}`);
console.log('Checking for updates...');

const latestVersion = await fetchLatestVersion();
if (latestVersion && latestVersion === currentVersion) {
console.log(`Already up to date (${currentVersion}).`);
return;
}

if (latestVersion) {
console.log(`Update available: ${currentVersion} → ${latestVersion}`);
}
console.log(`Updating agentv using ${pm}...\n`);

const result = await performSelfUpdate({ pm, currentVersion });
Expand Down
36 changes: 36 additions & 0 deletions apps/cli/src/self-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/

import { spawn } from 'node:child_process';
import { get } from 'node:https';

const NPM_REGISTRY_URL = 'https://registry.npmjs.org/agentv/latest';

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

/**
* Fetch the latest published version of agentv from the npm registry.
* Returns null on network errors or timeouts (best-effort).
*/
export function fetchLatestVersion(): Promise<string | null> {
return new Promise((resolve) => {
const req = get(NPM_REGISTRY_URL, { timeout: 5000 }, (res) => {
if (res.statusCode !== 200) {
res.resume();
resolve(null);
return;
}
let body = '';
res.on('data', (chunk: Buffer) => {
body += chunk.toString();
});
res.on('end', () => {
try {
const version = JSON.parse(body).version;
resolve(typeof version === 'string' ? version : null);
} catch {
resolve(null);
}
});
});
req.on('error', () => resolve(null));
req.on('timeout', () => {
req.destroy();
resolve(null);
});
});
}

function getInstallArgs(pm: 'bun' | 'npm', versionSpec: string): string[] {
const pkg = `agentv@${versionSpec}`;
return pm === 'npm' ? ['install', '-g', pkg] : ['add', '-g', pkg];
Expand Down
Loading