1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const niceTry = require('nice-try');
|
5 | const resolveCommand = require('./util/resolveCommand');
|
6 | const escape = require('./util/escape');
|
7 | const readShebang = require('./util/readShebang');
|
8 | const semver = require('semver');
|
9 |
|
10 | const isWin = process.platform === 'win32';
|
11 | const isExecutableRegExp = /\.(?:com|exe)$/i;
|
12 | const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
|
13 |
|
14 |
|
15 | const supportsShellOption = niceTry(() => semver.satisfies(process.version, '^4.8.0 || ^5.7.0 || >= 6.0.0', true)) || false;
|
16 |
|
17 | function detectShebang(parsed) {
|
18 | parsed.file = resolveCommand(parsed);
|
19 |
|
20 | const shebang = parsed.file && readShebang(parsed.file);
|
21 |
|
22 | if (shebang) {
|
23 | parsed.args.unshift(parsed.file);
|
24 | parsed.command = shebang;
|
25 |
|
26 | return resolveCommand(parsed);
|
27 | }
|
28 |
|
29 | return parsed.file;
|
30 | }
|
31 |
|
32 | function parseNonShell(parsed) {
|
33 | if (!isWin) {
|
34 | return parsed;
|
35 | }
|
36 |
|
37 |
|
38 | const commandFile = detectShebang(parsed);
|
39 |
|
40 |
|
41 | const needsShell = !isExecutableRegExp.test(commandFile);
|
42 |
|
43 |
|
44 |
|
45 | if (parsed.options.forceShell || needsShell) {
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
|
51 |
|
52 |
|
53 |
|
54 | parsed.command = path.normalize(parsed.command);
|
55 |
|
56 |
|
57 | parsed.command = escape.command(parsed.command);
|
58 | parsed.args = parsed.args.map((arg) => escape.argument(arg, needsDoubleEscapeMetaChars));
|
59 |
|
60 | const shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
61 |
|
62 | parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
|
63 | parsed.command = process.env.comspec || 'cmd.exe';
|
64 | parsed.options.windowsVerbatimArguments = true;
|
65 | }
|
66 |
|
67 | return parsed;
|
68 | }
|
69 |
|
70 | function parseShell(parsed) {
|
71 |
|
72 | if (supportsShellOption) {
|
73 | return parsed;
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | const shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
79 |
|
80 | if (isWin) {
|
81 | parsed.command = typeof parsed.options.shell === 'string' ? parsed.options.shell : process.env.comspec || 'cmd.exe';
|
82 | parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
|
83 | parsed.options.windowsVerbatimArguments = true;
|
84 | } else {
|
85 | if (typeof parsed.options.shell === 'string') {
|
86 | parsed.command = parsed.options.shell;
|
87 | } else if (process.platform === 'android') {
|
88 | parsed.command = '/system/bin/sh';
|
89 | } else {
|
90 | parsed.command = '/bin/sh';
|
91 | }
|
92 |
|
93 | parsed.args = ['-c', shellCommand];
|
94 | }
|
95 |
|
96 | return parsed;
|
97 | }
|
98 |
|
99 | function parse(command, args, options) {
|
100 |
|
101 | if (args && !Array.isArray(args)) {
|
102 | options = args;
|
103 | args = null;
|
104 | }
|
105 |
|
106 | args = args ? args.slice(0) : [];
|
107 | options = Object.assign({}, options);
|
108 |
|
109 |
|
110 | const parsed = {
|
111 | command,
|
112 | args,
|
113 | options,
|
114 | file: undefined,
|
115 | original: {
|
116 | command,
|
117 | args,
|
118 | },
|
119 | };
|
120 |
|
121 |
|
122 | return options.shell ? parseShell(parsed) : parseNonShell(parsed);
|
123 | }
|
124 |
|
125 | module.exports = parse;
|