Skip to content

Commit

Permalink
bugfixes for windows spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
q-nick committed Feb 4, 2023
1 parent 47674db commit 4979157
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions server/actions/execute-command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { spawn } from 'child_process';

import { spawn } from '../utils/simple-cross-spawn';
import { ZERO } from '../utils/utils';

export const executeCommand = (
Expand All @@ -18,7 +17,6 @@ export const executeCommand = (
const spawned = spawn(command, commandArguments, {
cwd,
detached: false,
shell: process.platform === 'win32',
});

// wait for stdout, stderr
Expand All @@ -34,9 +32,6 @@ export const executeCommand = (

// wait for finish and resolve
spawned.on('close', (exitStatus: number) => {
if (!process.env['NODE_TEST']) {
// console.log(exitStatus);
}
if (exitStatus === ZERO) {
resolve({
stdout,
Expand Down
30 changes: 30 additions & 0 deletions server/utils/simple-cross-spawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ChildProcess, SpawnOptionsWithoutStdio } from 'child_process';
import { spawn as cpSpawn } from 'child_process';

const metaCharsRegExp = /([ !"%&()*,;<>?[\]^`|])/g;

export const spawn = (
command: string,
arguments_?: readonly string[],
options?: SpawnOptionsWithoutStdio,
): ChildProcess => {
if (process.platform !== 'win32') {
return cpSpawn(command, arguments_, options);
}

const shellCommand = [
command,
...(arguments_ || []).map((argument) =>
`"${argument}"`.replace(metaCharsRegExp, '^$1'),
),
].join(' ');

return cpSpawn(
process.env['comspec'] || 'cmd.exe',
['/d', '/s', '/c', `"${shellCommand}"`],
{
...options,
windowsVerbatimArguments: true,
},
);
};

0 comments on commit 4979157

Please sign in to comment.