1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const resolveCommand = require('./util/resolveCommand');
|
5 | const escape = require('./util/escape');
|
6 | const readShebang = require('./util/readShebang');
|
7 |
|
8 | const isWin = process.platform === 'win32';
|
9 | const isExecutableRegExp = /\.(?:com|exe)$/i;
|
10 | const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
|
11 |
|
12 | function detectShebang(parsed) {
|
13 | parsed.file = resolveCommand(parsed);
|
14 |
|
15 | const shebang = parsed.file && readShebang(parsed.file);
|
16 |
|
17 | if (shebang) {
|
18 | parsed.args.unshift(parsed.file);
|
19 | parsed.command = shebang;
|
20 |
|
21 | return resolveCommand(parsed);
|
22 | }
|
23 |
|
24 | return parsed.file;
|
25 | }
|
26 |
|
27 | function parseNonShell(parsed) {
|
28 | if (!isWin) {
|
29 | return parsed;
|
30 | }
|
31 |
|
32 |
|
33 | const commandFile = detectShebang(parsed);
|
34 |
|
35 |
|
36 | const needsShell = !isExecutableRegExp.test(commandFile);
|
37 |
|
38 |
|
39 |
|
40 | if (parsed.options.forceShell || needsShell) {
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
|
46 |
|
47 |
|
48 |
|
49 | parsed.command = path.normalize(parsed.command);
|
50 |
|
51 |
|
52 | parsed.command = escape.command(parsed.command);
|
53 | parsed.args = parsed.args.map((arg) => escape.argument(arg, needsDoubleEscapeMetaChars));
|
54 |
|
55 | const shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
56 |
|
57 | parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
|
58 | parsed.command = process.env.comspec || 'cmd.exe';
|
59 | parsed.options.windowsVerbatimArguments = true;
|
60 | }
|
61 |
|
62 | return parsed;
|
63 | }
|
64 |
|
65 | function parse(command, args, options) {
|
66 |
|
67 | if (args && !Array.isArray(args)) {
|
68 | options = args;
|
69 | args = null;
|
70 | }
|
71 |
|
72 | args = args ? args.slice(0) : [];
|
73 | options = Object.assign({}, options);
|
74 |
|
75 |
|
76 | const parsed = {
|
77 | command,
|
78 | args,
|
79 | options,
|
80 | file: undefined,
|
81 | original: {
|
82 | command,
|
83 | args,
|
84 | },
|
85 | };
|
86 |
|
87 |
|
88 | return options.shell ? parsed : parseNonShell(parsed);
|
89 | }
|
90 |
|
91 | module.exports = parse;
|