-
-
Save sam-github/9144020 to your computer and use it in GitHub Desktop.
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 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 (!/[ \t"]/.test(arg)) | |
return arg; | |
if (!/[\\"]/.test(arg)) | |
return '"' + arg + '"'; | |
var result = '"'; | |
var quoteFlag = true; | |
for (var i = arg.length; i >= 0; i--) { | |
var char = arg[i]; | |
if (quoteFlag && char === '\\') { | |
result = '\\' + char + result; | |
} else if (char === '\\') { | |
quoteFlag = true; | |
result = '\\' + char + result | |
} else { | |
quoteFlag = false; | |
result = char + result; | |
} | |
} | |
result = ' "' + result; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment