-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathprocess-arguments.js
332 lines (270 loc) · 7.67 KB
/
process-arguments.js
1
2
3
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"use strict";
const path = require("path");
// Based on https://github.com/webpack/webpack/blob/master/lib/cli.js
// Please do not modify it
const cliAddedItems = new WeakMap();
const getObjectAndProperty = (config, schemaPath, index = 0) => {
if (!schemaPath) {
return { value: config };
}
const parts = schemaPath.split(".");
const property = parts.pop();
let current = config;
let i = 0;
for (const part of parts) {
const isArray = part.endsWith("[]");
const name = isArray ? part.slice(0, -2) : part;
let value = current[name];
if (isArray) {
// eslint-disable-next-line no-undefined
if (value === undefined) {
value = {};
current[name] = [...Array.from({ length: index }), value];
cliAddedItems.set(current[name], index + 1);
} else if (!Array.isArray(value)) {
return {
problem: {
type: "unexpected-non-array-in-path",
path: parts.slice(0, i).join("."),
},
};
} else {
let addedItems = cliAddedItems.get(value) || 0;
while (addedItems <= index) {
// eslint-disable-next-line no-undefined
value.push(undefined);
// eslint-disable-next-line no-plusplus
addedItems++;
}
cliAddedItems.set(value, addedItems);
const x = value.length - addedItems + index;
// eslint-disable-next-line no-undefined
if (value[x] === undefined) {
value[x] = {};
} else if (value[x] === null || typeof value[x] !== "object") {
return {
problem: {
type: "unexpected-non-object-in-path",
path: parts.slice(0, i).join("."),
},
};
}
value = value[x];
}
// eslint-disable-next-line no-undefined
} else if (value === undefined) {
// eslint-disable-next-line no-multi-assign
value = current[name] = {};
} else if (value === null || typeof value !== "object") {
return {
problem: {
type: "unexpected-non-object-in-path",
path: parts.slice(0, i).join("."),
},
};
}
current = value;
// eslint-disable-next-line no-plusplus
i++;
}
const value = current[property];
if (property.endsWith("[]")) {
const name = property.slice(0, -2);
// eslint-disable-next-line no-shadow
const value = current[name];
// eslint-disable-next-line no-undefined
if (value === undefined) {
// eslint-disable-next-line no-undefined
current[name] = [...Array.from({ length: index }), undefined];
cliAddedItems.set(current[name], index + 1);
// eslint-disable-next-line no-undefined
return { object: current[name], property: index, value: undefined };
} else if (!Array.isArray(value)) {
// eslint-disable-next-line no-undefined
current[name] = [value, ...Array.from({ length: index }), undefined];
cliAddedItems.set(current[name], index + 1);
// eslint-disable-next-line no-undefined
return { object: current[name], property: index + 1, value: undefined };
}
let addedItems = cliAddedItems.get(value) || 0;
while (addedItems <= index) {
// eslint-disable-next-line no-undefined
value.push(undefined);
// eslint-disable-next-line no-plusplus
addedItems++;
}
cliAddedItems.set(value, addedItems);
const x = value.length - addedItems + index;
// eslint-disable-next-line no-undefined
if (value[x] === undefined) {
value[x] = {};
} else if (value[x] === null || typeof value[x] !== "object") {
return {
problem: {
type: "unexpected-non-object-in-path",
path: schemaPath,
},
};
}
return {
object: value,
property: x,
value: value[x],
};
}
return { object: current, property, value };
};
const parseValueForArgumentConfig = (argConfig, value) => {
// eslint-disable-next-line default-case
switch (argConfig.type) {
case "string":
if (typeof value === "string") {
return value;
}
break;
case "path":
if (typeof value === "string") {
return path.resolve(value);
}
break;
case "number":
if (typeof value === "number") {
return value;
}
if (typeof value === "string" && /^[+-]?\d*(\.\d*)[eE]\d+$/) {
const n = +value;
if (!isNaN(n)) return n;
}
break;
case "boolean":
if (typeof value === "boolean") {
return value;
}
if (value === "true") {
return true;
}
if (value === "false") {
return false;
}
break;
case "RegExp":
if (value instanceof RegExp) {
return value;
}
if (typeof value === "string") {
// cspell:word yugi
const match = /^\/(.*)\/([yugi]*)$/.exec(value);
if (match && !/[^\\]\//.test(match[1])) {
return new RegExp(match[1], match[2]);
}
}
break;
case "enum":
if (argConfig.values.includes(value)) {
return value;
}
for (const item of argConfig.values) {
if (`${item}` === value) return item;
}
break;
case "reset":
if (value === true) {
return [];
}
break;
}
};
const getExpectedValue = (argConfig) => {
switch (argConfig.type) {
default:
return argConfig.type;
case "boolean":
return "true | false";
case "RegExp":
return "regular expression (example: /ab?c*/)";
case "enum":
return argConfig.values.map((v) => `${v}`).join(" | ");
case "reset":
return "true (will reset the previous value to an empty array)";
}
};
const setValue = (config, schemaPath, value, index) => {
const { problem, object, property } = getObjectAndProperty(
config,
schemaPath,
index
);
if (problem) {
return problem;
}
object[property] = value;
return null;
};
const processArgumentConfig = (argConfig, config, value, index) => {
// eslint-disable-next-line no-undefined
if (index !== undefined && !argConfig.multiple) {
return {
type: "multiple-values-unexpected",
path: argConfig.path,
};
}
const parsed = parseValueForArgumentConfig(argConfig, value);
// eslint-disable-next-line no-undefined
if (parsed === undefined) {
return {
type: "invalid-value",
path: argConfig.path,
expected: getExpectedValue(argConfig),
};
}
const problem = setValue(config, argConfig.path, parsed, index);
if (problem) {
return problem;
}
return null;
};
const processArguments = (args, config, values) => {
const problems = [];
for (const key of Object.keys(values)) {
const arg = args[key];
if (!arg) {
problems.push({
type: "unknown-argument",
path: "",
argument: key,
});
// eslint-disable-next-line no-continue
continue;
}
const processValue = (value, i) => {
const currentProblems = [];
for (const argConfig of arg.configs) {
const problem = processArgumentConfig(argConfig, config, value, i);
if (!problem) {
return;
}
currentProblems.push({
...problem,
argument: key,
value,
index: i,
});
}
problems.push(...currentProblems);
};
const value = values[key];
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
processValue(value[i], i);
}
} else {
// eslint-disable-next-line no-undefined
processValue(value, undefined);
}
}
if (problems.length === 0) {
return null;
}
return problems;
};
module.exports = processArguments;