UNPKG

@agentled/cli

Version:

CLI for Agentled — manage workflows, apps, and knowledge from the command line. Zero context-window cost for AI agents.

77 lines 3.03 kB
/** * Best-effort "new version available" check against the npm registry. * * Compares the locally-installed @agentled/cli version (from package.json) * against the latest published on npm and prints a one-line warning if the * user is behind. Never blocks setup on failure — a flaky network or * registry should not prevent first-time onboarding. */ import { readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const REGISTRY_URL = 'https://registry.npmjs.org/@agentled/cli/latest'; const TIMEOUT_MS = 3000; /** * Read the version field from this package's own package.json. Returns * '0.0.0-dev' as a fallback so a missing/unreadable file doesn't crash setup. */ export function getInstalledCliVersion() { try { const here = fileURLToPath(import.meta.url); // dist/utils/version-check.js → package root is two levels up const pkgRoot = resolve(dirname(here), '..', '..'); const pkg = JSON.parse(readFileSync(resolve(pkgRoot, 'package.json'), 'utf-8')); return typeof pkg.version === 'string' ? pkg.version : '0.0.0-dev'; } catch { return '0.0.0-dev'; } } /** Compare two semver-ish strings: -1, 0, 1. Prerelease suffixes are ignored. */ function compareVersions(a, b) { const parse = (v) => v.replace(/^v/, '').split('-')[0].split('.').map(n => parseInt(n, 10) || 0); const [a1, a2 = 0, a3 = 0] = parse(a); const [b1, b2 = 0, b3 = 0] = parse(b); if (a1 !== b1) return a1 < b1 ? -1 : 1; if (a2 !== b2) return a2 < b2 ? -1 : 1; if (a3 !== b3) return a3 < b3 ? -1 : 1; return 0; } export async function checkCliVersion() { const installed = getInstalledCliVersion(); const ac = new AbortController(); const timer = setTimeout(() => ac.abort(), TIMEOUT_MS); try { const res = await fetch(REGISTRY_URL, { signal: ac.signal }); if (!res.ok) { return { installed, latest: null, outdated: false, error: `HTTP ${res.status}` }; } const data = (await res.json()); const latest = typeof data.version === 'string' ? data.version : null; if (!latest) { return { installed, latest: null, outdated: false, error: 'No version field in registry response' }; } return { installed, latest, outdated: compareVersions(installed, latest) < 0, }; } catch (err) { return { installed, latest: null, outdated: false, error: err?.message ?? String(err) }; } finally { clearTimeout(timer); } } /** One-line summary suitable for the setup banner. Returns null if there is nothing to say. */ export function summarizeVersionCheck(r) { if (r.outdated && r.latest) { return `New @agentled/cli version available: v${r.installed} → v${r.latest}. Update with: npm install -g @agentled/cli@latest`; } return null; } //# sourceMappingURL=version-check.js.map