3 var fs = require('fs');
4 var LRU = require('lru-cache');
5 var resolveCommand = require('./resolveCommand');
7 var isWin = process.platform === 'win32';
8 var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec
10 function readShebang(command) {
16 // Check if it is in the cache first
17 if (shebangCache.has(command)) {
18 return shebangCache.get(command);
21 // Read the first 150 bytes from the file
22 buffer = new Buffer(150);
25 fd = fs.openSync(command, 'r');
26 fs.readSync(fd, buffer, 0, 150, 0);
28 } catch (e) { /* empty */ }
30 // Check if it is a shebang
31 match = buffer.toString().trim().match(/#!(.+)/i);
34 shebang = match[1].replace(/\/usr\/bin\/env\s+/i, ''); // Remove /usr/bin/env
37 // Store the shebang in the cache
38 shebangCache.set(command, shebang);
43 function escapeArg(arg, quote) {
47 // If we are not going to quote the argument,
48 // escape shell metacharacters, including double and single quotes:
50 arg = arg.replace(/([\(\)%!\^<>&|;,"'\s])/g, '^$1');
52 // Sequence of backslashes followed by a double quote:
53 // double up all the backslashes and escape the double quote
54 arg = arg.replace(/(\\*)"/g, '$1$1\\"');
56 // Sequence of backslashes followed by the end of the string
57 // (which will become a double quote later):
58 // double up all the backslashes
59 arg = arg.replace(/(\\*)$/, '$1$1');
61 // All other backslashes occur literally
63 // Quote the whole thing:
64 arg = '"' + arg + '"';
70 function escapeCommand(command) {
71 // Do not escape if this command is not dangerous..
72 // We do this so that commands like "echo" or "ifconfig" work
73 // Quoting them, will make them unaccessible
74 return /^[a-z0-9_-]+$/i.test(command) ? command : escapeArg(command, true);
77 function parse(command, args, options) {
83 // Normalize arguments, similar to nodejs
84 if (args && !Array.isArray(args)) {
89 args = args ? args.slice(0) : []; // Clone array to avoid changing the original
90 options = options || {};
94 // Detect & add support for shebangs
95 file = resolveCommand(command);
96 file = file || resolveCommand(command, true);
97 shebang = file && readShebang(file);
104 // Escape command & arguments
105 applyQuotes = command !== 'echo'; // Do not quote arguments for the special "echo" command
106 command = escapeCommand(command);
107 args = args.map(function (arg) {
108 return escapeArg(arg, applyQuotes);
112 args = ['/s', '/c', '"' + command + (args.length ? ' ' + args.join(' ') : '') + '"'];
113 command = process.env.comspec || 'cmd.exe';
115 // Tell node's spawn that the arguments are already escaped
116 options.windowsVerbatimArguments = true;
128 module.exports = parse;