|
3 | 3 | "use strict"; |
4 | 4 |
|
5 | 5 | const { stderr } = require("node:process"); |
| 6 | +const { spawn } = require("node:child_process"); |
| 7 | + |
| 8 | +const NPM_UPGRADE_HANDOFF = "JACOBIAN_NPM_UPGRADE_HANDOFF"; |
6 | 9 |
|
7 | 10 | /** |
8 | 11 | * Jacobian CLI entry point. |
@@ -58,6 +61,62 @@ function reportSetupFailure(error) { |
58 | 61 | process.exitCode = 1; |
59 | 62 | } |
60 | 63 |
|
| 64 | +/** |
| 65 | + * Resolve the latest npm bootstrap before upgrading the managed Python package. |
| 66 | + * |
| 67 | + * An unqualified npx invocation can execute an older cached launcher. The |
| 68 | + * handoff guard lets the latest package enter the pinned-runtime path once. |
| 69 | + * |
| 70 | + * @param {string[]} args |
| 71 | + */ |
| 72 | +function resolveLatestUpgrade(args) { |
| 73 | + const executable = |
| 74 | + process.env.JACOBIAN_NPX_EXECUTABLE || |
| 75 | + (process.platform === "win32" ? "npx.cmd" : "npx"); |
| 76 | + const child = spawn( |
| 77 | + executable, |
| 78 | + ["--yes", "--prefer-online", "jacobian@latest", "upgrade", ...args.slice(1)], |
| 79 | + { |
| 80 | + env: { ...process.env, [NPM_UPGRADE_HANDOFF]: "1" }, |
| 81 | + shell: process.platform === "win32", |
| 82 | + stdio: "inherit", |
| 83 | + }, |
| 84 | + ); |
| 85 | + const signals = ["SIGINT", "SIGTERM", "SIGHUP"]; |
| 86 | + const handlers = new Map( |
| 87 | + signals.map((signal) => [ |
| 88 | + signal, |
| 89 | + () => { |
| 90 | + if (!child.killed) child.kill(signal); |
| 91 | + }, |
| 92 | + ]), |
| 93 | + ); |
| 94 | + for (const [signal, handler] of handlers) process.on(signal, handler); |
| 95 | + |
| 96 | + const removeSignalHandlers = () => { |
| 97 | + for (const [signal, handler] of handlers) { |
| 98 | + process.removeListener(signal, handler); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + child.once("error", (error) => { |
| 103 | + removeSignalHandlers(); |
| 104 | + stderr.write( |
| 105 | + `Jacobian could not resolve its latest npm bootstrap: ${error.message}\n` + |
| 106 | + "Run `npx jacobian@latest upgrade` after checking that npx is available.\n", |
| 107 | + ); |
| 108 | + process.exitCode = 1; |
| 109 | + }); |
| 110 | + child.once("exit", (code, signal) => { |
| 111 | + removeSignalHandlers(); |
| 112 | + if (signal) { |
| 113 | + process.kill(process.pid, signal); |
| 114 | + return; |
| 115 | + } |
| 116 | + process.exitCode = code ?? 1; |
| 117 | + }); |
| 118 | +} |
| 119 | + |
61 | 120 | function main() { |
62 | 121 | const args = process.argv.slice(2); |
63 | 122 | const command = args[0]; |
@@ -152,6 +211,13 @@ function main() { |
152 | 211 | } |
153 | 212 |
|
154 | 213 | if (command === "upgrade") { |
| 214 | + if (process.env[NPM_UPGRADE_HANDOFF] !== "1") { |
| 215 | + stderr.write( |
| 216 | + "Resolving the latest Jacobian bootstrap before upgrading the managed Python runtime.\n", |
| 217 | + ); |
| 218 | + resolveLatestUpgrade(args); |
| 219 | + return; |
| 220 | + } |
155 | 221 | const { PACKAGE_SPEC, upgrade } = require("./launcher.cjs"); |
156 | 222 | try { |
157 | 223 | upgrade(); |
|
0 commit comments