@evandu/super-pi 0.1.66 → 0.1.68

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.
package/bin/super-pi.js CHANGED
@@ -37,6 +37,15 @@ function openBrowser(url) {
37
37
  else spawn("xdg-open", [url], { detached: true, stdio: "ignore" }).unref();
38
38
  }
39
39
 
40
+ function usage() {
41
+ console.error("Usage: super-pi [start|stop|restart|update]");
42
+ console.error(" start Open the web console (launch server if not running).");
43
+ console.error(" stop Shut the running server down.");
44
+ console.error(" restart Stop and start again, picking up the new code on disk.");
45
+ console.error(" update Update via npm to the latest @evandu/super-pi, then restart.");
46
+ process.exit(1);
47
+ }
48
+
40
49
  if (sub === "stop") {
41
50
  const running = await isRunning(port);
42
51
  if (!running) { console.log(`Super Pi is not running on port ${port}`); process.exit(0); }
@@ -46,16 +55,51 @@ if (sub === "stop") {
46
55
  process.exit(0);
47
56
  }
48
57
 
58
+ if (sub === "update") {
59
+ // `npm install -g` picks up the latest published version, then we hand off to the same restart
60
+ // path the in-app button uses. Spawning detached so this terminal can return immediately and
61
+ // the user's shell isn't tied to the upgrade.
62
+ console.log("Updating @evandu/super-pi via npm...");
63
+ const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
64
+ const updater = spawn(npmCmd, ["install", "-g", "@evandu/super-pi"], {
65
+ stdio: "inherit",
66
+ shell: process.platform === "win32",
67
+ env: process.env,
68
+ });
69
+ updater.on("error", (error) => {
70
+ console.error(`npm install failed: ${error instanceof Error ? error.message : "unknown error"}`);
71
+ process.exit(1);
72
+ });
73
+ updater.on("close", (code) => {
74
+ if (code !== 0) { console.error(`npm install exited with code ${code}`); process.exit(code ?? 1); }
75
+ console.log("Update installed. Restarting Super Pi...");
76
+ // The server itself uses scripts/restart-server.mjs for the same job; reuse it.
77
+ const restart = spawn(process.execPath, [resolve(import.meta.dirname, "../scripts/restart-server.mjs")], {
78
+ detached: true,
79
+ stdio: "ignore",
80
+ env: process.env,
81
+ });
82
+ restart.unref();
83
+ process.exit(0);
84
+ });
85
+ // The handler above is the only path out; the process keeps running so npm's output reaches
86
+ // this terminal.
87
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
88
+ await new Promise(() => {});
89
+ // unreachable: the close handler calls process.exit
90
+ return;
91
+ }
92
+
49
93
  if (sub === "restart") {
50
94
  if (await isRunning(port)) {
51
95
  console.log(`Restarting Super Pi on port ${port}...`);
52
96
  await stop(port);
53
97
  await new Promise((r) => setTimeout(r, 600));
98
+ } else {
99
+ console.log(`Super Pi is not running on port ${port}; starting fresh.`);
54
100
  }
55
101
  } else if (sub && sub !== "start") {
56
- console.error(`Unknown command: ${sub}`);
57
- console.error("Usage: super-pi [start|stop|restart]");
58
- process.exit(1);
102
+ usage();
59
103
  }
60
104
 
61
105
  const running = await isRunning(port);