MCPcopy Create free account
hub / github.com/Dart-Code/Dart-Code / safeSpawn

Function safeSpawn

src/shared/processes.ts:11–26  ·  view source on GitHub ↗
(workingDirectory: string | undefined, binPath: string, args: string[], env: Record<string, string | undefined> | undefined)

Source from the content-addressed store, hash-verified

9const simpleCommandRegex = new RegExp("^[\\w\\-.]+$");
10
11export function safeSpawn(workingDirectory: string | undefined, binPath: string, args: string[], env: Record<string, string | undefined> | undefined): SpawnedProcess {
12 const customEnv = Object.assign({}, process.env, env);
13
14 // On Windows we need to use shell-execute for running `.bat` files.
15 // Try to limit when we use this, because terminating a shell might not terminate
16 // the spawned process, so not using shell-execute may improve reliability of
17 // terminating processes.
18 if (isWin && binPath.endsWith(".bat")) {
19 const quotedArgs = args.map(quoteAndEscapeArg);
20 // Putting quotes around something like "git" will cause it to fail, so don't do it if binPath is just a single identifier.
21 binPath = simpleCommandRegex.test(binPath) ? binPath : `"${binPath}"`;
22 return child_process.spawn(binPath, quotedArgs, { cwd: workingDirectory, env: customEnv, shell: true }) satisfies SpawnedProcess;
23 }
24
25 return child_process.spawn(binPath, args, { cwd: workingDirectory, env: customEnv }) satisfies SpawnedProcess;
26}
27
28function quoteAndEscapeArg(arg: string) {
29 // Spawning processes on Windows with funny symbols in the path requires quoting. However if you quote an

Callers 3

createProcessFunction · 0.90
launchCustomEmulatorMethod · 0.90
safeToolSpawnFunction · 0.90

Calls 1

spawnMethod · 0.80

Tested by

no test coverage detected