Skip to content

Commit

Permalink
Simplify Windows-related code (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Sep 14, 2024
1 parent 742a917 commit 8eae6e6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
8 changes: 2 additions & 6 deletions source/spawn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {spawn} from 'node:child_process';
import {once} from 'node:events';
import process from 'node:process';
import {getForcedShell, escapeArguments} from './windows.js';
import {applyForceShell} from './windows.js';
import {getResultError} from './result.js';

export const spawnSubprocess = async (file, commandArguments, options, context) => {
Expand All @@ -13,11 +13,7 @@ export const spawnSubprocess = async (file, commandArguments, options, context)
? [process.execPath, [...process.execArgv.filter(flag => !flag.startsWith('--inspect')), ...commandArguments]]
: [file, commandArguments];

const forcedShell = await getForcedShell(file, options);
const instance = spawn(...escapeArguments(file, commandArguments, forcedShell), {
...options,
shell: options.shell || forcedShell,
});
const instance = spawn(...await applyForceShell(file, commandArguments, options));
bufferOutput(instance.stdout, context, 'stdout');
bufferOutput(instance.stderr, context, 'stderr');

Expand Down
14 changes: 7 additions & 7 deletions source/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import {stat} from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';

// When setting `shell: true` under-the-hood, we must manually escape the file and arguments.
// This ensures arguments are properly split, and prevents command injection.
export const applyForceShell = async (file, commandArguments, options) => await shouldForceShell(file, options)
? [escapeFile(file), commandArguments.map(argument => escapeArgument(argument)), {...options, shell: true}]
: [file, commandArguments, options];

// On Windows, running most executable files (except *.exe and *.com) requires using a shell.
// This includes *.cmd and *.bat, which itself includes Node modules binaries.
// We detect this situation and automatically:
// - Set the `shell: true` option
// - Escape shell-specific characters
export const getForcedShell = async (file, {shell, cwd, env = process.env}) => process.platform === 'win32'
const shouldForceShell = async (file, {shell, cwd, env = process.env}) => process.platform === 'win32'
&& !shell
&& !(await isExe(file, cwd, env));

Expand Down Expand Up @@ -60,12 +66,6 @@ const mIsExe = async (file, cwd, PATH) => {
// Other file extensions require using a shell
const exeExtensions = ['.exe', '.com'];

// When setting `shell: true` under-the-hood, we must manually escape the file and arguments.
// This ensures arguments are properly split, and prevents command injection.
export const escapeArguments = (file, commandArguments, forcedShell) => forcedShell
? [escapeFile(file), commandArguments.map(argument => escapeArgument(argument))]
: [file, commandArguments];

// `cmd.exe` escaping for arguments.
// Taken from https://github.com/moxystudio/node-cross-spawn
const escapeArgument = argument => escapeFile(escapeFile(`"${argument
Expand Down

0 comments on commit 8eae6e6

Please sign in to comment.