@pugi/cli 0.1.0-alpha.6 → 0.1.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,294 @@
1
+ /**
2
+ * Update check + install-method detection — Sprint α6.2.
3
+ *
4
+ * Shows the REPL operator a one-shot banner at startup when the npm
5
+ * registry advertises a `@pugi/cli` version newer than what is running.
6
+ * The banner adapts the upgrade hint per install method so the operator
7
+ * sees a copy-pasteable command for their toolchain (brew / npm / curl).
8
+ *
9
+ * Constraints baked into the spec:
10
+ *
11
+ * - **Default-quiet.** Network errors, cache misses, and the
12
+ * `PUGI_SKIP_UPDATE_BANNER=1` env var all return `null` so the
13
+ * REPL renders unchanged.
14
+ * - **24h cache.** The check runs at most once per day, persisted to
15
+ * `~/.pugi/update-check.json`. Repeated REPL launches in the same
16
+ * day skip the registry call entirely.
17
+ * - **3s timeout.** A slow registry never blocks REPL startup; the
18
+ * undici request is aborted after 3 seconds and treated as a
19
+ * silent miss.
20
+ * - **Pure helpers, IO at the edge.** Detection, comparison, cache
21
+ * decode/encode are exported as pure functions with explicit env /
22
+ * home / now / fetch seams so the spec's 8 tests can drive every
23
+ * branch without touching the network or the real filesystem.
24
+ */
25
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
26
+ import { homedir } from 'node:os';
27
+ import { resolve } from 'node:path';
28
+ import { request } from 'undici';
29
+ const REGISTRY_URL = 'https://registry.npmjs.org/@pugi/cli/latest';
30
+ const FETCH_TIMEOUT_MS = 3_000;
31
+ const CACHE_TTL_MS = 24 * 60 * 60 * 1_000;
32
+ /**
33
+ * Pin the install toolchain from the binary path + a small set of env
34
+ * markers. Order matters: the curl installer sets PUGI_VIA_CURL_INSTALL
35
+ * unconditionally, so we honor it first; brew is path-based; npm is the
36
+ * fallback for anything that looks like a node_modules layout; otherwise
37
+ * unknown.
38
+ */
39
+ export function detectInstallMethod(input = {}) {
40
+ const env = input.env ?? process.env;
41
+ const execPath = input.execPath ?? process.execPath;
42
+ if (env.PUGI_VIA_CURL_INSTALL === '1')
43
+ return 'curl';
44
+ // Homebrew on macOS keeps node + bin shims under /opt/homebrew (Apple
45
+ // silicon) or /usr/local/Cellar (Intel). Either suffices to pin brew.
46
+ if (execPath.includes('/opt/homebrew/') ||
47
+ execPath.includes('/usr/local/Cellar/') ||
48
+ execPath.includes('/homebrew/')) {
49
+ return 'brew';
50
+ }
51
+ // npm-global layouts: nvm, fnm, asdf, volta, yarn-global, and the
52
+ // classic `~/.npm-packages` PATH prefix all leave a fingerprint on
53
+ // execPath or PATH. Treat any of these as `npm` so the banner shows
54
+ // the matching `npm install -g` hint.
55
+ const pathEntries = (env.PATH ?? '').split(':');
56
+ const npmFingerprints = [
57
+ '/.nvm/',
58
+ '/.fnm/',
59
+ '/.volta/',
60
+ '/.asdf/',
61
+ '/.npm-packages/',
62
+ '/.config/yarn/global',
63
+ ];
64
+ if (npmFingerprints.some((marker) => execPath.includes(marker)))
65
+ return 'npm';
66
+ if (pathEntries.some((entry) => entry.includes('/.npm-packages') || entry.includes('/.config/yarn/global'))) {
67
+ return 'npm';
68
+ }
69
+ if (execPath.includes('/node_modules/'))
70
+ return 'npm';
71
+ return 'unknown';
72
+ }
73
+ /**
74
+ * Cache path resolver. PUGI_HOME wins (test seam matches the rest of
75
+ * the codebase), else `~/.pugi/update-check.json`.
76
+ */
77
+ export function resolveCachePath(home, env = process.env) {
78
+ const root = env.PUGI_HOME ?? resolve(home ?? homedir(), '.pugi');
79
+ return resolve(root, 'update-check.json');
80
+ }
81
+ export function loadCache(home, env = process.env) {
82
+ const path = resolveCachePath(home, env);
83
+ if (!existsSync(path))
84
+ return null;
85
+ try {
86
+ const text = readFileSync(path, 'utf8');
87
+ const parsed = JSON.parse(text);
88
+ if (typeof parsed.checkedAt !== 'string' || typeof parsed.latestVersion !== 'string') {
89
+ return null;
90
+ }
91
+ return { checkedAt: parsed.checkedAt, latestVersion: parsed.latestVersion };
92
+ }
93
+ catch {
94
+ // Corrupt or unreadable cache — treat as no cache. The next write
95
+ // overwrites the file cleanly.
96
+ return null;
97
+ }
98
+ }
99
+ export function saveCache(record, home, env = process.env) {
100
+ const path = resolveCachePath(home, env);
101
+ const dir = path.slice(0, path.lastIndexOf('/'));
102
+ try {
103
+ if (!existsSync(dir))
104
+ mkdirSync(dir, { recursive: true });
105
+ writeFileSync(path, `${JSON.stringify(record, null, 2)}\n`, { encoding: 'utf8' });
106
+ }
107
+ catch {
108
+ // Best-effort. A read-only home or full disk should not crash REPL
109
+ // startup — we just skip caching this round.
110
+ }
111
+ }
112
+ export function isCacheFresh(record, nowMs) {
113
+ const checkedMs = Date.parse(record.checkedAt);
114
+ if (!Number.isFinite(checkedMs))
115
+ return false;
116
+ return nowMs - checkedMs < CACHE_TTL_MS;
117
+ }
118
+ /**
119
+ * Compare two semver strings (with prerelease support sufficient for
120
+ * `0.1.0-alpha.6` vs `0.1.0-alpha.7` / `0.1.0-beta.1` / `1.0.0`).
121
+ *
122
+ * Returns:
123
+ * -1 if `a < b`
124
+ * 0 if `a === b`
125
+ * 1 if `a > b`
126
+ *
127
+ * Rules follow semver §11:
128
+ * - Major.Minor.Patch compared numerically left-to-right.
129
+ * - A version with a prerelease tag is LOWER than the same version
130
+ * without one (`1.0.0-alpha` < `1.0.0`).
131
+ * - Prerelease identifiers compared dot-by-dot; numeric chunks
132
+ * numerically, mixed chunks ASCII-lexicographic.
133
+ */
134
+ export function compareVersions(a, b) {
135
+ const [coreA, preA] = splitVersion(a);
136
+ const [coreB, preB] = splitVersion(b);
137
+ for (let i = 0; i < 3; i += 1) {
138
+ const da = coreA[i] ?? 0;
139
+ const db = coreB[i] ?? 0;
140
+ if (da < db)
141
+ return -1;
142
+ if (da > db)
143
+ return 1;
144
+ }
145
+ if (preA === null && preB === null)
146
+ return 0;
147
+ if (preA === null)
148
+ return 1;
149
+ if (preB === null)
150
+ return -1;
151
+ return comparePrerelease(preA, preB);
152
+ }
153
+ function splitVersion(v) {
154
+ const dashIdx = v.indexOf('-');
155
+ const coreStr = dashIdx === -1 ? v : v.slice(0, dashIdx);
156
+ const preStr = dashIdx === -1 ? null : v.slice(dashIdx + 1);
157
+ const core = coreStr.split('.').map((n) => {
158
+ const parsed = Number.parseInt(n, 10);
159
+ return Number.isFinite(parsed) ? parsed : 0;
160
+ });
161
+ return [core, preStr];
162
+ }
163
+ function comparePrerelease(a, b) {
164
+ const partsA = a.split('.');
165
+ const partsB = b.split('.');
166
+ const max = Math.max(partsA.length, partsB.length);
167
+ for (let i = 0; i < max; i += 1) {
168
+ const pa = partsA[i];
169
+ const pb = partsB[i];
170
+ if (pa === undefined)
171
+ return -1;
172
+ if (pb === undefined)
173
+ return 1;
174
+ const na = Number.parseInt(pa, 10);
175
+ const nb = Number.parseInt(pb, 10);
176
+ const aIsNum = String(na) === pa;
177
+ const bIsNum = String(nb) === pb;
178
+ if (aIsNum && bIsNum) {
179
+ if (na < nb)
180
+ return -1;
181
+ if (na > nb)
182
+ return 1;
183
+ continue;
184
+ }
185
+ if (aIsNum)
186
+ return -1; // numeric < alphanumeric, per semver §11
187
+ if (bIsNum)
188
+ return 1;
189
+ if (pa < pb)
190
+ return -1;
191
+ if (pa > pb)
192
+ return 1;
193
+ }
194
+ return 0;
195
+ }
196
+ /**
197
+ * One-shot registry GET. Wrapped in a 3s timeout and silently swallows
198
+ * every failure mode — the spec says cache miss / network error = no
199
+ * banner. Returns the `version` string from npm's well-known
200
+ * `/:pkg/latest` document on success, or `null` on any failure.
201
+ */
202
+ export async function fetchLatestVersion(fetcher = request) {
203
+ const controller = new AbortController();
204
+ const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
205
+ try {
206
+ const response = await fetcher(REGISTRY_URL, {
207
+ method: 'GET',
208
+ headers: { accept: 'application/json' },
209
+ bodyTimeout: FETCH_TIMEOUT_MS,
210
+ headersTimeout: FETCH_TIMEOUT_MS,
211
+ signal: controller.signal,
212
+ });
213
+ if (response.statusCode < 200 || response.statusCode >= 300) {
214
+ await response.body.dump();
215
+ return null;
216
+ }
217
+ const text = await response.body.text();
218
+ const parsed = JSON.parse(text);
219
+ if (typeof parsed.version !== 'string' || parsed.version.length === 0)
220
+ return null;
221
+ return parsed.version;
222
+ }
223
+ catch {
224
+ return null;
225
+ }
226
+ finally {
227
+ clearTimeout(timer);
228
+ }
229
+ }
230
+ /**
231
+ * High-level orchestrator wired into the CLI startup path. Returns the
232
+ * banner payload when an update is available AND every silence rule
233
+ * declines to fire, otherwise `null`.
234
+ *
235
+ * Silence rules (any one of these short-circuits to `null`):
236
+ * - `cliSkip === true` (operator passed `--no-update-check`).
237
+ * - `env.PUGI_SKIP_UPDATE_BANNER === '1'`.
238
+ * - `isTty === false` (CI / piped / scripted invocation).
239
+ * - Cache + network both miss — silent skip per spec.
240
+ * - `installed >= latest` — no upgrade to advertise.
241
+ */
242
+ export async function checkForUpdate(options) {
243
+ const env = options.env ?? process.env;
244
+ const now = options.now ?? Date.now;
245
+ const cliSkip = options.cliSkip === true;
246
+ const envSkip = env.PUGI_SKIP_UPDATE_BANNER === '1';
247
+ const isTty = options.isTty ??
248
+ (Boolean(process.stdout.isTTY) &&
249
+ Boolean(process.stdin.isTTY));
250
+ if (cliSkip || envSkip || !isTty)
251
+ return null;
252
+ // Cache-first path. A fresh cache (<24h) bypasses the registry round
253
+ // trip entirely so a daily REPL operator pays one network call per
254
+ // calendar day.
255
+ const cached = loadCache(options.home, env);
256
+ let latest = null;
257
+ if (cached && isCacheFresh(cached, now())) {
258
+ latest = cached.latestVersion;
259
+ }
260
+ else {
261
+ latest = await fetchLatestVersion(options.fetcher);
262
+ if (latest) {
263
+ saveCache({ checkedAt: new Date(now()).toISOString(), latestVersion: latest }, options.home, env);
264
+ }
265
+ }
266
+ if (!latest)
267
+ return null;
268
+ if (compareVersions(options.installed, latest) >= 0)
269
+ return null;
270
+ return {
271
+ installed: options.installed,
272
+ latest,
273
+ method: detectInstallMethod({ env }),
274
+ };
275
+ }
276
+ /**
277
+ * Render the per-method upgrade command. Pure helper so the Ink
278
+ * banner component and any future JSON / log surface share one source
279
+ * of truth on the copy.
280
+ */
281
+ export function upgradeCommand(method) {
282
+ switch (method) {
283
+ case 'brew':
284
+ return 'brew upgrade pugi-io/tap/pugi';
285
+ case 'npm':
286
+ return 'npm install -g @pugi/cli@latest';
287
+ case 'curl':
288
+ return 'curl -fsSL https://install.pugi.io | sh';
289
+ case 'unknown':
290
+ default:
291
+ return 'npm install -g @pugi/cli@latest # or your install method';
292
+ }
293
+ }
294
+ //# sourceMappingURL=update-check.js.map
@@ -10,6 +10,7 @@ const registry = [
10
10
  { name: 'task_get', permission: 'none', risk: 'low', concurrencySafe: true, m1: true },
11
11
  { name: 'task_list', permission: 'none', risk: 'low', concurrencySafe: true, m1: true },
12
12
  { name: 'task_update', permission: 'none', risk: 'low', concurrencySafe: false, m1: true },
13
+ { name: 'web_fetch', permission: 'network', risk: 'medium', concurrencySafe: true, m1: true },
13
14
  { name: 'write', permission: 'edit', risk: 'medium', concurrencySafe: false, m1: true },
14
15
  ];
15
16
  export const toolRegistry = registry.sort((a, b) => a.name.localeCompare(b.name));