Initial commit
[yaffs-website] / node_modules / node-sass / node_modules / cross-spawn / lib / parse.js
1 'use strict';
2
3 var fs = require('fs');
4 var LRU = require('lru-cache');
5 var resolveCommand = require('./resolveCommand');
6
7 var isWin = process.platform === 'win32';
8 var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 });  // Cache just for 30sec
9
10 function readShebang(command) {
11     var buffer;
12     var fd;
13     var match;
14     var shebang;
15
16     // Check if it is in the cache first
17     if (shebangCache.has(command)) {
18         return shebangCache.get(command);
19     }
20
21     // Read the first 150 bytes from the file
22     buffer = new Buffer(150);
23
24     try {
25         fd = fs.openSync(command, 'r');
26         fs.readSync(fd, buffer, 0, 150, 0);
27         fs.closeSync(fd);
28     } catch (e) { /* empty */ }
29
30     // Check if it is a shebang
31     match = buffer.toString().trim().match(/#!(.+)/i);
32
33     if (match) {
34         shebang = match[1].replace(/\/usr\/bin\/env\s+/i, '');   // Remove /usr/bin/env
35     }
36
37     // Store the shebang in the cache
38     shebangCache.set(command, shebang);
39
40     return shebang;
41 }
42
43 function escapeArg(arg, quote) {
44     // Convert to string
45     arg = '' + arg;
46
47     // If we are not going to quote the argument,
48     // escape shell metacharacters, including double and single quotes:
49     if (!quote) {
50         arg = arg.replace(/([\(\)%!\^<>&|;,"'\s])/g, '^$1');
51     } else {
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\\"');
55
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');
60
61         // All other backslashes occur literally
62
63         // Quote the whole thing:
64         arg = '"' + arg + '"';
65     }
66
67     return arg;
68 }
69
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);
75 }
76
77 function parse(command, args, options) {
78     var shebang;
79     var applyQuotes;
80     var file;
81     var original;
82
83     // Normalize arguments, similar to nodejs
84     if (args && !Array.isArray(args)) {
85         options = args;
86         args = null;
87     }
88
89     args = args ? args.slice(0) : [];  // Clone array to avoid changing the original
90     options = options || {};
91     original = command;
92
93     if (isWin) {
94         // Detect & add support for shebangs
95         file = resolveCommand(command);
96         file = file || resolveCommand(command, true);
97         shebang = file && readShebang(file);
98
99         if (shebang) {
100             args.unshift(file);
101             command = shebang;
102         }
103
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);
109         });
110
111         // Use cmd.exe
112         args = ['/s', '/c', '"' + command + (args.length ? ' ' + args.join(' ') : '') + '"'];
113         command = process.env.comspec || 'cmd.exe';
114
115         // Tell node's spawn that the arguments are already escaped
116         options.windowsVerbatimArguments = true;
117     }
118
119     return {
120         command: command,
121         args: args,
122         options: options,
123         file: file,
124         original: original,
125     };
126 }
127
128 module.exports = parse;