Skip to content

Commit 8e6e13f

Browse files
committed
feat: improve npm command execution for cross-platform compatibility
1 parent 4680a30 commit 8e6e13f

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/updateCheck.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawn } from "child_process";
1+
import { spawn, type ChildProcess, type SpawnOptions } from "child_process";
22
import React from "react";
33
import * as fs from "fs";
44
import * as os from "os";
@@ -162,9 +162,8 @@ async function promptUpdateChoice({
162162

163163
async function runNpmInstallGlobal(installSpec: string): Promise<boolean> {
164164
return new Promise((resolve) => {
165-
const child = spawn("npm", ["install", "-g", installSpec], {
165+
const child = spawnNpm(["install", "-g", installSpec], {
166166
stdio: "inherit",
167-
shell: process.platform === "win32",
168167
});
169168
child.on("error", (error) => {
170169
process.stderr.write(`Failed to start npm install: ${error.message}\n`);
@@ -206,9 +205,8 @@ function runNpmViewLatestVersion(
206205
if (registry) {
207206
args.push("--registry", registry);
208207
}
209-
const child = spawn("npm", args, {
208+
const child = spawnNpm(args, {
210209
stdio: ["ignore", "pipe", "pipe"],
211-
shell: process.platform === "win32",
212210
});
213211

214212
let stdout = "";
@@ -246,6 +244,24 @@ function runNpmViewLatestVersion(
246244
});
247245
}
248246

247+
function spawnNpm(args: string[], options: SpawnOptions): ChildProcess {
248+
if (process.platform === "win32") {
249+
return spawn(["npm", ...args.map(quoteCmdArg)].join(" "), [], {
250+
...options,
251+
shell: true,
252+
});
253+
}
254+
255+
return spawn("npm", args, {
256+
...options,
257+
shell: false,
258+
});
259+
}
260+
261+
function quoteCmdArg(arg: string): string {
262+
return `"${String(arg).replace(/"/g, '\\"')}"`;
263+
}
264+
249265
export function parseNpmViewVersion(output: string): string | null {
250266
const trimmed = output.trim();
251267
if (!trimmed) {

0 commit comments

Comments
 (0)