aether-code 0.42.0 → 0.43.1

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.
@@ -1,10 +1,12 @@
1
- // Startup update check — like Claude Code / npm itself nudging you when a newer
2
- // version is published. Never throws, bounded by a short timeout so it can't
3
- // slow or block startup; silent when offline or already current.
1
+ // Startup update check + auto-update. On every REPL launch, checks the npm
2
+ // registry and silently installs the latest version in the background if one
3
+ // exists. Never throws, bounded by a short timeout so it can't slow startup.
4
4
 
5
- import { readFileSync } from "node:fs";
5
+ import { readFileSync, writeFileSync, existsSync } from "node:fs";
6
6
  import { fileURLToPath } from "node:url";
7
7
  import { dirname, join } from "node:path";
8
+ import { tmpdir } from "node:os";
9
+ import { spawn } from "node:child_process";
8
10
  import { c } from "./render.js";
9
11
 
10
12
  export function currentVersion() {
@@ -27,10 +29,65 @@ export function cmpVersion(a, b) {
27
29
  return 0;
28
30
  }
29
31
 
32
+ const COOLDOWN_FILE = join(tmpdir(), "aether-update-ts");
33
+ const COOLDOWN_MS = 3600_000; // 1 hour
34
+
35
+ /**
36
+ * Check for updates. If a newer version exists on npm, spawn a background
37
+ * `npm install -g` to auto-install it. Returns a one-line status message
38
+ * or null. Cooldown: only checks once per hour.
39
+ */
40
+ export async function autoUpdate({ fetchImpl = fetch, timeoutMs = 2000 } = {}) {
41
+ const current = currentVersion();
42
+ if (!current) return null;
43
+
44
+ // Skip if we checked less than 1 hour ago
45
+ try {
46
+ const ts = parseInt(readFileSync(COOLDOWN_FILE, "utf8"), 10);
47
+ if (Date.now() - ts < COOLDOWN_MS) return null;
48
+ } catch { /* no file or parse error — check now */ }
49
+
50
+ try {
51
+ const ac = new AbortController();
52
+ const t = setTimeout(() => ac.abort(), timeoutMs);
53
+ let res;
54
+ try {
55
+ res = await fetchImpl("https://registry.npmjs.org/aether-code/latest", { signal: ac.signal });
56
+ } finally {
57
+ clearTimeout(t);
58
+ }
59
+ if (!res || !res.ok) return null;
60
+ const latest = (await res.json())?.version;
61
+
62
+ // Write cooldown regardless of whether an update is available
63
+ try { writeFileSync(COOLDOWN_FILE, String(Date.now())); } catch { /* ignore */ }
64
+
65
+ if (!latest || cmpVersion(latest, current) <= 0) return null;
66
+
67
+ // Spawn background npm install — detached so it doesn't block the CLI.
68
+ // On Windows, `npm` is a cmd script, so shell:true is required.
69
+ const child = spawn("npm", ["install", "-g", `aether-code@${latest}`], {
70
+ detached: true,
71
+ stdio: "ignore",
72
+ shell: true,
73
+ windowsHide: true,
74
+ });
75
+ child.unref();
76
+
77
+ return (
78
+ c.cyan("↑ auto-updating ") +
79
+ c.gray(current) + c.gray(" → ") + c.bold(c.green(latest)) +
80
+ c.gray(" (background — applies next launch)")
81
+ );
82
+ } catch {
83
+ /* offline / aborted / parse error — stay silent */
84
+ }
85
+ return null;
86
+ }
87
+
30
88
  /**
31
- * Resolve a one-line "update available" nudge if the npm registry has a newer
32
- * version than the installed one — otherwise null. Bounded to ~2s; any failure
33
- * (offline, timeout, registry hiccup) resolves to null silently.
89
+ * Legacy nudge-only check (kept for the test suite). Same as before: returns
90
+ * a one-line "update available" string or null.
34
91
  */
35
92
  export async function checkForUpdate({ fetchImpl = fetch, timeoutMs = 2000 } = {}) {
36
93
  const current = currentVersion();