1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var fs = require('fs');
|
19 | var path = require('path');
|
20 | var existsSync = fs.existsSync || path.existsSync;
|
21 |
|
22 | module.exports = parse;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function parse(argv) {
|
32 | if (typeof argv === 'string') {
|
33 | argv = argv.split(' ');
|
34 | }
|
35 |
|
36 | var eat = function (i, args) {
|
37 | if (i <= args.length) {
|
38 | return args.splice(i + 1, 1).pop();
|
39 | }
|
40 | };
|
41 |
|
42 | var args = argv.slice(2);
|
43 | var script = null;
|
44 | var nodemonOptions = { scriptPosition: null };
|
45 |
|
46 | var nodemonOpt = nodemonOption.bind(null, nodemonOptions);
|
47 | var lookForArgs = true;
|
48 |
|
49 |
|
50 | for (var i = 0; i < args.length; i++) {
|
51 |
|
52 | if (!script) {
|
53 | if (args[i] === '.' || existsSync(args[i])) {
|
54 | script = args.splice(i, 1).pop();
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | nodemonOptions.scriptPosition = i;
|
61 | i--;
|
62 | continue;
|
63 | }
|
64 | }
|
65 |
|
66 | if (lookForArgs) {
|
67 |
|
68 | if (args[i] === '--') {
|
69 | args.splice(i, 1);
|
70 | nodemonOptions.scriptPosition = i;
|
71 |
|
72 | i--;
|
73 |
|
74 |
|
75 | lookForArgs = false;
|
76 |
|
77 |
|
78 | continue;
|
79 | }
|
80 |
|
81 | if (nodemonOpt(args[i], eat.bind(null, i, args)) !== false) {
|
82 | args.splice(i, 1);
|
83 |
|
84 | i--;
|
85 | }
|
86 | }
|
87 | }
|
88 |
|
89 | nodemonOptions.script = script;
|
90 | nodemonOptions.args = args;
|
91 |
|
92 | return nodemonOptions;
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function nodemonOption(options, arg, eatNext) {
|
106 |
|
107 | if (arg === '--help' || arg === '-h' || arg === '-?') {
|
108 | var help = eatNext();
|
109 | options.help = help ? help : true;
|
110 | } else
|
111 |
|
112 | if (arg === '--version' || arg === '-v') {
|
113 | options.version = true;
|
114 | } else
|
115 |
|
116 | if (arg === '--no-update-notifier') {
|
117 | options.noUpdateNotifier = true;
|
118 | } else
|
119 |
|
120 | if (arg === '--spawn') {
|
121 | options.spawn = true;
|
122 | } else
|
123 |
|
124 | if (arg === '--dump') {
|
125 | options.dump = true;
|
126 | } else
|
127 |
|
128 | if (arg === '--verbose' || arg === '-V') {
|
129 | options.verbose = true;
|
130 | } else
|
131 |
|
132 | if (arg === '--legacy-watch' || arg === '-L') {
|
133 | options.legacyWatch = true;
|
134 | } else
|
135 |
|
136 | if (arg === '--polling-interval' || arg === '-P') {
|
137 | options.pollingInterval = parseInt(eatNext(), 10);
|
138 | } else
|
139 |
|
140 |
|
141 | if (arg === '--js') {
|
142 | options.js = true;
|
143 | } else
|
144 |
|
145 | if (arg === '--quiet' || arg === '-q') {
|
146 | options.quiet = true;
|
147 | } else
|
148 |
|
149 | if (arg === '--config') {
|
150 | options.configFile = eatNext();
|
151 | } else
|
152 |
|
153 | if (arg === '--watch' || arg === '-w') {
|
154 | if (!options.watch) { options.watch = []; }
|
155 | options.watch.push(eatNext());
|
156 | } else
|
157 |
|
158 | if (arg === '--ignore' || arg === '-i') {
|
159 | if (!options.ignore) { options.ignore = []; }
|
160 | options.ignore.push(eatNext());
|
161 | } else
|
162 |
|
163 | if (arg === '--exitcrash') {
|
164 | options.exitcrash = true;
|
165 | } else
|
166 |
|
167 | if (arg === '--delay' || arg === '-d') {
|
168 | options.delay = parseDelay(eatNext());
|
169 | } else
|
170 |
|
171 | if (arg === '--exec' || arg === '-x') {
|
172 | options.exec = eatNext();
|
173 | } else
|
174 |
|
175 | if (arg === '--no-stdin' || arg === '-I') {
|
176 | options.stdin = false;
|
177 | } else
|
178 |
|
179 | if (arg === '--on-change-only' || arg === '-C') {
|
180 | options.runOnChangeOnly = true;
|
181 | } else
|
182 |
|
183 | if (arg === '--ext' || arg === '-e') {
|
184 | options.ext = eatNext();
|
185 | } else
|
186 |
|
187 | if (arg === '--no-colours' || arg === '--no-colors') {
|
188 | options.colours = false;
|
189 | } else
|
190 |
|
191 | if (arg === '--signal' || arg === '-s') {
|
192 | options.signal = eatNext();
|
193 | } else
|
194 |
|
195 | if (arg === '--cwd') {
|
196 | options.cwd = eatNext();
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | process.chdir(path.resolve(options.cwd));
|
202 | } else {
|
203 |
|
204 |
|
205 | return false;
|
206 | }
|
207 | }
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function parseDelay(value) {
|
217 | var millisPerSecond = 1000;
|
218 | var millis = 0;
|
219 |
|
220 | if (value.match(/^\d*ms$/)) {
|
221 |
|
222 | millis = parseInt(value, 10);
|
223 | } else {
|
224 |
|
225 | millis = parseFloat(value) * millisPerSecond;
|
226 | }
|
227 |
|
228 | return isNaN(millis) ? 0 : millis;
|
229 | }
|
230 |
|