|
1 | | -import { spawn } from "child_process"; |
| 1 | +import { spawn, type ChildProcess, type SpawnOptions } from "child_process"; |
2 | 2 | import React from "react"; |
3 | 3 | import * as fs from "fs"; |
4 | 4 | import * as os from "os"; |
@@ -162,9 +162,8 @@ async function promptUpdateChoice({ |
162 | 162 |
|
163 | 163 | async function runNpmInstallGlobal(installSpec: string): Promise<boolean> { |
164 | 164 | return new Promise((resolve) => { |
165 | | - const child = spawn("npm", ["install", "-g", installSpec], { |
| 165 | + const child = spawnNpm(["install", "-g", installSpec], { |
166 | 166 | stdio: "inherit", |
167 | | - shell: process.platform === "win32", |
168 | 167 | }); |
169 | 168 | child.on("error", (error) => { |
170 | 169 | process.stderr.write(`Failed to start npm install: ${error.message}\n`); |
@@ -206,9 +205,8 @@ function runNpmViewLatestVersion( |
206 | 205 | if (registry) { |
207 | 206 | args.push("--registry", registry); |
208 | 207 | } |
209 | | - const child = spawn("npm", args, { |
| 208 | + const child = spawnNpm(args, { |
210 | 209 | stdio: ["ignore", "pipe", "pipe"], |
211 | | - shell: process.platform === "win32", |
212 | 210 | }); |
213 | 211 |
|
214 | 212 | let stdout = ""; |
@@ -246,6 +244,24 @@ function runNpmViewLatestVersion( |
246 | 244 | }); |
247 | 245 | } |
248 | 246 |
|
| 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 | + |
249 | 265 | export function parseNpmViewVersion(output: string): string | null { |
250 | 266 | const trimmed = output.trim(); |
251 | 267 | if (!trimmed) { |
|
0 commit comments