agentic-numerical-control 0.0.0-stage → 0.1.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.
@@ -0,0 +1,69 @@
1
+ // Self-update for installed copies: on start, if npm has a newer version,
2
+ // install it into the same place with the same Node, then relaunch.
3
+ // Development checkouts (a .git folder at the root) never self-update.
4
+ import { execFileSync, spawn } from "node:child_process";
5
+ import fs from "node:fs";
6
+ import path from "node:path";
7
+ import { PRODUCT, ROOT, VERSION } from "./product.js";
8
+ function newer(a, b) {
9
+ const pa = a.split(/[.-]/).map(Number);
10
+ const pb = b.split(/[.-]/).map(Number);
11
+ for (let i = 0; i < 3; i++) {
12
+ if ((pa[i] ?? 0) !== (pb[i] ?? 0))
13
+ return (pa[i] ?? 0) > (pb[i] ?? 0);
14
+ }
15
+ return false;
16
+ }
17
+ /** The npm prefix this copy was installed into: .../node_modules/<name> -> ... */
18
+ function installPrefix() {
19
+ const parts = ROOT.split(path.sep);
20
+ const i = parts.lastIndexOf("node_modules");
21
+ if (i < 0)
22
+ return null;
23
+ let prefix = parts.slice(0, i).join(path.sep);
24
+ if (process.platform !== "win32" && path.basename(prefix) === "lib")
25
+ prefix = path.dirname(prefix);
26
+ return prefix;
27
+ }
28
+ /** npm that ships with the Node running us. */
29
+ function npmCli() {
30
+ const bin = path.dirname(process.execPath);
31
+ return process.platform === "win32"
32
+ ? path.join(bin, "node_modules", "npm", "bin", "npm-cli.js")
33
+ : path.join(bin, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js");
34
+ }
35
+ /** Returns true if a newer version was installed and started in our place. */
36
+ export async function updateIfAvailable() {
37
+ if (fs.existsSync(path.join(ROOT, ".git")))
38
+ return false;
39
+ const prefix = installPrefix();
40
+ const npm = npmCli();
41
+ if (!prefix || !fs.existsSync(npm))
42
+ return false;
43
+ let latest;
44
+ try {
45
+ const res = await fetch(`https://registry.npmjs.org/${PRODUCT.name}/latest`, { signal: AbortSignal.timeout(5000) });
46
+ if (!res.ok)
47
+ return false;
48
+ latest = (await res.json()).version;
49
+ }
50
+ catch {
51
+ console.error(`[update] Could not reach npm to check for updates; staying on ${VERSION}.`);
52
+ return false;
53
+ }
54
+ if (!newer(latest, VERSION))
55
+ return false;
56
+ console.error(`[update] Updating ${VERSION} -> ${latest}`);
57
+ try {
58
+ execFileSync(process.execPath, [npm, "install", "-g", "--prefix", prefix, `${PRODUCT.name}@${latest}`], {
59
+ stdio: "inherit",
60
+ });
61
+ }
62
+ catch (e) {
63
+ console.error(`[update] Update failed (${e.message}); staying on ${VERSION}.`);
64
+ return false;
65
+ }
66
+ // The package was replaced in place; run the new copy with the same arguments.
67
+ spawn(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore", windowsHide: true }).unref();
68
+ return true;
69
+ }