1 | const path = require('path');
|
2 | const fs = require('fs');
|
3 | const existsSync = fs.existsSync;
|
4 | const utils = require('../utils');
|
5 |
|
6 | module.exports = exec;
|
7 | module.exports.expandScript = expandScript;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function execFromPackage() {
|
16 |
|
17 |
|
18 |
|
19 | try {
|
20 |
|
21 | var pkg = require(path.join(process.cwd(), 'package.json'));
|
22 | if (pkg.main !== undefined) {
|
23 |
|
24 | return { exec: null, script: pkg.main };
|
25 | }
|
26 |
|
27 | if (pkg.scripts && pkg.scripts.start) {
|
28 | return { exec: pkg.scripts.start };
|
29 | }
|
30 | } catch (e) { }
|
31 |
|
32 | return null;
|
33 | }
|
34 |
|
35 | function replace(map, str) {
|
36 | var re = new RegExp('{{(' + Object.keys(map).join('|') + ')}}', 'g');
|
37 | return str.replace(re, function (all, m) {
|
38 | return map[m] || all || '';
|
39 | });
|
40 | }
|
41 |
|
42 | function expandScript(script, ext) {
|
43 | if (!ext) {
|
44 | ext = '.js';
|
45 | }
|
46 | if (script.indexOf(ext) !== -1) {
|
47 | return script;
|
48 | }
|
49 |
|
50 | if (existsSync(path.resolve(script))) {
|
51 | return script;
|
52 | }
|
53 |
|
54 | if (existsSync(path.resolve(script + ext))) {
|
55 | return script + ext;
|
56 | }
|
57 |
|
58 | return script;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function exec(nodemonOptions, execMap) {
|
72 | if (!execMap) {
|
73 | execMap = {};
|
74 | }
|
75 |
|
76 | var options = utils.clone(nodemonOptions || {});
|
77 | var script;
|
78 |
|
79 |
|
80 | if (!options.script && (options.args || []).length) {
|
81 | script = expandScript(options.args[0],
|
82 | options.ext && ('.' + (options.ext || 'js').split(',')[0]));
|
83 |
|
84 |
|
85 | if (script !== options.args[0]) {
|
86 | options.script = script;
|
87 | options.args.shift();
|
88 | }
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | if (!options.exec && !options.script) {
|
96 | var found = execFromPackage();
|
97 | if (found !== null) {
|
98 | if (found.exec) {
|
99 | options.exec = found.exec;
|
100 | }
|
101 | if (!options.script) {
|
102 | options.script = found.script;
|
103 | }
|
104 | if (Array.isArray(options.args) &&
|
105 | options.scriptPosition === null) {
|
106 | options.scriptPosition = options.args.length;
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 |
|
112 | script = path.basename(options.script || '');
|
113 |
|
114 | var scriptExt = path.extname(script).slice(1);
|
115 |
|
116 | var extension = options.ext;
|
117 | if (extension === undefined) {
|
118 | var isJS = scriptExt === 'js' || scriptExt === 'mjs';
|
119 | extension = (isJS || !scriptExt) ? 'js,mjs' : scriptExt;
|
120 | extension += ',json';
|
121 | }
|
122 |
|
123 | var execDefined = !!options.exec;
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | if (!options.exec && execMap[scriptExt] !== undefined) {
|
129 | options.exec = execMap[scriptExt];
|
130 | execDefined = true;
|
131 | }
|
132 |
|
133 | options.execArgs = nodemonOptions.execArgs || [];
|
134 |
|
135 | if (Array.isArray(options.exec)) {
|
136 | options.execArgs = options.exec;
|
137 | options.exec = options.execArgs.shift();
|
138 | }
|
139 |
|
140 | if (options.exec === undefined) {
|
141 | options.exec = 'node';
|
142 | } else {
|
143 |
|
144 | var substitution = replace.bind(null, {
|
145 | filename: options.script,
|
146 | pwd: process.cwd(),
|
147 | });
|
148 |
|
149 | var newExec = substitution(options.exec);
|
150 | if (newExec !== options.exec &&
|
151 | options.exec.indexOf('{{filename}}') !== -1) {
|
152 | options.script = null;
|
153 | }
|
154 | options.exec = newExec;
|
155 |
|
156 | var newExecArgs = options.execArgs.map(substitution);
|
157 | if (newExecArgs.join('') !== options.execArgs.join('')) {
|
158 | options.execArgs = newExecArgs;
|
159 | delete options.script;
|
160 | }
|
161 | }
|
162 |
|
163 |
|
164 | if (options.exec === 'node' && options.nodeArgs && options.nodeArgs.length) {
|
165 | options.execArgs = options.execArgs.concat(options.nodeArgs);
|
166 | }
|
167 |
|
168 |
|
169 | if (!execDefined && options.exec === 'node' &&
|
170 | scriptExt.indexOf('coffee') !== -1) {
|
171 | options.exec = 'coffee';
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | var leadingArgs = (options.args || []).splice(0, options.scriptPosition);
|
177 | options.execArgs = options.execArgs.concat(leadingArgs);
|
178 | options.scriptPosition = 0;
|
179 |
|
180 | if (options.execArgs.length > 0) {
|
181 |
|
182 |
|
183 | options.execArgs = ['--nodejs', options.execArgs.join(' ')];
|
184 | }
|
185 | }
|
186 |
|
187 | if (options.exec === 'coffee') {
|
188 |
|
189 | if (options.ext === undefined) {
|
190 | if (extension) { extension += ','; }
|
191 | extension += 'coffee,litcoffee';
|
192 | }
|
193 |
|
194 |
|
195 | if (utils.isWindows) {
|
196 | options.exec += '.cmd';
|
197 | }
|
198 | }
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | extension = (extension.match(/[^,*\s]+/g) || [])
|
206 | .map(ext => ext.replace(/^\./, ''))
|
207 | .join(',');
|
208 |
|
209 | options.ext = extension;
|
210 |
|
211 | if (options.script) {
|
212 | options.script = expandScript(options.script,
|
213 | extension && ('.' + extension.split(',')[0]));
|
214 | }
|
215 |
|
216 | options.env = {};
|
217 |
|
218 | if (({}).toString.apply(nodemonOptions.env) === '[object Object]') {
|
219 | options.env = utils.clone(nodemonOptions.env);
|
220 | } else if (nodemonOptions.env !== undefined) {
|
221 | throw new Error('nodemon env values must be an object: { PORT: 8000 }');
|
222 | }
|
223 |
|
224 | return options;
|
225 | }
|