Skip to content

Instantly share code, notes, and snippets.

@piscisaureus
Last active February 6, 2017 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save piscisaureus/9143815 to your computer and use it in GitHub Desktop.
Save piscisaureus/9143815 to your computer and use it in GitHub Desktop.
Command.js
var assert = require('assert');
var spawn = require('child_process').spawn;
module.exports = function Command(command, npmModule) {
return function(argv, options, loader) {
var options = {
env: process.env,
stdio: 'inherit',
windowsVerbatimArguments: true
};
if (process.platform === 'win32') {
var quotedArgs = argv.map(windowsQuoteArg).join('');
var file = 'cmd.exe';
var args = ['/s', '/c', '"' + command + quotedArgs + '"'];
} else {
var file = '/bin/sh';
var args = ['-c', command].concat(argv);
}
return console.log([file].concat(args).join(' '));
return spawn(file, args, options)
.on('error', function(er) {
loader.error('Error running %s (%s), it may need installation, try `npm update -g %s`.',
command, er.message, npmModule);
}); };
};
function windowsQuoteArg(arg) {
arg = String(arg);
// If unnecessary, don't use quotes at all.
if (!/[ \t"]/.test(arg))
return ' ' + arg;
// String contains only spaces. It's sufficient to wrap in quotation marks.
if (!/[\\"]/.test(arg))
return ' "' + arg + '"';
/* String needs to be fully quoted.
* Expected input/output:
* input : hello"world
* output: "hello\"world"
* input : hello""world
* output: "hello\"\"world"
* input : hello\world
* output: hello\world
* input : hello\\world
* output: hello\\world
* input : hello\"world
* output: "hello\\\"world"
* input : hello\\"world
* output: "hello\\\\\"world"
* input : hello world\
* output: "hello world\"
*/
var result = '"';
var quoteFlag = true;
for (var i = arg.length - 1; i >= 0; i--) {
var char = arg[i];
if (quoteFlag && char === '\\') {
result = '\\\\' + result;
} else if (char === '"') {
quoteFlag = true;
result = '\\"' + result;
} else {
quoteFlag = false;
result = char + result;
}
}
result = ' "' + result;
return result;
}
var tests = [
['hello"world', ' "hello\\"world"'],
['hello""world', ' "hello\\"\\"world"'],
['hello\\world', ' hello\\world'],
['hello\\\\world', ' hello\\\\world'],
['hello\\"world', ' "hello\\\\\\"world"'],
['hello\\\\"world', ' "hello\\\\\\\\\"world"'],
['hello world\\', ' "hello world\\"'],
];
for (var i = 0; i < tests.length; i++) {
var t = tests[i];
assert.strictEqual(windowsQuoteArg(t[0]), t[1]);
}
@sam-github
Copy link

And I was just trying this with some success... I will read the above.

diff --git a/lib/command.js b/lib/command.js
index 54cfeac..47afa83 100644
--- a/lib/command.js
+++ b/lib/command.js
@@ -8,6 +8,12 @@ module.exports = function Command(command, npmModule) {
       stdio: 'inherit'
     };

+    var cmd = command + " " + argv.join(" ");
+    console.log('cmd:', cmd);
+
+    command = 'cmd';
+    argv = ['/c', cmd];
+
     return spawn(command, argv, options)
       .on('error', function(er) {
         loader.error('Error running %s (%s), it may need installation, try `npm update -g %s`.',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment