Last active
February 6, 2017 03:17
-
-
Save piscisaureus/9143815 to your computer and use it in GitHub Desktop.
Command.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And I was just trying this with some success... I will read the above.