cwm-assistant 0.3.2 → 0.3.4

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/main.js CHANGED
@@ -7,7 +7,7 @@ if (major < 22 || major === 22 && minor < 13) {
7
7
  console.error("Workspace \u52A9\u624B\u9700\u8981 Node.js 22.13 \u6216\u66F4\u9AD8\u7248\u672C\uFF08\u63A8\u8350 24 LTS\uFF09\u3002");
8
8
  process.exitCode = 1;
9
9
  } else {
10
- const { cli } = await import("./cli-D7E7V2UP.js");
10
+ const { cli } = await import("./cli-33QBXY2G.js");
11
11
  cli(process.argv.slice(2), fileURLToPath(import.meta.url)).catch((error) => {
12
12
  console.error(error instanceof Error ? error.message : String(error));
13
13
  process.exitCode = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cwm-assistant",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Local Git and workspace assistant for the CWM browser workbench",
5
5
  "keywords": [
6
6
  "cwm",
@@ -48,6 +48,36 @@ function record(stateDirectory, value) {
48
48
  }
49
49
  }
50
50
 
51
+ /**
52
+ * Find npm without relying on PATH.
53
+ *
54
+ * A login item starts with a minimal environment — no Homebrew, no nvm — so a bare `npm` spawn
55
+ * fails with ENOENT even though the shell that installed the package finds it. Every candidate is
56
+ * checked for existence; the bare name is the last resort for setups that do export PATH.
57
+ */
58
+ export function npmCandidates(platform = process.platform, execPath = process.execPath) {
59
+ const names = platform === "win32" ? ["npm.cmd"] : ["npm"];
60
+ const beside = path.dirname(execPath);
61
+ const candidates = names.map((name) => path.join(beside, name));
62
+ // npm's own entry point, present in every Node distribution regardless of the shim.
63
+ candidates.push(path.join(beside, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"));
64
+ if (platform !== "win32") candidates.push("/opt/homebrew/bin/npm", "/usr/local/bin/npm");
65
+ candidates.push(...names);
66
+ return candidates;
67
+ }
68
+
69
+ export function resolveNpm(platform = process.platform, execPath = process.execPath) {
70
+ for (const candidate of npmCandidates(platform, execPath)) {
71
+ if (!candidate.includes(path.sep) && !path.isAbsolute(candidate)) return candidate;
72
+ try {
73
+ if (fs.statSync(candidate).isFile()) return candidate;
74
+ } catch {
75
+ /* Try the next location. */
76
+ }
77
+ }
78
+ return platform === "win32" ? "npm.cmd" : "npm";
79
+ }
80
+
51
81
  async function main() {
52
82
  const { version, stateDirectory } = parseArgs(process.argv.slice(2));
53
83
  if (!version) throw new Error("缺少目标版本");
@@ -55,12 +85,23 @@ async function main() {
55
85
  const finished = await waitForExit(path.join(directory, "process.lock"));
56
86
  if (!finished) throw new Error("助手未在预期时间内退出");
57
87
 
58
- const npm = process.platform === "win32" ? "npm.cmd" : "npm";
88
+ const npm = resolveNpm();
89
+ // Running npm-cli.js directly needs Node, and a spawned npm still shells out for lifecycle
90
+ // scripts; export the directory Node lives in so both find it.
91
+ const command = npm.endsWith(".js") ? process.execPath : npm;
92
+ const args = npm.endsWith(".js")
93
+ ? [npm, "install", "-g", "cwm-assistant@" + version]
94
+ : ["install", "-g", "cwm-assistant@" + version];
95
+ const nodeDirectory = path.dirname(process.execPath);
59
96
  const installed = await new Promise((resolve) => {
60
- const child = spawn(npm, ["install", "-g", "cwm-assistant@" + version], {
97
+ const child = spawn(command, args, {
61
98
  stdio: ["ignore", "pipe", "pipe"],
62
99
  shell: false,
63
100
  windowsHide: true,
101
+ env: {
102
+ ...process.env,
103
+ PATH: [nodeDirectory, process.env.PATH].filter(Boolean).join(path.delimiter),
104
+ },
64
105
  });
65
106
  let output = "";
66
107
  const collect = (chunk) => {