1 | "use strict";
|
2 |
|
3 | const path = require("path");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | const cliAddedItems = new WeakMap();
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const getObjectAndProperty = (config, schemaPath, index = 0) => {
|
53 | if (!schemaPath) {
|
54 | return { value: config };
|
55 | }
|
56 |
|
57 | const parts = schemaPath.split(".");
|
58 | const property = parts.pop();
|
59 | let current = config;
|
60 | let i = 0;
|
61 |
|
62 | for (const part of parts) {
|
63 | const isArray = part.endsWith("[]");
|
64 | const name = isArray ? part.slice(0, -2) : part;
|
65 | let value = current[name];
|
66 |
|
67 | if (isArray) {
|
68 |
|
69 | if (value === undefined) {
|
70 | value = {};
|
71 | current[name] = [...Array.from({ length: index }), value];
|
72 | cliAddedItems.set(current[name], index + 1);
|
73 | } else if (!Array.isArray(value)) {
|
74 | return {
|
75 | problem: {
|
76 | type: "unexpected-non-array-in-path",
|
77 | path: parts.slice(0, i).join("."),
|
78 | },
|
79 | };
|
80 | } else {
|
81 | let addedItems = cliAddedItems.get(value) || 0;
|
82 |
|
83 | while (addedItems <= index) {
|
84 |
|
85 | value.push(undefined);
|
86 |
|
87 | addedItems++;
|
88 | }
|
89 |
|
90 | cliAddedItems.set(value, addedItems);
|
91 |
|
92 | const x = value.length - addedItems + index;
|
93 |
|
94 |
|
95 | if (value[x] === undefined) {
|
96 | value[x] = {};
|
97 | } else if (value[x] === null || typeof value[x] !== "object") {
|
98 | return {
|
99 | problem: {
|
100 | type: "unexpected-non-object-in-path",
|
101 | path: parts.slice(0, i).join("."),
|
102 | },
|
103 | };
|
104 | }
|
105 |
|
106 | value = value[x];
|
107 | }
|
108 |
|
109 | } else if (value === undefined) {
|
110 |
|
111 | value = current[name] = {};
|
112 | } else if (value === null || typeof value !== "object") {
|
113 | return {
|
114 | problem: {
|
115 | type: "unexpected-non-object-in-path",
|
116 | path: parts.slice(0, i).join("."),
|
117 | },
|
118 | };
|
119 | }
|
120 |
|
121 | current = value;
|
122 |
|
123 | i++;
|
124 | }
|
125 |
|
126 | const value = current[ (property)];
|
127 |
|
128 | if ( (property).endsWith("[]")) {
|
129 | const name = (property).slice(0, -2);
|
130 |
|
131 | const value = current[name];
|
132 |
|
133 |
|
134 | if (value === undefined) {
|
135 |
|
136 | current[name] = [...Array.from({ length: index }), undefined];
|
137 | cliAddedItems.set(current[name], index + 1);
|
138 |
|
139 |
|
140 | return { object: current[name], property: index, value: undefined };
|
141 | } else if (!Array.isArray(value)) {
|
142 |
|
143 | current[name] = [value, ...Array.from({ length: index }), undefined];
|
144 | cliAddedItems.set(current[name], index + 1);
|
145 |
|
146 |
|
147 | return { object: current[name], property: index + 1, value: undefined };
|
148 | }
|
149 |
|
150 | let addedItems = cliAddedItems.get(value) || 0;
|
151 |
|
152 | while (addedItems <= index) {
|
153 |
|
154 | value.push(undefined);
|
155 |
|
156 | addedItems++;
|
157 | }
|
158 |
|
159 | cliAddedItems.set(value, addedItems);
|
160 |
|
161 | const x = value.length - addedItems + index;
|
162 |
|
163 |
|
164 | if (value[x] === undefined) {
|
165 | value[x] = {};
|
166 | } else if (value[x] === null || typeof value[x] !== "object") {
|
167 | return {
|
168 | problem: {
|
169 | type: "unexpected-non-object-in-path",
|
170 | path: schemaPath,
|
171 | },
|
172 | };
|
173 | }
|
174 |
|
175 | return {
|
176 | object: value,
|
177 | property: x,
|
178 | value: value[x],
|
179 | };
|
180 | }
|
181 |
|
182 | return { object: current, property, value };
|
183 | };
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | const parseValueForArgumentConfig = (argConfig, value) => {
|
191 |
|
192 | switch (argConfig.type) {
|
193 | case "string":
|
194 | if (typeof value === "string") {
|
195 | return value;
|
196 | }
|
197 | break;
|
198 | case "path":
|
199 | if (typeof value === "string") {
|
200 | return path.resolve(value);
|
201 | }
|
202 | break;
|
203 | case "number":
|
204 | if (typeof value === "number") {
|
205 | return value;
|
206 | }
|
207 |
|
208 | if (typeof value === "string" && /^[+-]?\d*(\.\d*)[eE]\d+$/) {
|
209 | const n = +value;
|
210 | if (!isNaN(n)) return n;
|
211 | }
|
212 |
|
213 | break;
|
214 | case "boolean":
|
215 | if (typeof value === "boolean") {
|
216 | return value;
|
217 | }
|
218 |
|
219 | if (value === "true") {
|
220 | return true;
|
221 | }
|
222 |
|
223 | if (value === "false") {
|
224 | return false;
|
225 | }
|
226 |
|
227 | break;
|
228 | case "RegExp":
|
229 | if (value instanceof RegExp) {
|
230 | return value;
|
231 | }
|
232 |
|
233 | if (typeof value === "string") {
|
234 |
|
235 | const match = /^\/(.*)\/([yugi]*)$/.exec(value);
|
236 |
|
237 | if (match && !/[^\\]\//.test(match[1])) {
|
238 | return new RegExp(match[1], match[2]);
|
239 | }
|
240 | }
|
241 |
|
242 | break;
|
243 | case "enum":
|
244 | if ( (argConfig.values).includes(value)) {
|
245 | return value;
|
246 | }
|
247 |
|
248 | for (const item of (argConfig.values)) {
|
249 | if (`${item}` === value) return item;
|
250 | }
|
251 |
|
252 | break;
|
253 | case "reset":
|
254 | if (value === true) {
|
255 | return [];
|
256 | }
|
257 |
|
258 | break;
|
259 | }
|
260 | };
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | const getExpectedValue = (argConfig) => {
|
267 | switch (argConfig.type) {
|
268 | default:
|
269 | return argConfig.type;
|
270 | case "boolean":
|
271 | return "true | false";
|
272 | case "RegExp":
|
273 | return "regular expression (example: /ab?c*/)";
|
274 | case "enum":
|
275 | return (argConfig.values)
|
276 | .map((v) => `${v}`)
|
277 | .join(" | ");
|
278 | case "reset":
|
279 | return "true (will reset the previous value to an empty array)";
|
280 | }
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | const setValue = (config, schemaPath, value, index) => {
|
291 | const { problem, object, property } = getObjectAndProperty(
|
292 | config,
|
293 | schemaPath,
|
294 | index
|
295 | );
|
296 |
|
297 | if (problem) {
|
298 | return problem;
|
299 | }
|
300 |
|
301 | object[ (property)] = value;
|
302 |
|
303 | return null;
|
304 | };
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | const processArgumentConfig = (argConfig, config, value, index) => {
|
314 |
|
315 | if (index !== undefined && !argConfig.multiple) {
|
316 | return {
|
317 | type: "multiple-values-unexpected",
|
318 | path: argConfig.path,
|
319 | };
|
320 | }
|
321 |
|
322 | const parsed = parseValueForArgumentConfig(argConfig, value);
|
323 |
|
324 |
|
325 | if (parsed === undefined) {
|
326 | return {
|
327 | type: "invalid-value",
|
328 | path: argConfig.path,
|
329 | expected: getExpectedValue(argConfig),
|
330 | };
|
331 | }
|
332 |
|
333 | const problem = setValue(config, argConfig.path, parsed, index);
|
334 |
|
335 | if (problem) {
|
336 | return problem;
|
337 | }
|
338 |
|
339 | return null;
|
340 | };
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | const processArguments = (args, config, values) => {
|
349 | |
350 |
|
351 |
|
352 | const problems = [];
|
353 |
|
354 | for (const key of Object.keys(values)) {
|
355 | const arg = args[key];
|
356 |
|
357 | if (!arg) {
|
358 | problems.push({
|
359 | type: "unknown-argument",
|
360 | path: "",
|
361 | argument: key,
|
362 | });
|
363 |
|
364 |
|
365 | continue;
|
366 | }
|
367 |
|
368 | |
369 |
|
370 |
|
371 |
|
372 | const processValue = (value, i) => {
|
373 | const currentProblems = [];
|
374 |
|
375 | for (const argConfig of arg.configs) {
|
376 | const problem = processArgumentConfig(argConfig, config, value, i);
|
377 |
|
378 | if (!problem) {
|
379 | return;
|
380 | }
|
381 |
|
382 | currentProblems.push({
|
383 | ...problem,
|
384 | argument: key,
|
385 | value,
|
386 | index: i,
|
387 | });
|
388 | }
|
389 |
|
390 | problems.push(...currentProblems);
|
391 | };
|
392 |
|
393 | const value = values[key];
|
394 |
|
395 | if (Array.isArray(value)) {
|
396 | for (let i = 0; i < value.length; i++) {
|
397 | processValue(value[i], i);
|
398 | }
|
399 | } else {
|
400 |
|
401 | processValue(value, undefined);
|
402 | }
|
403 | }
|
404 |
|
405 | if (problems.length === 0) {
|
406 | return null;
|
407 | }
|
408 |
|
409 | return problems;
|
410 | };
|
411 |
|
412 | module.exports = processArguments;
|