@event4u/agent-switch 1.6.0 → 1.6.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.
- package/dist/updates.js +30 -2
- package/package.json +1 -1
package/dist/updates.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { spawnSync } from "node:child_process";
|
|
13
13
|
import * as fs from "node:fs";
|
|
14
|
+
import * as os from "node:os";
|
|
14
15
|
import * as path from "node:path";
|
|
15
16
|
import { fileURLToPath } from "node:url";
|
|
16
17
|
/** The published npm package + the repo whose GitHub Releases are the update source. */
|
|
@@ -102,9 +103,36 @@ export async function checkForUpdate(repo = UPDATE_REPO) {
|
|
|
102
103
|
return { kind: "error", current, message: e instanceof Error ? e.message : String(e) };
|
|
103
104
|
}
|
|
104
105
|
}
|
|
106
|
+
/** Build a PATH that can locate `npm` even when this CLI was spawned from the
|
|
107
|
+
* GUI with a minimal environment PATH (`/usr/bin:/bin:/usr/sbin:/sbin`) — the
|
|
108
|
+
* case a macOS/Linux app launched from Finder or the menu bar inherits. `npm`
|
|
109
|
+
* always ships in the same bin directory as the running `node`
|
|
110
|
+
* (`process.execPath`), so that dir goes first; the current PATH and the common
|
|
111
|
+
* global-install locations follow as fallbacks. Mirrors the GUI's Rust PATH
|
|
112
|
+
* recovery so the deepest spawn (`npm`) resolves regardless of launch context.
|
|
113
|
+
* Pure — every source is injectable for tests. */
|
|
114
|
+
export function npmSearchPath(nodeDir = path.dirname(process.execPath), currentPath = process.env.PATH ?? "", home = os.homedir()) {
|
|
115
|
+
return [
|
|
116
|
+
nodeDir,
|
|
117
|
+
currentPath,
|
|
118
|
+
"/opt/homebrew/bin",
|
|
119
|
+
"/usr/local/bin",
|
|
120
|
+
path.join(home, ".npm-global", "bin"),
|
|
121
|
+
path.join(home, ".local", "bin"),
|
|
122
|
+
]
|
|
123
|
+
.filter(Boolean)
|
|
124
|
+
.join(path.delimiter);
|
|
125
|
+
}
|
|
105
126
|
/** Self-update: reinstall the npm package at @latest (global). Streams npm's
|
|
106
|
-
* own output; returns its exit code (0 = success).
|
|
127
|
+
* own output; returns its exit code (0 = success). The spawn runs with a
|
|
128
|
+
* PATH that can always find `npm` ({@link npmSearchPath}), so a GUI-triggered
|
|
129
|
+
* update works from Finder/menu-bar launches with a stripped PATH; `shell` is
|
|
130
|
+
* required on Windows, where `npm` is a `.cmd` shim. */
|
|
107
131
|
export function selfUpdate() {
|
|
108
|
-
const r = spawnSync("npm", ["install", "-g", `${PACKAGE_NAME}@latest`], {
|
|
132
|
+
const r = spawnSync("npm", ["install", "-g", `${PACKAGE_NAME}@latest`], {
|
|
133
|
+
stdio: "inherit",
|
|
134
|
+
shell: process.platform === "win32",
|
|
135
|
+
env: { ...process.env, PATH: npmSearchPath() },
|
|
136
|
+
});
|
|
109
137
|
return r.status ?? 1;
|
|
110
138
|
}
|
package/package.json
CHANGED