alemonjs 2.1.76 → 2.1.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/app/router/dsl.js
CHANGED
|
@@ -72,6 +72,13 @@ function getImporterLabel(importer, index) {
|
|
|
72
72
|
function formatRouteDescription(description) {
|
|
73
73
|
return typeof description === 'string' && description.trim() ? description.trim() : undefined;
|
|
74
74
|
}
|
|
75
|
+
function formatArgReference(arg, displayIndex) {
|
|
76
|
+
const name = typeof arg.name === 'string' ? arg.name.trim() : '';
|
|
77
|
+
if (name) {
|
|
78
|
+
return `参数「${name}」`;
|
|
79
|
+
}
|
|
80
|
+
return `第${displayIndex}个参数`;
|
|
81
|
+
}
|
|
75
82
|
function formatArgRuleHint(arg, displayIndex) {
|
|
76
83
|
const rules = arg.rules ?? [];
|
|
77
84
|
const parts = [];
|
|
@@ -105,7 +112,7 @@ function formatArgRuleHint(arg, displayIndex) {
|
|
|
105
112
|
parts.push(`最大 ${typeRule.max}`);
|
|
106
113
|
}
|
|
107
114
|
const suffix = arg.description ? `,${arg.description}` : '';
|
|
108
|
-
return
|
|
115
|
+
return `${formatArgReference(arg, displayIndex)}:${parts.join(',')}${suffix}`;
|
|
109
116
|
}
|
|
110
117
|
function buildSchemaHints(schema) {
|
|
111
118
|
const args = schema?.args ?? [];
|
|
@@ -21,11 +21,15 @@ function levenshteinDistance(a, b) {
|
|
|
21
21
|
function normalizeForCompare(text) {
|
|
22
22
|
return text.replace(/^([!!/##])\s*/, '').trim();
|
|
23
23
|
}
|
|
24
|
+
function escapeRegExp(text) {
|
|
25
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
26
|
+
}
|
|
24
27
|
function shouldCheckCandidate(messageText, candidate) {
|
|
25
28
|
if (!messageText || !candidate) {
|
|
26
29
|
return false;
|
|
27
30
|
}
|
|
28
|
-
|
|
31
|
+
const candidatePattern = new RegExp(`^${escapeRegExp(candidate)}(?:\\s|$)`);
|
|
32
|
+
return candidatePattern.test(messageText);
|
|
29
33
|
}
|
|
30
34
|
function getAdaptiveMaxDistance(input) {
|
|
31
35
|
if (input.length <= 2) {
|
|
@@ -20,6 +20,24 @@ function getMaxArgs(schema) {
|
|
|
20
20
|
}
|
|
21
21
|
return args.length > 0 ? args.length : undefined;
|
|
22
22
|
}
|
|
23
|
+
function getMissingRequiredArg(rawArgs, schema) {
|
|
24
|
+
const args = schema?.args ?? [];
|
|
25
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
26
|
+
const arg = args[index];
|
|
27
|
+
const requiredRule = getRequiredRule(arg);
|
|
28
|
+
if (!requiredRule) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const rawValue = arg.rules?.some(rule => rule.type === 'rest') ? rawArgs.slice(index).join(' ').trim() : rawArgs[index];
|
|
32
|
+
if (rawValue === undefined || rawValue === '') {
|
|
33
|
+
return {
|
|
34
|
+
arg,
|
|
35
|
+
displayIndex: index + 1
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
23
41
|
function parseRange(rawValue, rule) {
|
|
24
42
|
const separators = rule.separators ?? ['-', '-'];
|
|
25
43
|
const separator = separators.find(item => rawValue.includes(item));
|
|
@@ -34,8 +52,15 @@ function parseRange(rawValue, rule) {
|
|
|
34
52
|
}
|
|
35
53
|
return { start, end };
|
|
36
54
|
}
|
|
55
|
+
function formatArgReference(argName, displayIndex) {
|
|
56
|
+
const name = String(argName ?? '').trim();
|
|
57
|
+
if (name) {
|
|
58
|
+
return `参数「${name}」`;
|
|
59
|
+
}
|
|
60
|
+
return `第${displayIndex}个参数`;
|
|
61
|
+
}
|
|
37
62
|
function validateTypedRule(rawValue, rule, context, usage) {
|
|
38
|
-
const messagePrefix =
|
|
63
|
+
const messagePrefix = formatArgReference(context.argName, context.displayIndex);
|
|
39
64
|
const normalizedValue = rule.normalizeMap?.[rawValue] ?? rawValue;
|
|
40
65
|
if (rule.pattern) {
|
|
41
66
|
rule.pattern.lastIndex = 0;
|
|
@@ -226,9 +251,10 @@ function validateRouteArgsForCommand(commandPath, rawArgs, schema) {
|
|
|
226
251
|
const maxArgs = getMaxArgs(schema);
|
|
227
252
|
const usage = schema?.usage;
|
|
228
253
|
if (rawArgs.length < minArgs) {
|
|
254
|
+
const missingRequiredArg = getMissingRequiredArg(rawArgs, schema);
|
|
229
255
|
return {
|
|
230
256
|
valid: false,
|
|
231
|
-
error: schema?.messages?.tooFewArgs ?? `至少需要 ${minArgs}
|
|
257
|
+
error: schema?.messages?.tooFewArgs ?? (missingRequiredArg ? `${formatArgReference(missingRequiredArg.arg.name, missingRequiredArg.displayIndex)}是必填的` : `至少需要 ${minArgs} 个参数`),
|
|
232
258
|
usage
|
|
233
259
|
};
|
|
234
260
|
}
|
|
@@ -252,7 +278,7 @@ function validateRouteArgsForCommand(commandPath, rawArgs, schema) {
|
|
|
252
278
|
if (requiredRule) {
|
|
253
279
|
return {
|
|
254
280
|
valid: false,
|
|
255
|
-
error: requiredRule.message ??
|
|
281
|
+
error: requiredRule.message ?? `${formatArgReference(arg.name, displayIndex)}是必填的`,
|
|
256
282
|
usage
|
|
257
283
|
};
|
|
258
284
|
}
|
|
@@ -282,7 +308,7 @@ function validateRouteArgsForCommand(commandPath, rawArgs, schema) {
|
|
|
282
308
|
if (requiredRule) {
|
|
283
309
|
return {
|
|
284
310
|
valid: false,
|
|
285
|
-
error: requiredRule.message ??
|
|
311
|
+
error: requiredRule.message ?? `${formatArgReference(arg.name, displayIndex)}是必填的`,
|
|
286
312
|
usage
|
|
287
313
|
};
|
|
288
314
|
}
|