@wkgeo/cli 2.0.3 → 2.1.0
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/CHANGELOG.md +10 -2
- package/README.md +7 -7
- package/SKILL.md +1038 -1038
- package/lib/api.mjs +351 -351
- package/lib/args.mjs +235 -235
- package/lib/build.mjs +1047 -0
- package/lib/cms.mjs +328 -328
- package/lib/commands/auth.mjs +1280 -1280
- package/lib/commands/data.mjs +694 -694
- package/lib/commands/index.mjs +15 -1
- package/lib/commands/init.mjs +111 -111
- package/lib/commands/orders.mjs +691 -691
- package/lib/commands/site.mjs +82 -0
- package/lib/commands/siteops.mjs +248 -0
- package/lib/commands/sites.mjs +968 -801
- package/lib/commands/spec.mjs +72 -72
- package/lib/commands/template.mjs +1484 -1484
- package/lib/config.mjs +57 -4
- package/lib/errors.mjs +446 -336
- package/lib/manifest-apply.mjs +1650 -1650
- package/lib/manifest-schema.json +480 -480
- package/lib/manifest.mjs +1106 -1106
- package/lib/runtime.mjs +767 -654
- package/lib/site-create.mjs +537 -537
- package/lib/site-credentials.mjs +157 -0
- package/lib/site-meta.mjs +474 -0
- package/lib/site-spec.mjs +302 -302
- package/lib/site-template.mjs +405 -0
- package/lib/template-gen.mjs +1354 -1354
- package/lib/template-publish.mjs +696 -696
- package/lib/templates-sync.mjs +676 -644
- package/lib/workspace-sync.mjs +386 -0
- package/package.json +56 -56
- package/spec/SITE-SPEC.md +198 -198
package/lib/args.mjs
CHANGED
|
@@ -1,235 +1,235 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 命令行解析(手写,零依赖)。
|
|
3
|
-
*
|
|
4
|
-
* 为什么手写而不是 yargs/commander:
|
|
5
|
-
* 1. 老板要求尽量零第三方依赖(npx 一行跑、审计面小)。
|
|
6
|
-
* 2. 我们只需要"多词命令 + 少量标志",手写 100 行就够,而且**能精确控制报错文案**(中文)。
|
|
7
|
-
*
|
|
8
|
-
* 支持的写法:
|
|
9
|
-
* wkgeo sites create --name "我的站" --template <id>
|
|
10
|
-
* wkgeo orders create --product=storage_mb --units 1
|
|
11
|
-
* wkgeo login --no-browser # --no-xxx → xxx=false
|
|
12
|
-
* wkgeo wallet -j # 短标志:-h/-v/-y/-j
|
|
13
|
-
*
|
|
14
|
-
* ⚠️ 已知取舍:`--name -abc` 这种"值本身以 - 开头"的写法会被当成布尔标志。
|
|
15
|
-
* 需要时用 `--name=-abc`。这也是大多数 CLI 的行为,不值得为它加复杂度。
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { UsageError } from './errors.mjs';
|
|
19
|
-
|
|
20
|
-
/** 全局标志:任何命令都接受(写在 spec 之外,避免每个命令重复声明) */
|
|
21
|
-
export const GLOBAL_FLAGS = {
|
|
22
|
-
base: { type: 'string', desc: 'API 基址(默认线上网关,可用 WKONGAI_BASE 覆盖)' },
|
|
23
|
-
/**
|
|
24
|
-
* CMS 面是**另一个云函数 / 另一个域名**(新写法 `https://api.wkgeo.cn/cms`),
|
|
25
|
-
* 所以单独给一个基址开关:联调 / 换环境时不用去改平台面的 `--base`。
|
|
26
|
-
*/
|
|
27
|
-
'cms-base': { type: 'string', desc: 'CMS 接口基址(默认 https://api.wkgeo.cn/cms,可用 WKONGAI_CMS_BASE 覆盖)' },
|
|
28
|
-
'web-base': { type: 'string', desc: '前端基址(授权页兜底地址)' },
|
|
29
|
-
'client-id': { type: 'string', desc: 'OAuth 客户端 id(默认 wkgeo-cli)' },
|
|
30
|
-
scope: { type: 'string', desc: '申请的能力范围(空格分隔)' },
|
|
31
|
-
credentials: { type: 'string', desc: '凭据文件路径(默认 ~/.wkongai/credentials.json)' },
|
|
32
|
-
'callback-port': { type: 'string', desc: '本地回调端口(默认 53682)' },
|
|
33
|
-
'callback-host': { type: 'string', desc: '本地回调监听地址(**只允许回环**,默认 127.0.0.1)' },
|
|
34
|
-
/**
|
|
35
|
-
* ⚠️ 单位是**秒**(不是毫秒)—— 原来按毫秒解释,`--timeout 600` 变成 600 毫秒、
|
|
36
|
-
* 0.8 秒就超时,用户按直觉写"600 秒"反而必然失败;`--help` 里当时也没写单位。
|
|
37
|
-
* 现在:纯数字按秒,也接受显式后缀 `600s` / `500ms`;非法值明确报错。
|
|
38
|
-
*/
|
|
39
|
-
timeout: { type: 'string', desc: '等待授权回调的超时,**默认单位秒**(--timeout 600 = 600 秒;也支持 600s / 500ms;默认 300 秒)' },
|
|
40
|
-
'http-timeout': { type: 'string', desc: '单次 HTTP 请求超时毫秒数(默认 30000)' },
|
|
41
|
-
json: { type: 'boolean', desc: '以 JSON 输出(默认:非 TTY 时自动 JSON)' },
|
|
42
|
-
text: { type: 'boolean', desc: '强制人类可读输出(管道里默认是 JSON,用它覆盖)' },
|
|
43
|
-
color: { type: 'boolean', desc: '彩色输出(--no-color 关闭)' },
|
|
44
|
-
verbose: { type: 'boolean', desc: '额外诊断信息(原始响应会先脱敏)' },
|
|
45
|
-
help: { type: 'boolean', desc: '显示帮助' },
|
|
46
|
-
version: { type: 'boolean', desc: '显示版本' },
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const SHORT_FLAGS = { h: 'help', v: 'version', j: 'json' };
|
|
50
|
-
|
|
51
|
-
/** kebab-case → camelCase:`client-id` → `clientId` */
|
|
52
|
-
export function camel(key) {
|
|
53
|
-
return String(key).replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 通用解析:只做"切成 positionals + flags",不校验具体命令。
|
|
58
|
-
* @returns {{positionals:string[], flags:Record<string, string|boolean>}}
|
|
59
|
-
*/
|
|
60
|
-
export function parseArgv(argv = []) {
|
|
61
|
-
const flags = {};
|
|
62
|
-
const positionals = [];
|
|
63
|
-
for (let i = 0; i < argv.length; i++) {
|
|
64
|
-
const a = String(argv[i]);
|
|
65
|
-
if (a === '--') {
|
|
66
|
-
positionals.push(...argv.slice(i + 1).map(String));
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
if (a.startsWith('--')) {
|
|
70
|
-
const eq = a.indexOf('=');
|
|
71
|
-
let key = eq === -1 ? a.slice(2) : a.slice(2, eq);
|
|
72
|
-
let val = eq === -1 ? undefined : a.slice(eq + 1);
|
|
73
|
-
if (!key) throw new UsageError('参数格式错误:`--` 后面没有标志名');
|
|
74
|
-
if (val === undefined) {
|
|
75
|
-
const next = argv[i + 1];
|
|
76
|
-
// 下一个词不是标志 → 当作本标志的值;否则本标志是布尔
|
|
77
|
-
if (next !== undefined && !String(next).startsWith('-')) {
|
|
78
|
-
val = String(next);
|
|
79
|
-
i++;
|
|
80
|
-
} else {
|
|
81
|
-
val = true;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (key.startsWith('no-') && key.length > 3) flags[key.slice(3)] = false;
|
|
85
|
-
else flags[key] = val;
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (/^-[A-Za-z]$/.test(a)) {
|
|
89
|
-
const long = SHORT_FLAGS[a.slice(1)];
|
|
90
|
-
if (!long) throw new UsageError(`无法识别的短参数:${a}`);
|
|
91
|
-
flags[long] = true;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (a.startsWith('-') && a !== '-') {
|
|
95
|
-
throw new UsageError(`无法识别的参数:${a}`);
|
|
96
|
-
}
|
|
97
|
-
positionals.push(a);
|
|
98
|
-
}
|
|
99
|
-
return { positionals, flags };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 按命令 spec 校验/转换标志。
|
|
104
|
-
* 未知标志直接报错(而不是静默忽略):拼错 `--nmae` 时静默忽略会让用户以为成功了,
|
|
105
|
-
* 这是最坑的一类"看起来成功其实没生效"。
|
|
106
|
-
*/
|
|
107
|
-
export function coerceFlags(spec, flags) {
|
|
108
|
-
const defs = { ...GLOBAL_FLAGS, ...(spec.flags || {}) };
|
|
109
|
-
/**
|
|
110
|
-
* ⚠️ 特例:`--no-xxx` 在 `parseArgv` 里被统一当成"把 xxx 关掉"(`--no-color` / `--no-browser`),
|
|
111
|
-
* 但有的标志**名字本身就以 `no-` 开头**,而且是个**正向开关** —— 例如
|
|
112
|
-
* `login --no-wait`("不要等待,申请完码就退出")。
|
|
113
|
-
*
|
|
114
|
-
* 解析阶段拿不到命令 spec、无法区分这两种情况,所以会把它变成 `wait:false`。
|
|
115
|
-
* 这里按"注册表"还原:只有当 spec 里**确实定义了** `no-wait`、
|
|
116
|
-
* 且**没有人**定义 `wait` 时,`wait:false` 才只可能来自用户写的 `--no-wait`。
|
|
117
|
-
* (踩过的坑:不加这段的话 `login --no-wait` 会报"不认识的参数:--wait",
|
|
118
|
-
* 而 `--wait` 在帮助里根本不存在,用户完全看不懂自己写错了什么。)
|
|
119
|
-
*/
|
|
120
|
-
const restored = { ...flags };
|
|
121
|
-
for (const key of Object.keys(defs)) {
|
|
122
|
-
if (!key.startsWith('no-')) continue;
|
|
123
|
-
const negated = key.slice(3);
|
|
124
|
-
if (defs[negated] === undefined && restored[key] === undefined && restored[negated] === false) {
|
|
125
|
-
restored[key] = true;
|
|
126
|
-
delete restored[negated];
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const out = {};
|
|
131
|
-
const unknown = [];
|
|
132
|
-
for (const [key, val] of Object.entries(restored)) {
|
|
133
|
-
const def = defs[key];
|
|
134
|
-
if (!def) {
|
|
135
|
-
unknown.push(key);
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
const name = camel(key);
|
|
139
|
-
if (def.type === 'boolean') {
|
|
140
|
-
out[name] = val === false ? false : val === true ? true : !/^(false|0|no)$/i.test(String(val));
|
|
141
|
-
} else if (def.type === 'number') {
|
|
142
|
-
const n = Number(val);
|
|
143
|
-
if (!Number.isFinite(n)) throw new UsageError(`参数 --${key} 需要数字,收到:${val}`);
|
|
144
|
-
out[name] = n;
|
|
145
|
-
} else {
|
|
146
|
-
if (val === true) throw new UsageError(`参数 --${key} 缺少值(写成 --${key}=值 也可以)`);
|
|
147
|
-
out[name] = String(val);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (unknown.length) {
|
|
151
|
-
const usable = Object.keys(defs).sort().join(' / ');
|
|
152
|
-
throw new UsageError(`不认识的参数:${unknown.map((u) => '--' + u).join(', ')}\n本命令可用参数:${usable}`);
|
|
153
|
-
}
|
|
154
|
-
for (const [key, def] of Object.entries(spec.flags || {})) {
|
|
155
|
-
if (def.required && out[camel(key)] === undefined) {
|
|
156
|
-
throw new UsageError(`缺少必填参数 --${key}${def.desc ? `(${def.desc})` : ''}`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return out;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 位置参数校验:按顺序、必填在前。
|
|
164
|
-
*
|
|
165
|
-
* 最后一个 arg 可标 `variadic: true`(如 `<文件…>`):
|
|
166
|
-
* 它会把剩下的位置参数全部收进一个**数组**,不再报「参数过多」。
|
|
167
|
-
*/
|
|
168
|
-
export function checkPositionals(spec, rest) {
|
|
169
|
-
const wanted = spec.args || [];
|
|
170
|
-
const last = wanted[wanted.length - 1];
|
|
171
|
-
const variadic = Boolean(last && last.variadic);
|
|
172
|
-
const fixed = variadic ? wanted.length - 1 : wanted.length;
|
|
173
|
-
if (!variadic && rest.length > fixed) {
|
|
174
|
-
throw new UsageError(`参数过多:${rest.slice(fixed).map((r) => `"${r}"`).join(' ')}\n用法:${spec.usage}`);
|
|
175
|
-
}
|
|
176
|
-
for (let i = 0; i < fixed; i++) {
|
|
177
|
-
if (wanted[i].required && (rest[i] === undefined || rest[i] === '')) {
|
|
178
|
-
throw new UsageError(`缺少参数 <${wanted[i].name}>${wanted[i].desc ? `(${wanted[i].desc})` : ''}\n用法:${spec.usage}`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
if (variadic) {
|
|
182
|
-
const tail = rest.slice(fixed);
|
|
183
|
-
if (last.required && tail.length === 0) {
|
|
184
|
-
throw new UsageError(`缺少参数 <${last.name}>${last.desc ? `(${last.desc})` : ''}\n用法:${spec.usage}`);
|
|
185
|
-
}
|
|
186
|
-
const out = {};
|
|
187
|
-
for (let i = 0; i < fixed; i++) {
|
|
188
|
-
if (rest[i] !== undefined) out[wanted[i].name] = rest[i];
|
|
189
|
-
}
|
|
190
|
-
out[last.name] = tail;
|
|
191
|
-
return out;
|
|
192
|
-
}
|
|
193
|
-
const out = {};
|
|
194
|
-
wanted.forEach((w, i) => {
|
|
195
|
-
if (rest[i] !== undefined) out[w.name] = rest[i];
|
|
196
|
-
});
|
|
197
|
-
return out;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 从位置参数里认出命令(最长前缀匹配:支持 `publish targets save` 这类三词命令)。
|
|
202
|
-
* 先按 4 词、再 3 词、再 2 词、再 1 词试,命中即返回,剩下的位置参数归命令自己。
|
|
203
|
-
*
|
|
204
|
-
* ★ 2026-10-28:两词 → 更长命令(当时最长四词)。顺带补一个坑:
|
|
205
|
-
* `wkgeo publish targets`(少打最后一个词)原本会退化命中两词命令 `publish targets`,
|
|
206
|
-
* 于是 "save" 被当成位置参数传给一条完全不相干的命令 —— 报错文案极其误导。
|
|
207
|
-
* 所以这里先查"打了一半的更长命令",有就直接报"命令不完整"。
|
|
208
|
-
* @param {Array} commands 命令 spec 数组(必须带 name,如 'sites create')
|
|
209
|
-
*/
|
|
210
|
-
export function resolveCommand(commands, positionals) {
|
|
211
|
-
const byName = new Map(commands.map((c) => [c.name, c]));
|
|
212
|
-
const typed = positionals.join(' ');
|
|
213
|
-
if (!byName.has(typed) && positionals.length >= 2) {
|
|
214
|
-
const partial = commands
|
|
215
|
-
.filter((c) => c.name.startsWith(`${typed} `))
|
|
216
|
-
.map((c) => c.name);
|
|
217
|
-
if (partial.length) {
|
|
218
|
-
throw new UsageError(`命令不完整:"${typed}" 还差子命令\n可用:${partial.join(' / ')}`);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
const maxWords = Math.min(4, positionals.length);
|
|
222
|
-
for (let n = maxWords; n >= 1; n -= 1) {
|
|
223
|
-
const name = positionals.slice(0, n).join(' ');
|
|
224
|
-
if (byName.has(name)) return { spec: byName.get(name), rest: positionals.slice(n) };
|
|
225
|
-
}
|
|
226
|
-
// 命令名不完整(比如只打了 `sites`):给出该分组下的可用命令
|
|
227
|
-
const group = positionals[0];
|
|
228
|
-
if (group) {
|
|
229
|
-
const siblings = commands.filter((c) => c.name.startsWith(`${group} `)).map((c) => c.name);
|
|
230
|
-
if (siblings.length) {
|
|
231
|
-
throw new UsageError(`命令不完整:"${group}" 需要子命令\n可用:${siblings.join(' / ')}`);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 命令行解析(手写,零依赖)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么手写而不是 yargs/commander:
|
|
5
|
+
* 1. 老板要求尽量零第三方依赖(npx 一行跑、审计面小)。
|
|
6
|
+
* 2. 我们只需要"多词命令 + 少量标志",手写 100 行就够,而且**能精确控制报错文案**(中文)。
|
|
7
|
+
*
|
|
8
|
+
* 支持的写法:
|
|
9
|
+
* wkgeo sites create --name "我的站" --template <id>
|
|
10
|
+
* wkgeo orders create --product=storage_mb --units 1
|
|
11
|
+
* wkgeo login --no-browser # --no-xxx → xxx=false
|
|
12
|
+
* wkgeo wallet -j # 短标志:-h/-v/-y/-j
|
|
13
|
+
*
|
|
14
|
+
* ⚠️ 已知取舍:`--name -abc` 这种"值本身以 - 开头"的写法会被当成布尔标志。
|
|
15
|
+
* 需要时用 `--name=-abc`。这也是大多数 CLI 的行为,不值得为它加复杂度。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { UsageError } from './errors.mjs';
|
|
19
|
+
|
|
20
|
+
/** 全局标志:任何命令都接受(写在 spec 之外,避免每个命令重复声明) */
|
|
21
|
+
export const GLOBAL_FLAGS = {
|
|
22
|
+
base: { type: 'string', desc: 'API 基址(默认线上网关,可用 WKONGAI_BASE 覆盖)' },
|
|
23
|
+
/**
|
|
24
|
+
* CMS 面是**另一个云函数 / 另一个域名**(新写法 `https://api.wkgeo.cn/cms`),
|
|
25
|
+
* 所以单独给一个基址开关:联调 / 换环境时不用去改平台面的 `--base`。
|
|
26
|
+
*/
|
|
27
|
+
'cms-base': { type: 'string', desc: 'CMS 接口基址(默认 https://api.wkgeo.cn/cms,可用 WKONGAI_CMS_BASE 覆盖)' },
|
|
28
|
+
'web-base': { type: 'string', desc: '前端基址(授权页兜底地址)' },
|
|
29
|
+
'client-id': { type: 'string', desc: 'OAuth 客户端 id(默认 wkgeo-cli)' },
|
|
30
|
+
scope: { type: 'string', desc: '申请的能力范围(空格分隔)' },
|
|
31
|
+
credentials: { type: 'string', desc: '凭据文件路径(默认 ~/.wkongai/credentials.json)' },
|
|
32
|
+
'callback-port': { type: 'string', desc: '本地回调端口(默认 53682)' },
|
|
33
|
+
'callback-host': { type: 'string', desc: '本地回调监听地址(**只允许回环**,默认 127.0.0.1)' },
|
|
34
|
+
/**
|
|
35
|
+
* ⚠️ 单位是**秒**(不是毫秒)—— 原来按毫秒解释,`--timeout 600` 变成 600 毫秒、
|
|
36
|
+
* 0.8 秒就超时,用户按直觉写"600 秒"反而必然失败;`--help` 里当时也没写单位。
|
|
37
|
+
* 现在:纯数字按秒,也接受显式后缀 `600s` / `500ms`;非法值明确报错。
|
|
38
|
+
*/
|
|
39
|
+
timeout: { type: 'string', desc: '等待授权回调的超时,**默认单位秒**(--timeout 600 = 600 秒;也支持 600s / 500ms;默认 300 秒)' },
|
|
40
|
+
'http-timeout': { type: 'string', desc: '单次 HTTP 请求超时毫秒数(默认 30000)' },
|
|
41
|
+
json: { type: 'boolean', desc: '以 JSON 输出(默认:非 TTY 时自动 JSON)' },
|
|
42
|
+
text: { type: 'boolean', desc: '强制人类可读输出(管道里默认是 JSON,用它覆盖)' },
|
|
43
|
+
color: { type: 'boolean', desc: '彩色输出(--no-color 关闭)' },
|
|
44
|
+
verbose: { type: 'boolean', desc: '额外诊断信息(原始响应会先脱敏)' },
|
|
45
|
+
help: { type: 'boolean', desc: '显示帮助' },
|
|
46
|
+
version: { type: 'boolean', desc: '显示版本' },
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const SHORT_FLAGS = { h: 'help', v: 'version', j: 'json' };
|
|
50
|
+
|
|
51
|
+
/** kebab-case → camelCase:`client-id` → `clientId` */
|
|
52
|
+
export function camel(key) {
|
|
53
|
+
return String(key).replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 通用解析:只做"切成 positionals + flags",不校验具体命令。
|
|
58
|
+
* @returns {{positionals:string[], flags:Record<string, string|boolean>}}
|
|
59
|
+
*/
|
|
60
|
+
export function parseArgv(argv = []) {
|
|
61
|
+
const flags = {};
|
|
62
|
+
const positionals = [];
|
|
63
|
+
for (let i = 0; i < argv.length; i++) {
|
|
64
|
+
const a = String(argv[i]);
|
|
65
|
+
if (a === '--') {
|
|
66
|
+
positionals.push(...argv.slice(i + 1).map(String));
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
if (a.startsWith('--')) {
|
|
70
|
+
const eq = a.indexOf('=');
|
|
71
|
+
let key = eq === -1 ? a.slice(2) : a.slice(2, eq);
|
|
72
|
+
let val = eq === -1 ? undefined : a.slice(eq + 1);
|
|
73
|
+
if (!key) throw new UsageError('参数格式错误:`--` 后面没有标志名');
|
|
74
|
+
if (val === undefined) {
|
|
75
|
+
const next = argv[i + 1];
|
|
76
|
+
// 下一个词不是标志 → 当作本标志的值;否则本标志是布尔
|
|
77
|
+
if (next !== undefined && !String(next).startsWith('-')) {
|
|
78
|
+
val = String(next);
|
|
79
|
+
i++;
|
|
80
|
+
} else {
|
|
81
|
+
val = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (key.startsWith('no-') && key.length > 3) flags[key.slice(3)] = false;
|
|
85
|
+
else flags[key] = val;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (/^-[A-Za-z]$/.test(a)) {
|
|
89
|
+
const long = SHORT_FLAGS[a.slice(1)];
|
|
90
|
+
if (!long) throw new UsageError(`无法识别的短参数:${a}`);
|
|
91
|
+
flags[long] = true;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (a.startsWith('-') && a !== '-') {
|
|
95
|
+
throw new UsageError(`无法识别的参数:${a}`);
|
|
96
|
+
}
|
|
97
|
+
positionals.push(a);
|
|
98
|
+
}
|
|
99
|
+
return { positionals, flags };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 按命令 spec 校验/转换标志。
|
|
104
|
+
* 未知标志直接报错(而不是静默忽略):拼错 `--nmae` 时静默忽略会让用户以为成功了,
|
|
105
|
+
* 这是最坑的一类"看起来成功其实没生效"。
|
|
106
|
+
*/
|
|
107
|
+
export function coerceFlags(spec, flags) {
|
|
108
|
+
const defs = { ...GLOBAL_FLAGS, ...(spec.flags || {}) };
|
|
109
|
+
/**
|
|
110
|
+
* ⚠️ 特例:`--no-xxx` 在 `parseArgv` 里被统一当成"把 xxx 关掉"(`--no-color` / `--no-browser`),
|
|
111
|
+
* 但有的标志**名字本身就以 `no-` 开头**,而且是个**正向开关** —— 例如
|
|
112
|
+
* `login --no-wait`("不要等待,申请完码就退出")。
|
|
113
|
+
*
|
|
114
|
+
* 解析阶段拿不到命令 spec、无法区分这两种情况,所以会把它变成 `wait:false`。
|
|
115
|
+
* 这里按"注册表"还原:只有当 spec 里**确实定义了** `no-wait`、
|
|
116
|
+
* 且**没有人**定义 `wait` 时,`wait:false` 才只可能来自用户写的 `--no-wait`。
|
|
117
|
+
* (踩过的坑:不加这段的话 `login --no-wait` 会报"不认识的参数:--wait",
|
|
118
|
+
* 而 `--wait` 在帮助里根本不存在,用户完全看不懂自己写错了什么。)
|
|
119
|
+
*/
|
|
120
|
+
const restored = { ...flags };
|
|
121
|
+
for (const key of Object.keys(defs)) {
|
|
122
|
+
if (!key.startsWith('no-')) continue;
|
|
123
|
+
const negated = key.slice(3);
|
|
124
|
+
if (defs[negated] === undefined && restored[key] === undefined && restored[negated] === false) {
|
|
125
|
+
restored[key] = true;
|
|
126
|
+
delete restored[negated];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const out = {};
|
|
131
|
+
const unknown = [];
|
|
132
|
+
for (const [key, val] of Object.entries(restored)) {
|
|
133
|
+
const def = defs[key];
|
|
134
|
+
if (!def) {
|
|
135
|
+
unknown.push(key);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const name = camel(key);
|
|
139
|
+
if (def.type === 'boolean') {
|
|
140
|
+
out[name] = val === false ? false : val === true ? true : !/^(false|0|no)$/i.test(String(val));
|
|
141
|
+
} else if (def.type === 'number') {
|
|
142
|
+
const n = Number(val);
|
|
143
|
+
if (!Number.isFinite(n)) throw new UsageError(`参数 --${key} 需要数字,收到:${val}`);
|
|
144
|
+
out[name] = n;
|
|
145
|
+
} else {
|
|
146
|
+
if (val === true) throw new UsageError(`参数 --${key} 缺少值(写成 --${key}=值 也可以)`);
|
|
147
|
+
out[name] = String(val);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (unknown.length) {
|
|
151
|
+
const usable = Object.keys(defs).sort().join(' / ');
|
|
152
|
+
throw new UsageError(`不认识的参数:${unknown.map((u) => '--' + u).join(', ')}\n本命令可用参数:${usable}`);
|
|
153
|
+
}
|
|
154
|
+
for (const [key, def] of Object.entries(spec.flags || {})) {
|
|
155
|
+
if (def.required && out[camel(key)] === undefined) {
|
|
156
|
+
throw new UsageError(`缺少必填参数 --${key}${def.desc ? `(${def.desc})` : ''}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 位置参数校验:按顺序、必填在前。
|
|
164
|
+
*
|
|
165
|
+
* 最后一个 arg 可标 `variadic: true`(如 `<文件…>`):
|
|
166
|
+
* 它会把剩下的位置参数全部收进一个**数组**,不再报「参数过多」。
|
|
167
|
+
*/
|
|
168
|
+
export function checkPositionals(spec, rest) {
|
|
169
|
+
const wanted = spec.args || [];
|
|
170
|
+
const last = wanted[wanted.length - 1];
|
|
171
|
+
const variadic = Boolean(last && last.variadic);
|
|
172
|
+
const fixed = variadic ? wanted.length - 1 : wanted.length;
|
|
173
|
+
if (!variadic && rest.length > fixed) {
|
|
174
|
+
throw new UsageError(`参数过多:${rest.slice(fixed).map((r) => `"${r}"`).join(' ')}\n用法:${spec.usage}`);
|
|
175
|
+
}
|
|
176
|
+
for (let i = 0; i < fixed; i++) {
|
|
177
|
+
if (wanted[i].required && (rest[i] === undefined || rest[i] === '')) {
|
|
178
|
+
throw new UsageError(`缺少参数 <${wanted[i].name}>${wanted[i].desc ? `(${wanted[i].desc})` : ''}\n用法:${spec.usage}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (variadic) {
|
|
182
|
+
const tail = rest.slice(fixed);
|
|
183
|
+
if (last.required && tail.length === 0) {
|
|
184
|
+
throw new UsageError(`缺少参数 <${last.name}>${last.desc ? `(${last.desc})` : ''}\n用法:${spec.usage}`);
|
|
185
|
+
}
|
|
186
|
+
const out = {};
|
|
187
|
+
for (let i = 0; i < fixed; i++) {
|
|
188
|
+
if (rest[i] !== undefined) out[wanted[i].name] = rest[i];
|
|
189
|
+
}
|
|
190
|
+
out[last.name] = tail;
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
const out = {};
|
|
194
|
+
wanted.forEach((w, i) => {
|
|
195
|
+
if (rest[i] !== undefined) out[w.name] = rest[i];
|
|
196
|
+
});
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 从位置参数里认出命令(最长前缀匹配:支持 `publish targets save` 这类三词命令)。
|
|
202
|
+
* 先按 4 词、再 3 词、再 2 词、再 1 词试,命中即返回,剩下的位置参数归命令自己。
|
|
203
|
+
*
|
|
204
|
+
* ★ 2026-10-28:两词 → 更长命令(当时最长四词)。顺带补一个坑:
|
|
205
|
+
* `wkgeo publish targets`(少打最后一个词)原本会退化命中两词命令 `publish targets`,
|
|
206
|
+
* 于是 "save" 被当成位置参数传给一条完全不相干的命令 —— 报错文案极其误导。
|
|
207
|
+
* 所以这里先查"打了一半的更长命令",有就直接报"命令不完整"。
|
|
208
|
+
* @param {Array} commands 命令 spec 数组(必须带 name,如 'sites create')
|
|
209
|
+
*/
|
|
210
|
+
export function resolveCommand(commands, positionals) {
|
|
211
|
+
const byName = new Map(commands.map((c) => [c.name, c]));
|
|
212
|
+
const typed = positionals.join(' ');
|
|
213
|
+
if (!byName.has(typed) && positionals.length >= 2) {
|
|
214
|
+
const partial = commands
|
|
215
|
+
.filter((c) => c.name.startsWith(`${typed} `))
|
|
216
|
+
.map((c) => c.name);
|
|
217
|
+
if (partial.length) {
|
|
218
|
+
throw new UsageError(`命令不完整:"${typed}" 还差子命令\n可用:${partial.join(' / ')}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const maxWords = Math.min(4, positionals.length);
|
|
222
|
+
for (let n = maxWords; n >= 1; n -= 1) {
|
|
223
|
+
const name = positionals.slice(0, n).join(' ');
|
|
224
|
+
if (byName.has(name)) return { spec: byName.get(name), rest: positionals.slice(n) };
|
|
225
|
+
}
|
|
226
|
+
// 命令名不完整(比如只打了 `sites`):给出该分组下的可用命令
|
|
227
|
+
const group = positionals[0];
|
|
228
|
+
if (group) {
|
|
229
|
+
const siblings = commands.filter((c) => c.name.startsWith(`${group} `)).map((c) => c.name);
|
|
230
|
+
if (siblings.length) {
|
|
231
|
+
throw new UsageError(`命令不完整:"${group}" 需要子命令\n可用:${siblings.join(' / ')}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
}
|