Skip to content

Commit

Permalink
fix(windows): properly handle quoted args in event
Browse files Browse the repository at this point in the history
Fixes: #1823

This mirrors the command parsing from run.js into the separate parser
for launching event driven shells.
  • Loading branch information
remy committed Jul 10, 2021
1 parent b52fc89 commit 0823f18
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/spawn.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const utils = require('./utils');
const merge = utils.merge;
const bus = utils.bus;
Expand All @@ -10,26 +11,43 @@ module.exports = function spawnCommand(command, config, eventArgs) {
stdio = ['pipe', process.stdout, process.stderr];
}

const env = merge(process.env, { FILENAME: eventArgs[0] });

var sh = 'sh';
var shFlag = '-c';
var spawnOptions = {
env: merge(config.options.execOptions.env, env),
stdio: stdio,
};

if (utils.isWindows) {
sh = 'cmd';
shFlag = '/c';
if (!Array.isArray(command)) {
command = [command];
}

if (utils.isWindows) {
// if the exec includes a forward slash, reverse it for windows compat
// but *only* apply to the first command, and none of the arguments.
// ref #1251 and #1236
command = command.map(executable => {
if (executable.indexOf('/') === -1) {
return executable;
}

if (!Array.isArray(command)) {
command = [command];
return executable.split(' ').map((e, i) => {
if (i === 0) {
return path.normalize(e);
}
return e;
}).join(' ');
});
// taken from npm's cli: https://git.io/vNFD4
sh = process.env.comspec || 'cmd';
shFlag = '/d /s /c';
spawnOptions.windowsVerbatimArguments = true;
}

const args = command.join(' ');

const env = merge(process.env, { FILENAME: eventArgs[0] });
const child = spawn(sh, [shFlag, args], {
env: merge(config.options.execOptions.env, env),
stdio: stdio,
});
const child = spawn(sh, [shFlag, args], spawnOptions);

if (config.required) {
var emit = {
Expand Down

0 comments on commit 0823f18

Please sign in to comment.