ylcwf 0.0.0-stage → 0.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 +16 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/dist/index.js +1480 -0
- package/index.js +3 -0
- package/package.json +44 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1480 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { isMap, isSeq, parseDocument } from "yaml";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import picocolors from "picocolors";
|
|
7
|
+
import * as __rspack_external__clack_prompts_d2d5c36d from "@clack/prompts";
|
|
8
|
+
const errors_EXIT = {
|
|
9
|
+
OK: 0,
|
|
10
|
+
INVALID: 1,
|
|
11
|
+
USAGE: 2,
|
|
12
|
+
NOT_FOUND: 3
|
|
13
|
+
};
|
|
14
|
+
class errors_CliError extends Error {
|
|
15
|
+
exitCode;
|
|
16
|
+
constructor(message, exitCode){
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'CliError';
|
|
19
|
+
this.exitCode = exitCode;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function printJson(value) {
|
|
23
|
+
writeFileSync(process.stdout.fd, `${JSON.stringify(value, null, 2)}\n`);
|
|
24
|
+
}
|
|
25
|
+
function printLine(text = '') {
|
|
26
|
+
writeFileSync(process.stdout.fd, `${text}\n`);
|
|
27
|
+
}
|
|
28
|
+
function printError(text) {
|
|
29
|
+
writeFileSync(process.stderr.fd, `${text}\n`);
|
|
30
|
+
}
|
|
31
|
+
const CONFIG_DIR = join(homedir(), '.ylcwf');
|
|
32
|
+
const DEFAULT_CONFIG_PATH = join(CONFIG_DIR, 'config.yaml');
|
|
33
|
+
function resolveConfigPath(explicit) {
|
|
34
|
+
const fromFlag = explicit?.trim();
|
|
35
|
+
if (void 0 !== fromFlag && fromFlag.length > 0) return isAbsolute(fromFlag) ? fromFlag : resolve(process.cwd(), fromFlag);
|
|
36
|
+
const fromEnv = process.env['YLCWF_CONFIG']?.trim();
|
|
37
|
+
if (void 0 !== fromEnv && fromEnv.length > 0) return isAbsolute(fromEnv) ? fromEnv : resolve(process.cwd(), fromEnv);
|
|
38
|
+
return DEFAULT_CONFIG_PATH;
|
|
39
|
+
}
|
|
40
|
+
function backupsDir(configPath) {
|
|
41
|
+
return join(dirname(configPath), 'backups');
|
|
42
|
+
}
|
|
43
|
+
const DASH_PATTERN = /[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D_-]+/g;
|
|
44
|
+
function normalizeName(input) {
|
|
45
|
+
return input.normalize('NFKC').replace(/\s+/g, '').replace(DASH_PATTERN, '-').replace(/^-+/, '').replace(/-+$/, '').toLowerCase();
|
|
46
|
+
}
|
|
47
|
+
function editDistance(a, b) {
|
|
48
|
+
if (a === b) return 0;
|
|
49
|
+
if (0 === a.length) return b.length;
|
|
50
|
+
if (0 === b.length) return a.length;
|
|
51
|
+
let previous = Array.from({
|
|
52
|
+
length: b.length + 1
|
|
53
|
+
}, (_, index)=>index);
|
|
54
|
+
for(let i = 1; i <= a.length; i += 1){
|
|
55
|
+
const current = [
|
|
56
|
+
i
|
|
57
|
+
];
|
|
58
|
+
for(let j = 1; j <= b.length; j += 1){
|
|
59
|
+
const cost = a.charAt(i - 1) === b.charAt(j - 1) ? 0 : 1;
|
|
60
|
+
const deletion = (previous[j] ?? 0) + 1;
|
|
61
|
+
const insertion = (current[j - 1] ?? 0) + 1;
|
|
62
|
+
const substitution = (previous[j - 1] ?? 0) + cost;
|
|
63
|
+
current[j] = Math.min(deletion, insertion, substitution);
|
|
64
|
+
}
|
|
65
|
+
previous = current;
|
|
66
|
+
}
|
|
67
|
+
return previous[b.length] ?? 0;
|
|
68
|
+
}
|
|
69
|
+
function utils_isRecord(value) {
|
|
70
|
+
return 'object' == typeof value && null !== value && !Array.isArray(value);
|
|
71
|
+
}
|
|
72
|
+
function isStringArray(value) {
|
|
73
|
+
return Array.isArray(value) && value.every((item)=>'string' == typeof item);
|
|
74
|
+
}
|
|
75
|
+
function parsePathList(input) {
|
|
76
|
+
return input.split(/[,,;;]/).map((item)=>item.trim()).filter((item)=>item.length > 0);
|
|
77
|
+
}
|
|
78
|
+
const SCHEMA_VERSION = 2;
|
|
79
|
+
const ROLES = [
|
|
80
|
+
'frontend',
|
|
81
|
+
'backend',
|
|
82
|
+
'fullstack'
|
|
83
|
+
];
|
|
84
|
+
const SIDES = [
|
|
85
|
+
'frontend',
|
|
86
|
+
'backend'
|
|
87
|
+
];
|
|
88
|
+
const TOP_LEVEL_KEYS = new Set([
|
|
89
|
+
'schemaVersion',
|
|
90
|
+
'profile',
|
|
91
|
+
'docs',
|
|
92
|
+
'terminals'
|
|
93
|
+
]);
|
|
94
|
+
const PROFILE_KEYS = new Set([
|
|
95
|
+
'developer',
|
|
96
|
+
'role'
|
|
97
|
+
]);
|
|
98
|
+
const DOCS_KEYS = new Set([
|
|
99
|
+
'root'
|
|
100
|
+
]);
|
|
101
|
+
const TERMINAL_KEYS = new Set([
|
|
102
|
+
'aliases',
|
|
103
|
+
'repos'
|
|
104
|
+
]);
|
|
105
|
+
const REPO_KEYS = new Set(SIDES);
|
|
106
|
+
const DEVELOPER_PATTERN = /^[a-z][a-z0-9]*$/;
|
|
107
|
+
function isRole(value) {
|
|
108
|
+
return 'string' == typeof value && ROLES.some((role)=>role === value);
|
|
109
|
+
}
|
|
110
|
+
function schema_error(code, message, where) {
|
|
111
|
+
return {
|
|
112
|
+
level: 'error',
|
|
113
|
+
code,
|
|
114
|
+
message,
|
|
115
|
+
...void 0 === where ? {} : {
|
|
116
|
+
where
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function warning(code, message, where) {
|
|
121
|
+
return {
|
|
122
|
+
level: 'warning',
|
|
123
|
+
code,
|
|
124
|
+
message,
|
|
125
|
+
...void 0 === where ? {} : {
|
|
126
|
+
where
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function unknownKeys(value, allowed, where) {
|
|
131
|
+
return Object.keys(value).filter((key)=>!allowed.has(key)).map((key)=>schema_error('UNKNOWN_KEY', `未知字段「${key}」,允许的字段为:${[
|
|
132
|
+
...allowed
|
|
133
|
+
].join(', ')}`, where));
|
|
134
|
+
}
|
|
135
|
+
function checkAbsolutePath(value, where, issues, required) {
|
|
136
|
+
if (null === value) {
|
|
137
|
+
if (required) issues.push(schema_error('FIELD_REQUIRED', '该字段不能为空', where));
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
if ('string' != typeof value || 0 === value.trim().length) {
|
|
141
|
+
issues.push(schema_error('FIELD_TYPE', '必须是绝对路径字符串', where));
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
if (!isAbsolute(value)) {
|
|
145
|
+
issues.push(schema_error('PATH_NOT_ABSOLUTE', `必须是绝对路径:${value}`, where));
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
function parseProfile(raw, options, issues) {
|
|
151
|
+
if (!utils_isRecord(raw)) {
|
|
152
|
+
issues.push(schema_error('PROFILE_TYPE', 'profile 必须是对象', 'profile'));
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
issues.push(...unknownKeys(raw, PROFILE_KEYS, 'profile'));
|
|
156
|
+
const incomplete = true === options.allowIncomplete;
|
|
157
|
+
let valid = true;
|
|
158
|
+
const developerRaw = raw['developer'];
|
|
159
|
+
let developer = '';
|
|
160
|
+
if (void 0 === developerRaw || 'string' == typeof developerRaw && '' === developerRaw.trim()) {
|
|
161
|
+
if (!incomplete) {
|
|
162
|
+
issues.push(schema_error('FIELD_REQUIRED', 'profile.developer 未配置', 'profile.developer'));
|
|
163
|
+
valid = false;
|
|
164
|
+
}
|
|
165
|
+
} else if ('string' == typeof developerRaw && DEVELOPER_PATTERN.test(developerRaw)) developer = developerRaw;
|
|
166
|
+
else {
|
|
167
|
+
issues.push(schema_error('DEVELOPER_INVALID', `开发者标识必须是小写字母开头的字母数字串:${String(developerRaw)}`, 'profile.developer'));
|
|
168
|
+
valid = false;
|
|
169
|
+
}
|
|
170
|
+
const roleRaw = raw['role'];
|
|
171
|
+
let role = 'frontend';
|
|
172
|
+
if (void 0 === roleRaw) {
|
|
173
|
+
if (!incomplete) {
|
|
174
|
+
issues.push(schema_error('FIELD_REQUIRED', 'profile.role 未配置', 'profile.role'));
|
|
175
|
+
valid = false;
|
|
176
|
+
}
|
|
177
|
+
} else if (isRole(roleRaw)) role = roleRaw;
|
|
178
|
+
else {
|
|
179
|
+
issues.push(schema_error('ROLE_INVALID', `角色必须是 ${ROLES.join(' / ')} 之一:${String(roleRaw)}`, 'profile.role'));
|
|
180
|
+
valid = false;
|
|
181
|
+
}
|
|
182
|
+
if (!valid) return null;
|
|
183
|
+
return {
|
|
184
|
+
developer,
|
|
185
|
+
role
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function parseDocs(raw, options, issues) {
|
|
189
|
+
if (!utils_isRecord(raw)) {
|
|
190
|
+
issues.push(schema_error('DOCS_TYPE', 'docs 必须是对象', 'docs'));
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
issues.push(...unknownKeys(raw, DOCS_KEYS, 'docs'));
|
|
194
|
+
const rootRaw = raw['root'];
|
|
195
|
+
if (void 0 === rootRaw || 'string' == typeof rootRaw && '' === rootRaw.trim()) {
|
|
196
|
+
if (true === options.allowIncomplete) return {
|
|
197
|
+
root: ''
|
|
198
|
+
};
|
|
199
|
+
issues.push(schema_error('FIELD_REQUIRED', 'docs.root 未配置', 'docs.root'));
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
const root = checkAbsolutePath(rootRaw, 'docs.root', issues, true);
|
|
203
|
+
return null === root ? null : {
|
|
204
|
+
root
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function parseRepos(raw, where, issues) {
|
|
208
|
+
if (!utils_isRecord(raw)) {
|
|
209
|
+
issues.push(schema_error('REPOS_TYPE', 'repos 必须是对象', where));
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
issues.push(...unknownKeys(raw, REPO_KEYS, where));
|
|
213
|
+
const repos = {
|
|
214
|
+
frontend: [],
|
|
215
|
+
backend: []
|
|
216
|
+
};
|
|
217
|
+
let valid = true;
|
|
218
|
+
for (const side of SIDES){
|
|
219
|
+
const value = raw[side];
|
|
220
|
+
const whereSide = `${where}.${side}`;
|
|
221
|
+
if (null == value) continue;
|
|
222
|
+
if (!Array.isArray(value)) {
|
|
223
|
+
issues.push(schema_error('REPOS_NOT_ARRAY', '该端仓库必须是路径数组,例如 ["/path/to/repo"]', whereSide));
|
|
224
|
+
valid = false;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const seen = new Set();
|
|
228
|
+
for (const item of value){
|
|
229
|
+
if ('string' != typeof item || 0 === item.trim().length) {
|
|
230
|
+
issues.push(schema_error('REPO_PATH_TYPE', '仓库路径必须是非空字符串', whereSide));
|
|
231
|
+
valid = false;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (!isAbsolute(item)) {
|
|
235
|
+
issues.push(schema_error('PATH_NOT_ABSOLUTE', `仓库路径必须是绝对路径:${item}`, whereSide));
|
|
236
|
+
valid = false;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (seen.has(item)) {
|
|
240
|
+
issues.push(schema_error('REPO_DUPLICATE', `同一端存在重复仓库路径:${item}`, whereSide));
|
|
241
|
+
valid = false;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
seen.add(item);
|
|
245
|
+
repos[side].push(item);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return valid ? repos : null;
|
|
249
|
+
}
|
|
250
|
+
function parseTerminals(raw, issues) {
|
|
251
|
+
if (!utils_isRecord(raw)) {
|
|
252
|
+
issues.push(schema_error('TERMINALS_TYPE', 'terminals 必须是对象', 'terminals'));
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
const terminals = {};
|
|
256
|
+
let valid = true;
|
|
257
|
+
for (const [name, value] of Object.entries(raw)){
|
|
258
|
+
const where = `terminals.${name}`;
|
|
259
|
+
if (name.includes('.')) {
|
|
260
|
+
issues.push(schema_error('TERMINAL_KEY_DOT', `终端名不能包含「.」:${name}`, where));
|
|
261
|
+
valid = false;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (0 === name.trim().length) {
|
|
265
|
+
issues.push(schema_error('TERMINAL_KEY_EMPTY', '终端名不能为空', 'terminals'));
|
|
266
|
+
valid = false;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (!utils_isRecord(value)) {
|
|
270
|
+
issues.push(schema_error('TERMINAL_TYPE', '终端配置必须是对象', where));
|
|
271
|
+
valid = false;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
issues.push(...unknownKeys(value, TERMINAL_KEYS, where));
|
|
275
|
+
const aliasesRaw = value['aliases'];
|
|
276
|
+
let aliases = [];
|
|
277
|
+
if (void 0 !== aliasesRaw) if (isStringArray(aliasesRaw)) aliases = aliasesRaw.map((alias)=>alias.trim()).filter((alias)=>alias.length > 0);
|
|
278
|
+
else {
|
|
279
|
+
issues.push(schema_error('ALIASES_TYPE', 'aliases 必须是字符串数组', `${where}.aliases`));
|
|
280
|
+
valid = false;
|
|
281
|
+
}
|
|
282
|
+
const repos = parseRepos(value['repos'], `${where}.repos`, issues);
|
|
283
|
+
if (null === repos) {
|
|
284
|
+
valid = false;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
terminals[name] = {
|
|
288
|
+
aliases,
|
|
289
|
+
repos
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
return valid ? terminals : null;
|
|
293
|
+
}
|
|
294
|
+
function checkTerminalIdentity(terminals) {
|
|
295
|
+
const issues = [];
|
|
296
|
+
const seen = new Map();
|
|
297
|
+
for (const [name, terminal] of Object.entries(terminals)){
|
|
298
|
+
if (name.includes('.') || 0 === name.trim().length) continue;
|
|
299
|
+
const normalizedKey = normalizeName(name);
|
|
300
|
+
const existingKey = seen.get(normalizedKey);
|
|
301
|
+
if (void 0 !== existingKey) issues.push(schema_error('TERMINAL_DUPLICATE', `终端名「${name}」与「${existingKey}」归一化后重复(${normalizedKey}),请改名或合并`, `terminals.${name}`));
|
|
302
|
+
else seen.set(normalizedKey, name);
|
|
303
|
+
}
|
|
304
|
+
for (const [name, terminal] of Object.entries(terminals))for (const alias of terminal.aliases){
|
|
305
|
+
const normalizedAlias = normalizeName(alias);
|
|
306
|
+
if (normalizedAlias === normalizeName(name)) {
|
|
307
|
+
issues.push(warning('ALIAS_REDUNDANT', `别名「${alias}」归一化后与终端名相同,可删除(空格/全角/大小写差异已由归一化覆盖)`, `terminals.${name}.aliases`));
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
const existing = seen.get(normalizedAlias);
|
|
311
|
+
if (void 0 !== existing) issues.push(schema_error('ALIAS_CONFLICT', `别名「${alias}」与「${existing}」冲突(归一化后为 ${normalizedAlias})`, `terminals.${name}.aliases`));
|
|
312
|
+
else seen.set(normalizedAlias, `${name}(别名)`);
|
|
313
|
+
}
|
|
314
|
+
return issues;
|
|
315
|
+
}
|
|
316
|
+
function checkFsOfDocs(config, issues) {
|
|
317
|
+
const { root } = config.docs;
|
|
318
|
+
if ('' === root) return;
|
|
319
|
+
if (!existsSync(root)) return void issues.push(schema_error('DOCS_ROOT_MISSING', `文档仓库不存在:${root}`, 'docs.root'));
|
|
320
|
+
if (!statSync(root).isDirectory()) return void issues.push(schema_error('DOCS_ROOT_NOT_DIR', `文档仓库不是目录:${root}`, 'docs.root'));
|
|
321
|
+
if (!existsSync(join(root, 'skills', 'iteration-task-docs', 'SKILL.md'))) issues.push(schema_error('DOCS_ROOT_NO_SKILL', `文档仓库缺少 skills/iteration-task-docs/SKILL.md:${root}`, 'docs.root'));
|
|
322
|
+
}
|
|
323
|
+
function checkFsOfTerminals(config, issues) {
|
|
324
|
+
const pathOwners = new Map();
|
|
325
|
+
for (const [name, terminal] of Object.entries(config.terminals))for (const side of SIDES){
|
|
326
|
+
const targets = terminal.repos[side];
|
|
327
|
+
const where = `terminals.${name}.repos.${side}`;
|
|
328
|
+
if (0 === targets.length) {
|
|
329
|
+
if ('fullstack' === config.profile.role) issues.push(warning('SIDE_NOT_CONFIGURED', `角色为 fullstack,但「${name}」未配置 ${side} 仓库`, where));
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
for (const target of targets){
|
|
333
|
+
if (!existsSync(target)) {
|
|
334
|
+
issues.push(schema_error('REPO_PATH_MISSING', `仓库路径不存在:${target}`, where));
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (!statSync(target).isDirectory()) {
|
|
338
|
+
issues.push(schema_error('REPO_PATH_NOT_DIR', `仓库路径不是目录:${target}`, where));
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (!existsSync(join(target, '.git'))) issues.push(warning('REPO_NOT_GIT', `路径不是 git 仓库:${target}`, where));
|
|
342
|
+
const entry = pathOwners.get(`${side}:${target}`) ?? {
|
|
343
|
+
side,
|
|
344
|
+
target,
|
|
345
|
+
owners: []
|
|
346
|
+
};
|
|
347
|
+
entry.owners.push(name);
|
|
348
|
+
pathOwners.set(`${side}:${target}`, entry);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
for (const entry of pathOwners.values())if (!(entry.owners.length < 2)) issues.push(warning('REPO_PATH_SHARED', `${entry.side} 仓库路径被多个终端复用:${entry.target}(${entry.owners.join('、')})`, 'terminals'));
|
|
352
|
+
}
|
|
353
|
+
function checkConfig(raw, options = {}) {
|
|
354
|
+
const issues = [];
|
|
355
|
+
if (!utils_isRecord(raw)) return {
|
|
356
|
+
config: null,
|
|
357
|
+
issues: [
|
|
358
|
+
schema_error('CONFIG_ROOT', '配置文件根节点必须是对象')
|
|
359
|
+
]
|
|
360
|
+
};
|
|
361
|
+
issues.push(...unknownKeys(raw, TOP_LEVEL_KEYS, 'root'));
|
|
362
|
+
const version = raw['schemaVersion'];
|
|
363
|
+
let versionValid = true;
|
|
364
|
+
if (void 0 === version) {
|
|
365
|
+
issues.push(schema_error('SCHEMA_VERSION_MISSING', `缺少 schemaVersion,应为 ${SCHEMA_VERSION}`, 'schemaVersion'));
|
|
366
|
+
versionValid = false;
|
|
367
|
+
} else if (version !== SCHEMA_VERSION) {
|
|
368
|
+
issues.push(schema_error('SCHEMA_VERSION_UNSUPPORTED', `不支持的 schemaVersion:${String(version)}(当前 CLI 支持 ${SCHEMA_VERSION})`, 'schemaVersion'));
|
|
369
|
+
versionValid = false;
|
|
370
|
+
}
|
|
371
|
+
const profile = parseProfile(raw['profile'], options, issues);
|
|
372
|
+
const docs = parseDocs(raw['docs'], options, issues);
|
|
373
|
+
const terminals = parseTerminals(raw['terminals'] ?? {}, issues);
|
|
374
|
+
if (null === profile || null === docs || null === terminals || !versionValid) return {
|
|
375
|
+
config: null,
|
|
376
|
+
issues
|
|
377
|
+
};
|
|
378
|
+
const config = {
|
|
379
|
+
schemaVersion: SCHEMA_VERSION,
|
|
380
|
+
profile,
|
|
381
|
+
docs,
|
|
382
|
+
terminals
|
|
383
|
+
};
|
|
384
|
+
issues.push(...checkTerminalIdentity(terminals));
|
|
385
|
+
if (true === options.checkFs) {
|
|
386
|
+
checkFsOfDocs(config, issues);
|
|
387
|
+
checkFsOfTerminals(config, issues);
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
config,
|
|
391
|
+
issues
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
function hasError(issues) {
|
|
395
|
+
return issues.some((issue)=>'error' === issue.level);
|
|
396
|
+
}
|
|
397
|
+
function formatIssue(issue) {
|
|
398
|
+
const tag = 'error' === issue.level ? picocolors.red('✗ error') : picocolors.yellow('! warning');
|
|
399
|
+
const where = void 0 === issue.where ? '' : picocolors.dim(` [${issue.where}]`);
|
|
400
|
+
return `${tag} ${issue.code}${where} ${issue.message}`;
|
|
401
|
+
}
|
|
402
|
+
function formatIssues(issues, emptyText = '未发现问题') {
|
|
403
|
+
if (0 === issues.length) return picocolors.green(`✓ ${emptyText}`);
|
|
404
|
+
return issues.map(formatIssue).join('\n');
|
|
405
|
+
}
|
|
406
|
+
function countIssues(issues) {
|
|
407
|
+
let errors = 0;
|
|
408
|
+
let warnings = 0;
|
|
409
|
+
for (const issue of issues)if ('error' === issue.level) errors += 1;
|
|
410
|
+
else warnings += 1;
|
|
411
|
+
return {
|
|
412
|
+
errors,
|
|
413
|
+
warnings
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
function existsMark(exists) {
|
|
417
|
+
return exists ? picocolors.green('✓') : picocolors.red('✗');
|
|
418
|
+
}
|
|
419
|
+
const MAX_BACKUPS = 10;
|
|
420
|
+
const TEMPLATE = `# ylcwf 配置(schemaVersion 2)
|
|
421
|
+
# 由 ylcwf CLI 管理;手改后请运行 \`ylcwf doctor\` 校验。
|
|
422
|
+
schemaVersion: 2
|
|
423
|
+
|
|
424
|
+
profile:
|
|
425
|
+
developer: ''
|
|
426
|
+
role: frontend
|
|
427
|
+
|
|
428
|
+
docs:
|
|
429
|
+
root: ''
|
|
430
|
+
|
|
431
|
+
terminals: {}
|
|
432
|
+
`;
|
|
433
|
+
function createEmptyDocument() {
|
|
434
|
+
return parseDocument(TEMPLATE);
|
|
435
|
+
}
|
|
436
|
+
function loadConfig(explicit) {
|
|
437
|
+
const path = resolveConfigPath(explicit);
|
|
438
|
+
if (!existsSync(path)) return {
|
|
439
|
+
path,
|
|
440
|
+
exists: false,
|
|
441
|
+
doc: null,
|
|
442
|
+
raw: null,
|
|
443
|
+
config: null,
|
|
444
|
+
issues: []
|
|
445
|
+
};
|
|
446
|
+
const text = readFileSync(path, 'utf8');
|
|
447
|
+
const doc = parseDocument(text);
|
|
448
|
+
if (doc.errors.length > 0) {
|
|
449
|
+
const detail = doc.errors.map((item)=>item.message).join('\n');
|
|
450
|
+
throw new errors_CliError(`配置文件 YAML 语法错误:${path}\n${detail}`, errors_EXIT.INVALID);
|
|
451
|
+
}
|
|
452
|
+
const raw = doc.toJS();
|
|
453
|
+
const checked = checkConfig(raw, {
|
|
454
|
+
allowIncomplete: true
|
|
455
|
+
});
|
|
456
|
+
return {
|
|
457
|
+
path,
|
|
458
|
+
exists: true,
|
|
459
|
+
doc,
|
|
460
|
+
raw,
|
|
461
|
+
config: checked.config,
|
|
462
|
+
issues: checked.issues
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function requireConfig(explicit) {
|
|
466
|
+
const loaded = loadConfig(explicit);
|
|
467
|
+
if (!loaded.exists || null === loaded.doc) throw new errors_CliError(`配置未初始化:${loaded.path}\n请先运行:ylcwf init`, errors_EXIT.NOT_FOUND);
|
|
468
|
+
if (null === loaded.config) throw new errors_CliError(`配置结构不合法:${loaded.path}\n${formatIssues(loaded.issues)}`, errors_EXIT.INVALID);
|
|
469
|
+
return {
|
|
470
|
+
path: loaded.path,
|
|
471
|
+
doc: loaded.doc,
|
|
472
|
+
config: loaded.config,
|
|
473
|
+
issues: loaded.issues
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function obtainDocument(explicit) {
|
|
477
|
+
const loaded = loadConfig(explicit);
|
|
478
|
+
if (null !== loaded.doc) return {
|
|
479
|
+
path: loaded.path,
|
|
480
|
+
doc: loaded.doc
|
|
481
|
+
};
|
|
482
|
+
return {
|
|
483
|
+
path: loaded.path,
|
|
484
|
+
doc: parseDocument(TEMPLATE)
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function assertWritable(doc, path) {
|
|
488
|
+
const raw = doc.toJS();
|
|
489
|
+
const checked = checkConfig(raw, {
|
|
490
|
+
allowIncomplete: true
|
|
491
|
+
});
|
|
492
|
+
if (null === checked.config || checked.issues.some((issue)=>'error' === issue.level)) throw new errors_CliError(`配置校验失败,已取消写入:${path}\n${formatIssues(checked.issues)}`, errors_EXIT.INVALID);
|
|
493
|
+
return checked.config;
|
|
494
|
+
}
|
|
495
|
+
function toBlockStyle(node) {
|
|
496
|
+
if (isMap(node)) {
|
|
497
|
+
if (node.items.length > 0) node.flow = false;
|
|
498
|
+
for (const pair of node.items){
|
|
499
|
+
toBlockStyle(pair.key);
|
|
500
|
+
toBlockStyle(pair.value);
|
|
501
|
+
}
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (isSeq(node)) {
|
|
505
|
+
if (node.items.length > 0) node.flow = false;
|
|
506
|
+
for (const item of node.items)toBlockStyle(item);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
function saveDocument(doc, target) {
|
|
510
|
+
mkdirSync(dirname(target), {
|
|
511
|
+
recursive: true
|
|
512
|
+
});
|
|
513
|
+
if (existsSync(target)) backup(target);
|
|
514
|
+
toBlockStyle(doc.contents);
|
|
515
|
+
const text = doc.toString({
|
|
516
|
+
lineWidth: 0
|
|
517
|
+
});
|
|
518
|
+
const tmp = `${target}.tmp-${process.pid}`;
|
|
519
|
+
writeFileSync(tmp, text, 'utf8');
|
|
520
|
+
renameSync(tmp, target);
|
|
521
|
+
}
|
|
522
|
+
function backup(target) {
|
|
523
|
+
const dir = backupsDir(target);
|
|
524
|
+
mkdirSync(dir, {
|
|
525
|
+
recursive: true
|
|
526
|
+
});
|
|
527
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
528
|
+
let destination = join(dir, `config-${stamp}.yaml`);
|
|
529
|
+
let index = 1;
|
|
530
|
+
while(existsSync(destination)){
|
|
531
|
+
destination = join(dir, `config-${stamp}-${index}.yaml`);
|
|
532
|
+
index += 1;
|
|
533
|
+
}
|
|
534
|
+
copyFileSync(target, destination);
|
|
535
|
+
const files = readdirSync(dir).filter((name)=>name.startsWith('config-') && name.endsWith('.yaml')).sort();
|
|
536
|
+
const stale = files.slice(0, Math.max(0, files.length - MAX_BACKUPS));
|
|
537
|
+
for (const name of stale)unlinkSync(`${dir}/${name}`);
|
|
538
|
+
}
|
|
539
|
+
function parseDotPath(input) {
|
|
540
|
+
const parts = input.split('.').map((part)=>part.trim());
|
|
541
|
+
if (0 === parts.length || parts.some((part)=>0 === part.length)) throw new errors_CliError(`路径不合法:${input}(应形如 docs.role)`, errors_EXIT.USAGE);
|
|
542
|
+
return parts;
|
|
543
|
+
}
|
|
544
|
+
function getIn(root, path) {
|
|
545
|
+
let current = root;
|
|
546
|
+
for (const key of path){
|
|
547
|
+
if (!utils_isRecord(current)) return;
|
|
548
|
+
if (!Object.hasOwn(current, key)) return;
|
|
549
|
+
current = current[key];
|
|
550
|
+
}
|
|
551
|
+
return current;
|
|
552
|
+
}
|
|
553
|
+
function renderScalar(value) {
|
|
554
|
+
if ('string' == typeof value) return value;
|
|
555
|
+
if (null === value) return 'null';
|
|
556
|
+
return JSON.stringify(value);
|
|
557
|
+
}
|
|
558
|
+
function bail() {
|
|
559
|
+
__rspack_external__clack_prompts_d2d5c36d.cancel('已取消');
|
|
560
|
+
process.exit(errors_EXIT.OK);
|
|
561
|
+
}
|
|
562
|
+
async function askText(message, placeholder, validate, defaultValue = '') {
|
|
563
|
+
const answer = await __rspack_external__clack_prompts_d2d5c36d.text({
|
|
564
|
+
message,
|
|
565
|
+
placeholder,
|
|
566
|
+
defaultValue,
|
|
567
|
+
validate
|
|
568
|
+
});
|
|
569
|
+
if (__rspack_external__clack_prompts_d2d5c36d.isCancel(answer)) bail();
|
|
570
|
+
return answer;
|
|
571
|
+
}
|
|
572
|
+
async function askConfirm(message, initialValue) {
|
|
573
|
+
const answer = await __rspack_external__clack_prompts_d2d5c36d.confirm({
|
|
574
|
+
message,
|
|
575
|
+
initialValue
|
|
576
|
+
});
|
|
577
|
+
if (__rspack_external__clack_prompts_d2d5c36d.isCancel(answer)) bail();
|
|
578
|
+
return answer;
|
|
579
|
+
}
|
|
580
|
+
function validateExistingDir(value) {
|
|
581
|
+
const trimmed = (value ?? '').trim();
|
|
582
|
+
if (0 === trimmed.length) return '不能为空';
|
|
583
|
+
if (!isAbsolute(trimmed)) return '必须是绝对路径';
|
|
584
|
+
if (!existsSync(trimmed)) return '路径不存在';
|
|
585
|
+
if (!statSync(trimmed).isDirectory()) return '不是目录';
|
|
586
|
+
}
|
|
587
|
+
function validateDeveloper(value) {
|
|
588
|
+
if (!/^[a-z][a-z0-9]*$/.test((value ?? '').trim())) return '必须是小写字母开头的字母数字串(姓名小写全拼)';
|
|
589
|
+
}
|
|
590
|
+
function splitAliases(value) {
|
|
591
|
+
return value.split(',').map((item)=>item.trim()).filter((item)=>item.length > 0);
|
|
592
|
+
}
|
|
593
|
+
function validatePathList(value) {
|
|
594
|
+
const raw = (value ?? '').trim();
|
|
595
|
+
if (0 === raw.length) return;
|
|
596
|
+
for (const part of parsePathList(raw)){
|
|
597
|
+
const message = validateExistingDir(part);
|
|
598
|
+
if (void 0 !== message) return `${part}:${message}`;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
async function runInit(options) {
|
|
602
|
+
if (true !== process.stdin.isTTY) throw new errors_CliError('init 需要交互式终端;无 TTY 时请改用:ylcwf set / ylcwf terminal add', errors_EXIT.USAGE);
|
|
603
|
+
const configPath = resolveConfigPath(options.config);
|
|
604
|
+
const existing = loadConfig(options.config);
|
|
605
|
+
if (existing.exists && true !== options.force) {
|
|
606
|
+
const proceed = await askConfirm(`配置已存在(${configPath}),继续将覆盖全部字段。是否继续?`, false);
|
|
607
|
+
if (!proceed) return void __rspack_external__clack_prompts_d2d5c36d.cancel('已取消');
|
|
608
|
+
}
|
|
609
|
+
__rspack_external__clack_prompts_d2d5c36d.intro(`ylcwf 初始化 → ${configPath}`);
|
|
610
|
+
const doc = createEmptyDocument();
|
|
611
|
+
const root = await askText('迭代任务文档仓库绝对路径', '/Users/you/develop/iteration-task-repo', validateExistingDir);
|
|
612
|
+
doc.setIn([
|
|
613
|
+
'docs',
|
|
614
|
+
'root'
|
|
615
|
+
], root.trim());
|
|
616
|
+
const developer = await askText('开发者标识(姓名小写全拼)', 'yourname', validateDeveloper);
|
|
617
|
+
doc.setIn([
|
|
618
|
+
'profile',
|
|
619
|
+
'developer'
|
|
620
|
+
], developer.trim());
|
|
621
|
+
const role = await __rspack_external__clack_prompts_d2d5c36d.select({
|
|
622
|
+
message: '默认角色',
|
|
623
|
+
options: [
|
|
624
|
+
{
|
|
625
|
+
value: 'frontend',
|
|
626
|
+
label: '前端'
|
|
627
|
+
},
|
|
628
|
+
{
|
|
629
|
+
value: 'backend',
|
|
630
|
+
label: '后端'
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
value: 'fullstack',
|
|
634
|
+
label: '全栈(前端 + 后端)'
|
|
635
|
+
}
|
|
636
|
+
],
|
|
637
|
+
initialValue: 'frontend'
|
|
638
|
+
});
|
|
639
|
+
if (__rspack_external__clack_prompts_d2d5c36d.isCancel(role)) bail();
|
|
640
|
+
doc.setIn([
|
|
641
|
+
'profile',
|
|
642
|
+
'role'
|
|
643
|
+
], role);
|
|
644
|
+
__rspack_external__clack_prompts_d2d5c36d.log.info('接下来逐项录入终端 → 本地仓库映射;终端名留空即结束。每端多个仓库用逗号或分号分隔。');
|
|
645
|
+
let index = 0;
|
|
646
|
+
for(;;){
|
|
647
|
+
index += 1;
|
|
648
|
+
const name = await askText(`终端 #${index}:PRD 中的终端名(留空结束)`, '示例终端-A', ()=>void 0);
|
|
649
|
+
const trimmedName = name.trim();
|
|
650
|
+
if (0 === trimmedName.length) break;
|
|
651
|
+
if (void 0 !== getIn(doc.toJS(), [
|
|
652
|
+
'terminals',
|
|
653
|
+
trimmedName
|
|
654
|
+
])) {
|
|
655
|
+
__rspack_external__clack_prompts_d2d5c36d.log.warn(`终端「${trimmedName}」已存在,跳过`);
|
|
656
|
+
index -= 1;
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
const aliases = await askText('别名(逗号分隔,可留空)', '别名1, 别名2', ()=>void 0);
|
|
660
|
+
const frontendRaw = await askText('前端仓库绝对路径(多个用逗号或分号分隔,可留空)', '/Users/you/develop/ylc/xxx', validatePathList);
|
|
661
|
+
const backendRaw = await askText('后端仓库绝对路径(多个用逗号或分号分隔,可留空)', '/Users/you/develop/java/xxx', validatePathList);
|
|
662
|
+
doc.setIn([
|
|
663
|
+
'terminals',
|
|
664
|
+
trimmedName
|
|
665
|
+
], {
|
|
666
|
+
aliases: splitAliases(aliases),
|
|
667
|
+
repos: {
|
|
668
|
+
frontend: parsePathList(frontendRaw),
|
|
669
|
+
backend: parsePathList(backendRaw)
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
assertWritable(doc, configPath);
|
|
674
|
+
saveDocument(doc, configPath);
|
|
675
|
+
__rspack_external__clack_prompts_d2d5c36d.outro(`已写入 ${configPath}\n下一步:ylcwf doctor 校验,ylcwf show 查看;后续追加仓库用 ylcwf terminal repo add`);
|
|
676
|
+
printLine('');
|
|
677
|
+
}
|
|
678
|
+
function registerInit(program) {
|
|
679
|
+
program.command('init').description('交互式首次配置(逐项引导)').option('-c, --config <path>', '指定配置文件路径').option('--force', '已存在时直接覆盖,不再确认').action(async (options)=>{
|
|
680
|
+
await runInit(options);
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
const ANSI_PATTERN = new RegExp('\\u001B\\[[0-9;]*m', 'g');
|
|
684
|
+
function displayWidth(text) {
|
|
685
|
+
let width = 0;
|
|
686
|
+
for (const char of text.replace(ANSI_PATTERN, '')){
|
|
687
|
+
const code = char.codePointAt(0) ?? 0;
|
|
688
|
+
if (code >= 0x1f300 && code <= 0x1faff) {
|
|
689
|
+
width += 2;
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
width += isWide(char) ? 2 : 1;
|
|
693
|
+
}
|
|
694
|
+
return width;
|
|
695
|
+
}
|
|
696
|
+
function isWide(char) {
|
|
697
|
+
const code = char.codePointAt(0) ?? 0;
|
|
698
|
+
return code >= 0x1100 && code <= 0x115f || code >= 0x2e80 && code <= 0x303e || code >= 0x3041 && code <= 0x33ff || code >= 0x3400 && code <= 0x4dbf || code >= 0x4e00 && code <= 0x9fff || code >= 0xa000 && code <= 0xa4cf || code >= 0xac00 && code <= 0xd7a3 || code >= 0xf900 && code <= 0xfaff || code >= 0xfe30 && code <= 0xfe4f || code >= 0xff00 && code <= 0xff60 || code >= 0xffe0 && code <= 0xffe6;
|
|
699
|
+
}
|
|
700
|
+
function padCell(cell, width) {
|
|
701
|
+
const visible = displayWidth(cell);
|
|
702
|
+
return visible >= width ? cell : cell + ' '.repeat(width - visible);
|
|
703
|
+
}
|
|
704
|
+
function renderTable(headers, rows) {
|
|
705
|
+
headers.length;
|
|
706
|
+
const widths = headers.map((header, index)=>Math.max(displayWidth(header), ...rows.map((row)=>displayWidth(row[index] ?? ''))));
|
|
707
|
+
const formatRow = (cells, padLast)=>cells.map((cell, index)=>{
|
|
708
|
+
const width = widths[index] ?? 0;
|
|
709
|
+
return padLast || index < cells.length - 1 ? padCell(cell, width) : cell;
|
|
710
|
+
}).join(' ').trimEnd();
|
|
711
|
+
const separator = widths.map((width)=>'─'.repeat(width)).join(' ');
|
|
712
|
+
return [
|
|
713
|
+
formatRow(headers, true),
|
|
714
|
+
separator,
|
|
715
|
+
...rows.map((row)=>formatRow(row, false))
|
|
716
|
+
];
|
|
717
|
+
}
|
|
718
|
+
function parseFormat(value) {
|
|
719
|
+
if (void 0 === value || 'human' === value) return 'human';
|
|
720
|
+
if ('json' === value) return 'json';
|
|
721
|
+
throw new errors_CliError(`不支持的输出格式:${value}(可选 human / json)`, errors_EXIT.USAGE);
|
|
722
|
+
}
|
|
723
|
+
const ROLE_LABEL = {
|
|
724
|
+
frontend: '前端',
|
|
725
|
+
backend: '后端',
|
|
726
|
+
fullstack: '全栈(前端 + 后端)'
|
|
727
|
+
};
|
|
728
|
+
const SIDE_LABEL = {
|
|
729
|
+
frontend: '前端',
|
|
730
|
+
backend: '后端'
|
|
731
|
+
};
|
|
732
|
+
function showConfig(explicit, format) {
|
|
733
|
+
const loaded = loadConfig(explicit);
|
|
734
|
+
if (!loaded.exists) {
|
|
735
|
+
if ('json' === format) return void printJson({
|
|
736
|
+
schemaVersion: null,
|
|
737
|
+
configPath: loaded.path,
|
|
738
|
+
exists: false,
|
|
739
|
+
config: null,
|
|
740
|
+
issues: []
|
|
741
|
+
});
|
|
742
|
+
printLine(`配置文件 ${loaded.path} ${existsMark(false)}(未初始化)`);
|
|
743
|
+
printLine('');
|
|
744
|
+
printLine('运行 `ylcwf init` 完成首次配置,或 `ylcwf set <path> <value>` 精确修改。');
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
const checked = checkConfig(loaded.raw, {
|
|
748
|
+
checkFs: true
|
|
749
|
+
});
|
|
750
|
+
if ('json' === format) return void printJson({
|
|
751
|
+
schemaVersion: checked.config?.schemaVersion ?? null,
|
|
752
|
+
configPath: loaded.path,
|
|
753
|
+
exists: true,
|
|
754
|
+
config: checked.config,
|
|
755
|
+
issues: checked.issues
|
|
756
|
+
});
|
|
757
|
+
renderHuman(loaded.path, checked.config, checked.issues);
|
|
758
|
+
}
|
|
759
|
+
function renderHuman(path, config, issues) {
|
|
760
|
+
const { errors, warnings } = countIssues(issues);
|
|
761
|
+
if (null === config) {
|
|
762
|
+
printLine(renderTable([
|
|
763
|
+
'项',
|
|
764
|
+
'值'
|
|
765
|
+
], [
|
|
766
|
+
[
|
|
767
|
+
'配置文件',
|
|
768
|
+
`${path} ${existsMark(true)}`
|
|
769
|
+
],
|
|
770
|
+
[
|
|
771
|
+
'结构',
|
|
772
|
+
picocolors.red('不合法,无法读取有效字段')
|
|
773
|
+
]
|
|
774
|
+
]).join('\n'));
|
|
775
|
+
printLine('');
|
|
776
|
+
printLine(formatIssues(issues));
|
|
777
|
+
printHint();
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
printLine(renderTable([
|
|
781
|
+
'项',
|
|
782
|
+
'值'
|
|
783
|
+
], [
|
|
784
|
+
[
|
|
785
|
+
'配置文件',
|
|
786
|
+
`${path} ${existsMark(true)}`
|
|
787
|
+
],
|
|
788
|
+
[
|
|
789
|
+
'文档仓库',
|
|
790
|
+
`${config.docs.root} ${existsMark(existsSync(config.docs.root))}`
|
|
791
|
+
],
|
|
792
|
+
[
|
|
793
|
+
'开发者标识',
|
|
794
|
+
config.profile.developer
|
|
795
|
+
],
|
|
796
|
+
[
|
|
797
|
+
'默认角色',
|
|
798
|
+
ROLE_LABEL[config.profile.role]
|
|
799
|
+
]
|
|
800
|
+
]).join('\n'));
|
|
801
|
+
const names = Object.keys(config.terminals);
|
|
802
|
+
printLine('');
|
|
803
|
+
printLine(`PRD终端-本地仓库映射表(${names.length})`);
|
|
804
|
+
if (0 === names.length) printLine(picocolors.dim(' (空)运行 ylcwf terminal add <终端名> --frontend <路径> 添加'));
|
|
805
|
+
else {
|
|
806
|
+
const rows = [];
|
|
807
|
+
for (const name of names){
|
|
808
|
+
const terminal = config.terminals[name];
|
|
809
|
+
if (void 0 === terminal) continue;
|
|
810
|
+
const aliases = 0 === terminal.aliases.length ? '—' : terminal.aliases.join('、');
|
|
811
|
+
let printedName = false;
|
|
812
|
+
for (const side of SIDES){
|
|
813
|
+
const targets = terminal.repos[side];
|
|
814
|
+
if (0 === targets.length) {
|
|
815
|
+
rows.push([
|
|
816
|
+
printedName ? '' : name,
|
|
817
|
+
printedName ? '' : aliases,
|
|
818
|
+
SIDE_LABEL[side],
|
|
819
|
+
'—',
|
|
820
|
+
picocolors.dim('未配置')
|
|
821
|
+
]);
|
|
822
|
+
printedName = true;
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
targets.forEach((target, index)=>{
|
|
826
|
+
rows.push([
|
|
827
|
+
printedName ? '' : name,
|
|
828
|
+
printedName ? '' : aliases,
|
|
829
|
+
0 === index ? SIDE_LABEL[side] : '',
|
|
830
|
+
target,
|
|
831
|
+
existsMark(existsSync(target))
|
|
832
|
+
]);
|
|
833
|
+
printedName = true;
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
printLine(renderTable([
|
|
838
|
+
'终端',
|
|
839
|
+
'别名',
|
|
840
|
+
'端',
|
|
841
|
+
'本地仓库',
|
|
842
|
+
'状态'
|
|
843
|
+
], rows).join('\n'));
|
|
844
|
+
}
|
|
845
|
+
printLine('');
|
|
846
|
+
if (0 === errors && 0 === warnings) printLine(formatIssues([]));
|
|
847
|
+
else {
|
|
848
|
+
printLine(`问题(error ${errors} / warning ${warnings})`);
|
|
849
|
+
printLine(formatIssues(issues));
|
|
850
|
+
}
|
|
851
|
+
printHint();
|
|
852
|
+
}
|
|
853
|
+
function printHint() {
|
|
854
|
+
printLine('');
|
|
855
|
+
printLine(picocolors.dim('更多命令:ylcwf --help | 健康检查:ylcwf doctor | 首次配置:ylcwf init'));
|
|
856
|
+
}
|
|
857
|
+
function registerShow(program) {
|
|
858
|
+
program.command('show').description('查看当前配置(带路径存在性检查)').option('-c, --config <path>', '指定配置文件路径').option('--format <format>', '输出格式:human | json', 'human').action((options)=>{
|
|
859
|
+
showConfig(options.config, parseFormat(options.format));
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
function runGet(dotPath, options) {
|
|
863
|
+
const format = parseFormat(options.format);
|
|
864
|
+
const ready = requireConfig(options.config);
|
|
865
|
+
const path = parseDotPath(dotPath);
|
|
866
|
+
const value = getIn(ready.config, path);
|
|
867
|
+
if (void 0 === value) throw new errors_CliError(`配置项不存在:${dotPath}`, errors_EXIT.NOT_FOUND);
|
|
868
|
+
if ('json' === format) return void printJson({
|
|
869
|
+
path: dotPath,
|
|
870
|
+
value
|
|
871
|
+
});
|
|
872
|
+
printLine(renderScalar(value));
|
|
873
|
+
}
|
|
874
|
+
function registerGet(program) {
|
|
875
|
+
program.command('get <dotPath>').description('读取配置项,输出裸值(机器接口)').option('-c, --config <path>', '指定配置文件路径').option('--format <format>', '输出格式:human | json', 'human').action((dotPath, options)=>{
|
|
876
|
+
runGet(dotPath, options);
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
const ROLE_SET = new Set(ROLES);
|
|
880
|
+
const SIDE_SET = new Set(SIDES);
|
|
881
|
+
function assertSettable(path) {
|
|
882
|
+
const [first, second, third, fourth] = path;
|
|
883
|
+
if (2 === path.length && 'docs' === first && 'root' === second) return;
|
|
884
|
+
if (2 === path.length && 'profile' === first && ('developer' === second || 'role' === second)) return;
|
|
885
|
+
if (4 === path.length && 'terminals' === first && void 0 !== second && 'repos' === third && void 0 !== fourth && SIDE_SET.has(fourth)) return;
|
|
886
|
+
if (3 === path.length && 'terminals' === first && void 0 !== second && 'aliases' === third) return;
|
|
887
|
+
throw new errors_CliError(`不支持通过 set 修改该路径:${path.join('.')}\n允许:docs.root / profile.developer / profile.role / terminals.<终端>.aliases / terminals.<终端>.repos.frontend|backend\n终端的新增与删除请用:ylcwf terminal add | rm`, errors_EXIT.USAGE);
|
|
888
|
+
}
|
|
889
|
+
function coerceValue(path, raw) {
|
|
890
|
+
const key = path[path.length - 1];
|
|
891
|
+
const trimmed = raw.trim();
|
|
892
|
+
if ('root' === key) {
|
|
893
|
+
if (!isAbsolute(trimmed)) throw new errors_CliError(`文档仓库必须是绝对路径:${raw}`, errors_EXIT.USAGE);
|
|
894
|
+
return trimmed;
|
|
895
|
+
}
|
|
896
|
+
if ('developer' === key) {
|
|
897
|
+
if (!/^[a-z][a-z0-9]*$/.test(trimmed)) throw new errors_CliError(`开发者标识必须是小写字母开头的字母数字串:${raw}`, errors_EXIT.USAGE);
|
|
898
|
+
return trimmed;
|
|
899
|
+
}
|
|
900
|
+
if ('role' === key) {
|
|
901
|
+
if (!ROLE_SET.has(trimmed)) throw new errors_CliError(`角色必须是 ${ROLES.join(' / ')} 之一:${raw}`, errors_EXIT.USAGE);
|
|
902
|
+
return trimmed;
|
|
903
|
+
}
|
|
904
|
+
if ('aliases' === key) return trimmed.split(',').map((item)=>item.trim()).filter((item)=>item.length > 0);
|
|
905
|
+
if ('frontend' === key || 'backend' === key) {
|
|
906
|
+
const parts = parsePathList(trimmed);
|
|
907
|
+
for (const part of parts)if (!isAbsolute(part)) throw new errors_CliError(`仓库路径必须是绝对路径:${part}`, errors_EXIT.USAGE);
|
|
908
|
+
return parts;
|
|
909
|
+
}
|
|
910
|
+
return raw;
|
|
911
|
+
}
|
|
912
|
+
function runSet(dotPath, rawValue, options) {
|
|
913
|
+
const format = parseFormat(options.format);
|
|
914
|
+
const path = parseDotPath(dotPath);
|
|
915
|
+
assertSettable(path);
|
|
916
|
+
const value = coerceValue(path, rawValue);
|
|
917
|
+
const { path: configPath, doc } = obtainDocument(options.config);
|
|
918
|
+
const before = doc.toJS();
|
|
919
|
+
doc.setIn([
|
|
920
|
+
...path
|
|
921
|
+
], value);
|
|
922
|
+
assertWritable(doc, configPath);
|
|
923
|
+
const previous = getIn(before, path);
|
|
924
|
+
const next = getIn(doc.toJS(), path);
|
|
925
|
+
if (true === options.dryRun) {
|
|
926
|
+
if ('json' === format) return void printJson({
|
|
927
|
+
dryRun: true,
|
|
928
|
+
path: dotPath,
|
|
929
|
+
previous: previous ?? null,
|
|
930
|
+
next: next ?? null
|
|
931
|
+
});
|
|
932
|
+
printLine(`(dry-run) ${dotPath}: ${renderScalar(previous)} → ${renderScalar(next)}`);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
saveDocument(doc, configPath);
|
|
936
|
+
if ('json' === format) return void printJson({
|
|
937
|
+
saved: true,
|
|
938
|
+
configPath,
|
|
939
|
+
path: dotPath,
|
|
940
|
+
previous: previous ?? null,
|
|
941
|
+
next: next ?? null
|
|
942
|
+
});
|
|
943
|
+
printLine(`已更新 ${dotPath}: ${renderScalar(previous)} → ${renderScalar(next)}`);
|
|
944
|
+
printLine(`配置文件:${configPath}`);
|
|
945
|
+
}
|
|
946
|
+
function registerSet(program) {
|
|
947
|
+
program.command('set <dotPath> <value>').description('精确修改配置项(校验通过才写入)').option('-c, --config <path>', '指定配置文件路径').option('--dry-run', '只显示变更,不写入').option('--format <format>', '输出格式:human | json', 'human').action((dotPath, value, options)=>{
|
|
948
|
+
runSet(dotPath, value, options);
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
const terminal_SIDE_LABEL = {
|
|
952
|
+
frontend: '前端',
|
|
953
|
+
backend: '后端'
|
|
954
|
+
};
|
|
955
|
+
function collect(value, previous) {
|
|
956
|
+
return [
|
|
957
|
+
...previous,
|
|
958
|
+
value
|
|
959
|
+
];
|
|
960
|
+
}
|
|
961
|
+
function assertAbsolute(value, field) {
|
|
962
|
+
const trimmed = value.trim();
|
|
963
|
+
if (0 === trimmed.length) throw new errors_CliError(`${field} 不能为空`, errors_EXIT.USAGE);
|
|
964
|
+
if (!isAbsolute(trimmed)) throw new errors_CliError(`${field} 必须是绝对路径:${value}`, errors_EXIT.USAGE);
|
|
965
|
+
return trimmed;
|
|
966
|
+
}
|
|
967
|
+
function parseSide(value) {
|
|
968
|
+
const trimmed = value?.trim();
|
|
969
|
+
if (void 0 === trimmed || 0 === trimmed.length) throw new errors_CliError('必须指定 --side frontend|backend', errors_EXIT.USAGE);
|
|
970
|
+
if ('frontend' === trimmed || 'backend' === trimmed) return trimmed;
|
|
971
|
+
throw new errors_CliError(`--side 必须是 frontend / backend:${trimmed}`, errors_EXIT.USAGE);
|
|
972
|
+
}
|
|
973
|
+
function terminal_splitAliases(value) {
|
|
974
|
+
const trimmed = value?.trim() ?? '';
|
|
975
|
+
if (0 === trimmed.length) return [];
|
|
976
|
+
return trimmed.split(',').map((item)=>item.trim()).filter((item)=>item.length > 0);
|
|
977
|
+
}
|
|
978
|
+
function readSideList(doc, key, side) {
|
|
979
|
+
const value = getIn(doc.toJS(), [
|
|
980
|
+
'terminals',
|
|
981
|
+
key,
|
|
982
|
+
'repos',
|
|
983
|
+
side
|
|
984
|
+
]);
|
|
985
|
+
if (!Array.isArray(value)) return [];
|
|
986
|
+
return value.filter((item)=>'string' == typeof item);
|
|
987
|
+
}
|
|
988
|
+
function runTerminalList(options) {
|
|
989
|
+
const format = parseFormat(options.format);
|
|
990
|
+
const ready = requireConfig(options.config);
|
|
991
|
+
if ('json' === format) return void printJson({
|
|
992
|
+
configPath: ready.path,
|
|
993
|
+
terminals: ready.config.terminals
|
|
994
|
+
});
|
|
995
|
+
const names = Object.keys(ready.config.terminals);
|
|
996
|
+
if (0 === names.length) return void printLine('(空)运行 `ylcwf terminal add <终端名> --frontend <路径>` 添加');
|
|
997
|
+
for (const name of names){
|
|
998
|
+
const terminal = ready.config.terminals[name];
|
|
999
|
+
if (void 0 === terminal) continue;
|
|
1000
|
+
const aliases = 0 === terminal.aliases.length ? '(无)' : terminal.aliases.join('、');
|
|
1001
|
+
printLine(`${name} 别名:${aliases}`);
|
|
1002
|
+
for (const side of SIDES){
|
|
1003
|
+
const targets = terminal.repos[side];
|
|
1004
|
+
if (0 === targets.length) {
|
|
1005
|
+
printLine(` ${terminal_SIDE_LABEL[side]} (未配置)`);
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
targets.forEach((target, index)=>{
|
|
1009
|
+
const mark = existsSync(target) ? picocolors.green(' ✓') : picocolors.red(' ✗');
|
|
1010
|
+
const label = 0 === index ? terminal_SIDE_LABEL[side] : ' '.repeat(terminal_SIDE_LABEL[side].length);
|
|
1011
|
+
printLine(` ${label} ${target}${mark}`);
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
function runTerminalAdd(key, options) {
|
|
1017
|
+
const trimmedKey = key.trim();
|
|
1018
|
+
if (0 === trimmedKey.length) throw new errors_CliError('终端名不能为空', errors_EXIT.USAGE);
|
|
1019
|
+
const { path: configPath, doc } = obtainDocument(options.config);
|
|
1020
|
+
if (void 0 !== getIn(doc.toJS(), [
|
|
1021
|
+
'terminals',
|
|
1022
|
+
trimmedKey
|
|
1023
|
+
])) throw new errors_CliError(`终端已存在:${trimmedKey}\n修改请用:ylcwf terminal repo add|rm,或 terminal set --aliases`, errors_EXIT.USAGE);
|
|
1024
|
+
doc.setIn([
|
|
1025
|
+
'terminals',
|
|
1026
|
+
trimmedKey
|
|
1027
|
+
], {
|
|
1028
|
+
aliases: options.alias ?? [],
|
|
1029
|
+
repos: {
|
|
1030
|
+
frontend: (options.frontend ?? []).map((item)=>assertAbsolute(item, '--frontend')),
|
|
1031
|
+
backend: (options.backend ?? []).map((item)=>assertAbsolute(item, '--backend'))
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
assertWritable(doc, configPath);
|
|
1035
|
+
saveDocument(doc, configPath);
|
|
1036
|
+
printLine(`已添加终端:${trimmedKey}`);
|
|
1037
|
+
printLine(`配置文件:${configPath}`);
|
|
1038
|
+
}
|
|
1039
|
+
function runTerminalSetAliases(key, options) {
|
|
1040
|
+
const trimmedKey = key.trim();
|
|
1041
|
+
const { path: configPath, doc } = obtainDocument(options.config);
|
|
1042
|
+
if (void 0 === getIn(doc.toJS(), [
|
|
1043
|
+
'terminals',
|
|
1044
|
+
trimmedKey
|
|
1045
|
+
])) throw new errors_CliError(`终端不存在:${trimmedKey}\n新增请用:ylcwf terminal add`, errors_EXIT.NOT_FOUND);
|
|
1046
|
+
if (void 0 === options.aliases) throw new errors_CliError('需要指定 --aliases(逗号分隔,整体替换)', errors_EXIT.USAGE);
|
|
1047
|
+
doc.setIn([
|
|
1048
|
+
'terminals',
|
|
1049
|
+
trimmedKey,
|
|
1050
|
+
'aliases'
|
|
1051
|
+
], terminal_splitAliases(options.aliases));
|
|
1052
|
+
assertWritable(doc, configPath);
|
|
1053
|
+
saveDocument(doc, configPath);
|
|
1054
|
+
printLine(`已更新终端别名:${trimmedKey}`);
|
|
1055
|
+
printLine(`配置文件:${configPath}`);
|
|
1056
|
+
}
|
|
1057
|
+
function runTerminalRepoAdd(key, options) {
|
|
1058
|
+
const trimmedKey = key.trim();
|
|
1059
|
+
const side = parseSide(options.side);
|
|
1060
|
+
const target = assertAbsolute(options.path ?? '', '--path');
|
|
1061
|
+
const { path: configPath, doc } = obtainDocument(options.config);
|
|
1062
|
+
if (void 0 === getIn(doc.toJS(), [
|
|
1063
|
+
'terminals',
|
|
1064
|
+
trimmedKey
|
|
1065
|
+
])) throw new errors_CliError(`终端不存在:${trimmedKey}\n新增请用:ylcwf terminal add`, errors_EXIT.NOT_FOUND);
|
|
1066
|
+
const list = readSideList(doc, trimmedKey, side);
|
|
1067
|
+
if (list.includes(target)) throw new errors_CliError(`${side} 端已存在该仓库:${target}`, errors_EXIT.USAGE);
|
|
1068
|
+
doc.setIn([
|
|
1069
|
+
'terminals',
|
|
1070
|
+
trimmedKey,
|
|
1071
|
+
'repos',
|
|
1072
|
+
side
|
|
1073
|
+
], [
|
|
1074
|
+
...list,
|
|
1075
|
+
target
|
|
1076
|
+
]);
|
|
1077
|
+
assertWritable(doc, configPath);
|
|
1078
|
+
saveDocument(doc, configPath);
|
|
1079
|
+
printLine(`已为「${trimmedKey}」的 ${side} 端添加仓库:${target}(共 ${list.length + 1} 个)`);
|
|
1080
|
+
}
|
|
1081
|
+
function runTerminalRepoRemove(key, options) {
|
|
1082
|
+
const trimmedKey = key.trim();
|
|
1083
|
+
const side = parseSide(options.side);
|
|
1084
|
+
const target = assertAbsolute(options.path ?? '', '--path');
|
|
1085
|
+
const { path: configPath, doc } = obtainDocument(options.config);
|
|
1086
|
+
const list = readSideList(doc, trimmedKey, side);
|
|
1087
|
+
if (0 === list.length) throw new errors_CliError(`终端「${trimmedKey}」的 ${side} 端没有仓库`, errors_EXIT.NOT_FOUND);
|
|
1088
|
+
if (!list.includes(target)) throw new errors_CliError(`${side} 端不存在该仓库:${target}\n现有:\n${list.map((item, index)=>` ${index + 1}) ${item}`).join('\n')}`, errors_EXIT.NOT_FOUND);
|
|
1089
|
+
const next = list.filter((item)=>item !== target);
|
|
1090
|
+
doc.setIn([
|
|
1091
|
+
'terminals',
|
|
1092
|
+
trimmedKey,
|
|
1093
|
+
'repos',
|
|
1094
|
+
side
|
|
1095
|
+
], next);
|
|
1096
|
+
assertWritable(doc, configPath);
|
|
1097
|
+
saveDocument(doc, configPath);
|
|
1098
|
+
printLine(`已移除「${trimmedKey}」的 ${side} 端仓库:${target}(剩余 ${next.length} 个)`);
|
|
1099
|
+
}
|
|
1100
|
+
function runTerminalRepoList(key, options) {
|
|
1101
|
+
const trimmedKey = key.trim();
|
|
1102
|
+
const format = parseFormat(options.format);
|
|
1103
|
+
const ready = requireConfig(options.config);
|
|
1104
|
+
const terminal = ready.config.terminals[trimmedKey];
|
|
1105
|
+
if (void 0 === terminal) throw new errors_CliError(`终端不存在:${trimmedKey}`, errors_EXIT.NOT_FOUND);
|
|
1106
|
+
const sides = void 0 === options.side ? [
|
|
1107
|
+
...SIDES
|
|
1108
|
+
] : [
|
|
1109
|
+
parseSide(options.side)
|
|
1110
|
+
];
|
|
1111
|
+
if ('json' === format) {
|
|
1112
|
+
const payload = {};
|
|
1113
|
+
for (const side of sides)payload[side] = terminal.repos[side];
|
|
1114
|
+
printJson({
|
|
1115
|
+
terminal: trimmedKey,
|
|
1116
|
+
repos: payload
|
|
1117
|
+
});
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
for (const side of sides){
|
|
1121
|
+
const targets = terminal.repos[side];
|
|
1122
|
+
if (0 === targets.length) {
|
|
1123
|
+
printLine(`${side}(未配置)`);
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
targets.forEach((target, index)=>{
|
|
1127
|
+
printLine(`${side} ${index + 1}) ${target}${existsSync(target) ? '' : ' (路径不存在)'}`);
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
async function runTerminalRemove(key, options) {
|
|
1132
|
+
const trimmedKey = key.trim();
|
|
1133
|
+
const ready = requireConfig(options.config);
|
|
1134
|
+
if (void 0 === ready.config.terminals[trimmedKey]) throw new errors_CliError(`终端不存在:${trimmedKey}`, errors_EXIT.NOT_FOUND);
|
|
1135
|
+
if (true !== options.yes) {
|
|
1136
|
+
if (true !== process.stdin.isTTY) throw new errors_CliError('非交互环境删除终端需要显式加 --yes', errors_EXIT.USAGE);
|
|
1137
|
+
const { confirm } = await import("@clack/prompts");
|
|
1138
|
+
const answer = await confirm({
|
|
1139
|
+
message: `确认删除终端「${trimmedKey}」?`,
|
|
1140
|
+
initialValue: false
|
|
1141
|
+
});
|
|
1142
|
+
if (true !== answer) return void printLine('已取消');
|
|
1143
|
+
}
|
|
1144
|
+
ready.doc.deleteIn([
|
|
1145
|
+
'terminals',
|
|
1146
|
+
trimmedKey
|
|
1147
|
+
]);
|
|
1148
|
+
assertWritable(ready.doc, ready.path);
|
|
1149
|
+
saveDocument(ready.doc, ready.path);
|
|
1150
|
+
printLine(`已删除终端:${trimmedKey}`);
|
|
1151
|
+
}
|
|
1152
|
+
function registerTerminal(program) {
|
|
1153
|
+
const terminal = program.command('terminal').description('维护 PRD 终端 → 本地仓库映射');
|
|
1154
|
+
terminal.command('list').description('列出全部终端').option('-c, --config <path>', '指定配置文件路径').option('--format <format>', '输出格式:human | json', 'human').action((options)=>{
|
|
1155
|
+
runTerminalList(options);
|
|
1156
|
+
});
|
|
1157
|
+
terminal.command('add <key>').description('新增终端(终端名用 PRD 中的取值,可为中文)').option('-c, --config <path>', '指定配置文件路径').option('--frontend <path>', '前端仓库绝对路径,可重复', collect, []).option('--backend <path>', '后端仓库绝对路径,可重复', collect, []).option('--alias <name>', '别名,可重复', collect, []).action((key, options)=>{
|
|
1158
|
+
runTerminalAdd(key, options);
|
|
1159
|
+
});
|
|
1160
|
+
terminal.command('set <key>').description('修改终端别名(仓库增删用 terminal repo)').option('-c, --config <path>', '指定配置文件路径').option('--aliases <list>', '别名,逗号分隔(整体替换)').action((key, options)=>{
|
|
1161
|
+
runTerminalSetAliases(key, options);
|
|
1162
|
+
});
|
|
1163
|
+
terminal.command('rm <key>').description('删除终端').option('-c, --config <path>', '指定配置文件路径').option('--yes', '跳过确认').action(async (key, options)=>{
|
|
1164
|
+
await runTerminalRemove(key, options);
|
|
1165
|
+
});
|
|
1166
|
+
const repo = terminal.command('repo').description('维护单个终端某一端的仓库列表');
|
|
1167
|
+
repo.command('list <key>').description('列出仓库').option('-c, --config <path>', '指定配置文件路径').option('--side <side>', '限定端口:frontend | backend').option('--format <format>', '输出格式:human | json', 'human').action((key, options)=>{
|
|
1168
|
+
runTerminalRepoList(key, options);
|
|
1169
|
+
});
|
|
1170
|
+
repo.command('add <key>').description('为某一端追加仓库').option('-c, --config <path>', '指定配置文件路径').option('--side <side>', '端口:frontend | backend').option('--path <path>', '仓库绝对路径').action((key, options)=>{
|
|
1171
|
+
runTerminalRepoAdd(key, options);
|
|
1172
|
+
});
|
|
1173
|
+
repo.command('rm <key>').description('从某一端移除仓库').option('-c, --config <path>', '指定配置文件路径').option('--side <side>', '端口:frontend | backend').option('--path <path>', '仓库绝对路径').action((key, options)=>{
|
|
1174
|
+
runTerminalRepoRemove(key, options);
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
function matched(config, name, kind) {
|
|
1178
|
+
const entry = config.terminals[name] ?? null;
|
|
1179
|
+
return {
|
|
1180
|
+
input: name,
|
|
1181
|
+
matched: null !== entry,
|
|
1182
|
+
matchedBy: null === entry ? null : kind,
|
|
1183
|
+
terminal: null === entry ? null : name,
|
|
1184
|
+
entry,
|
|
1185
|
+
candidates: []
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
const SUGGEST_DISTANCE = 3;
|
|
1189
|
+
function suggest(config, target) {
|
|
1190
|
+
if (0 === target.length) return [];
|
|
1191
|
+
const scored = [];
|
|
1192
|
+
for (const [name, entry] of Object.entries(config.terminals)){
|
|
1193
|
+
const pool = [
|
|
1194
|
+
normalizeName(name),
|
|
1195
|
+
...entry.aliases.map((alias)=>normalizeName(alias))
|
|
1196
|
+
].filter((item)=>item.length > 0);
|
|
1197
|
+
let best = 1 / 0;
|
|
1198
|
+
for (const candidate of pool){
|
|
1199
|
+
if (candidate.includes(target) || target.includes(candidate)) {
|
|
1200
|
+
best = Math.min(best, 1);
|
|
1201
|
+
continue;
|
|
1202
|
+
}
|
|
1203
|
+
best = Math.min(best, editDistance(candidate, target));
|
|
1204
|
+
}
|
|
1205
|
+
if (best <= SUGGEST_DISTANCE) scored.push({
|
|
1206
|
+
name,
|
|
1207
|
+
score: best
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
return scored.sort((left, right)=>left.score - right.score).slice(0, 3).map((item)=>item.name);
|
|
1211
|
+
}
|
|
1212
|
+
function resolveTerminal(config, input) {
|
|
1213
|
+
const raw = input.trim();
|
|
1214
|
+
const names = Object.keys(config.terminals);
|
|
1215
|
+
if (Object.hasOwn(config.terminals, raw)) return matched(config, raw, 'exact');
|
|
1216
|
+
for (const name of names){
|
|
1217
|
+
const entry = config.terminals[name];
|
|
1218
|
+
if (void 0 !== entry) {
|
|
1219
|
+
if (entry.aliases.includes(raw)) return matched(config, name, 'alias');
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
const target = normalizeName(raw);
|
|
1223
|
+
if (target.length > 0) {
|
|
1224
|
+
const byKey = names.find((name)=>normalizeName(name) === target);
|
|
1225
|
+
if (void 0 !== byKey) return matched(config, byKey, 'normalized');
|
|
1226
|
+
for (const name of names){
|
|
1227
|
+
const entry = config.terminals[name];
|
|
1228
|
+
if (void 0 !== entry) {
|
|
1229
|
+
if (entry.aliases.some((alias)=>normalizeName(alias) === target)) return matched(config, name, 'normalized');
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
return {
|
|
1234
|
+
input: raw,
|
|
1235
|
+
matched: false,
|
|
1236
|
+
matchedBy: null,
|
|
1237
|
+
terminal: null,
|
|
1238
|
+
entry: null,
|
|
1239
|
+
candidates: suggest(config, target)
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
const FIELDS = [
|
|
1243
|
+
'path',
|
|
1244
|
+
'exists',
|
|
1245
|
+
'terminal',
|
|
1246
|
+
'repos'
|
|
1247
|
+
];
|
|
1248
|
+
function isSide(value) {
|
|
1249
|
+
return SIDES.some((side)=>side === value);
|
|
1250
|
+
}
|
|
1251
|
+
function isField(value) {
|
|
1252
|
+
return FIELDS.some((field)=>field === value);
|
|
1253
|
+
}
|
|
1254
|
+
function buildRepoView(terminal) {
|
|
1255
|
+
const view = {
|
|
1256
|
+
frontend: [],
|
|
1257
|
+
backend: []
|
|
1258
|
+
};
|
|
1259
|
+
for (const side of SIDES)view[side] = terminal.repos[side].map((path)=>({
|
|
1260
|
+
path,
|
|
1261
|
+
exists: existsSync(path)
|
|
1262
|
+
}));
|
|
1263
|
+
return view;
|
|
1264
|
+
}
|
|
1265
|
+
function formatCandidates(items) {
|
|
1266
|
+
return items.map((item, index)=>` ${index + 1}) ${item.path}${item.exists ? '' : '(路径不存在)'}`).join('\n');
|
|
1267
|
+
}
|
|
1268
|
+
function pickRepo(items, pick) {
|
|
1269
|
+
const wanted = pick.trim();
|
|
1270
|
+
const index = Number.parseInt(wanted, 10);
|
|
1271
|
+
if (String(index) === wanted && index >= 1 && index <= items.length) {
|
|
1272
|
+
const hit = items[index - 1];
|
|
1273
|
+
if (void 0 !== hit) return hit;
|
|
1274
|
+
}
|
|
1275
|
+
const byPath = items.find((item)=>item.path === wanted);
|
|
1276
|
+
if (void 0 !== byPath) return byPath;
|
|
1277
|
+
const byName = items.find((item)=>basename(item.path) === wanted || basename(item.path).toLowerCase() === wanted.toLowerCase());
|
|
1278
|
+
if (void 0 !== byName) return byName;
|
|
1279
|
+
throw new errors_CliError(`--pick 未匹配到仓库:${pick}\n候选:\n${formatCandidates(items)}`, errors_EXIT.NOT_FOUND);
|
|
1280
|
+
}
|
|
1281
|
+
function selectRepos(items, terminal, side, pick, all) {
|
|
1282
|
+
if (0 === items.length) throw new errors_CliError(`终端「${terminal}」未配置 ${side} 仓库`, errors_EXIT.NOT_FOUND);
|
|
1283
|
+
if (all) return [
|
|
1284
|
+
...items
|
|
1285
|
+
];
|
|
1286
|
+
if (void 0 !== pick) return [
|
|
1287
|
+
pickRepo(items, pick)
|
|
1288
|
+
];
|
|
1289
|
+
if (1 === items.length) return [
|
|
1290
|
+
items[0]
|
|
1291
|
+
].filter((item)=>void 0 !== item);
|
|
1292
|
+
throw new errors_CliError(`终端「${terminal}」的 ${side} 端存在多个仓库,必须显式指定(--pick)或询问开发者:\n${formatCandidates(items)}`, errors_EXIT.NOT_FOUND);
|
|
1293
|
+
}
|
|
1294
|
+
function runResolve(name, options) {
|
|
1295
|
+
const format = parseFormat(options.format);
|
|
1296
|
+
const ready = requireConfig(options.config);
|
|
1297
|
+
const result = resolveTerminal(ready.config, name);
|
|
1298
|
+
if (!result.matched || null === result.terminal || null === result.entry) {
|
|
1299
|
+
const hint = 0 === result.candidates.length ? '\n请确认终端名,或先运行:ylcwf terminal add' : `\n候选(需人工确认,禁止自动采用):${result.candidates.join('、')}`;
|
|
1300
|
+
throw new errors_CliError(`未找到终端:${name}${hint}`, errors_EXIT.NOT_FOUND);
|
|
1301
|
+
}
|
|
1302
|
+
const rawSide = options.side?.trim();
|
|
1303
|
+
if (void 0 !== rawSide && rawSide.length > 0 && !isSide(rawSide)) throw new errors_CliError(`--side 必须是 frontend / backend:${rawSide}`, errors_EXIT.USAGE);
|
|
1304
|
+
const side = void 0 !== rawSide && rawSide.length > 0 && isSide(rawSide) ? rawSide : void 0;
|
|
1305
|
+
const rawField = options.field?.trim() ?? 'repos';
|
|
1306
|
+
if (!isField(rawField)) throw new errors_CliError(`--field 必须是 ${FIELDS.join(' / ')} 之一:${rawField}`, errors_EXIT.USAGE);
|
|
1307
|
+
const field = rawField;
|
|
1308
|
+
const terminal = result.terminal;
|
|
1309
|
+
const repos = buildRepoView(result.entry);
|
|
1310
|
+
if ('terminal' === field) {
|
|
1311
|
+
if ('json' === format) printJson({
|
|
1312
|
+
terminal,
|
|
1313
|
+
matchedBy: result.matchedBy
|
|
1314
|
+
});
|
|
1315
|
+
else printLine(terminal);
|
|
1316
|
+
return;
|
|
1317
|
+
}
|
|
1318
|
+
if ('path' === field || 'exists' === field) {
|
|
1319
|
+
if (void 0 === side) throw new errors_CliError(`--field ${field} 需要同时指定 --side frontend|backend`, errors_EXIT.USAGE);
|
|
1320
|
+
const selected = selectRepos(repos[side], terminal, side, options.pick, true === options.all);
|
|
1321
|
+
if ('path' === field) {
|
|
1322
|
+
if ('json' === format) printJson({
|
|
1323
|
+
terminal,
|
|
1324
|
+
side,
|
|
1325
|
+
paths: selected.map((item)=>item.path)
|
|
1326
|
+
});
|
|
1327
|
+
else for (const item of selected)printLine(item.path);
|
|
1328
|
+
return;
|
|
1329
|
+
}
|
|
1330
|
+
if ('json' === format) return void printJson({
|
|
1331
|
+
terminal,
|
|
1332
|
+
side,
|
|
1333
|
+
items: selected
|
|
1334
|
+
});
|
|
1335
|
+
for (const item of selected)printLine(1 === selected.length ? String(item.exists) : `${item.path} ${item.exists}`);
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
if (void 0 !== side) {
|
|
1339
|
+
const selected = selectRepos(repos[side], terminal, side, options.pick, true);
|
|
1340
|
+
if ('json' === format) return void printJson({
|
|
1341
|
+
terminal,
|
|
1342
|
+
side,
|
|
1343
|
+
items: selected
|
|
1344
|
+
});
|
|
1345
|
+
for (const item of selected)printLine(`${item.path} ${item.exists ? '✓' : '✗'}`);
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
if ('json' === format) return void printJson({
|
|
1349
|
+
schemaVersion: ready.config.schemaVersion,
|
|
1350
|
+
input: name,
|
|
1351
|
+
matched: true,
|
|
1352
|
+
matchedBy: result.matchedBy,
|
|
1353
|
+
terminal,
|
|
1354
|
+
repos
|
|
1355
|
+
});
|
|
1356
|
+
printLine(`终端:${terminal}(匹配方式:${result.matchedBy ?? 'exact'})`);
|
|
1357
|
+
for (const item of SIDES){
|
|
1358
|
+
const list = repos[item];
|
|
1359
|
+
if (0 === list.length) {
|
|
1360
|
+
printLine(` ${item} (未配置)`);
|
|
1361
|
+
continue;
|
|
1362
|
+
}
|
|
1363
|
+
list.forEach((repo, index)=>{
|
|
1364
|
+
const prefix = 1 === list.length ? ` ${item} ` : ` ${item} ${index + 1}) `;
|
|
1365
|
+
printLine(`${prefix}${repo.path} ${repo.exists ? '✓' : '✗'}`);
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
function registerResolve(program) {
|
|
1370
|
+
program.command('resolve <name>').description('把终端名(支持中文与别名)解析为本地仓库路径').option('-c, --config <path>', '指定配置文件路径').option('--side <side>', '限定端口:frontend | backend').option('--field <field>', '输出字段:path | exists | terminal | repos', 'repos').option('--pick <repo>', '该端存在多个仓库时显式指定:序号(1 起)、完整路径或目录名').option('--all', '输出该端全部仓库').option('--format <format>', '输出格式:human | json', 'human').action((name, options)=>{
|
|
1371
|
+
runResolve(name, options);
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
function runDoctor(options) {
|
|
1375
|
+
const format = parseFormat(options.format);
|
|
1376
|
+
const loaded = loadConfig(options.config);
|
|
1377
|
+
if (!loaded.exists) throw new errors_CliError(`配置未初始化:${loaded.path}\n请先运行:ylcwf init`, errors_EXIT.NOT_FOUND);
|
|
1378
|
+
const checked = checkConfig(loaded.raw, {
|
|
1379
|
+
checkFs: true
|
|
1380
|
+
});
|
|
1381
|
+
const errors = countIssues(checked.issues).errors;
|
|
1382
|
+
const ok = !hasError(checked.issues);
|
|
1383
|
+
if ('json' === format) printJson({
|
|
1384
|
+
ok,
|
|
1385
|
+
configPath: loaded.path,
|
|
1386
|
+
...countIssues(checked.issues),
|
|
1387
|
+
issues: checked.issues
|
|
1388
|
+
});
|
|
1389
|
+
else {
|
|
1390
|
+
printLine(`配置文件 ${loaded.path}`);
|
|
1391
|
+
printLine(formatIssues(checked.issues, '校验通过,未发现问题'));
|
|
1392
|
+
if (!ok) printLine(`\n校验失败:${errors} 项 error`);
|
|
1393
|
+
}
|
|
1394
|
+
if (!ok) process.exitCode = errors_EXIT.INVALID;
|
|
1395
|
+
}
|
|
1396
|
+
function registerDoctor(program) {
|
|
1397
|
+
program.command('doctor').description('严格校验配置(schema、路径存在性、终端唯一性)').option('-c, --config <path>', '指定配置文件路径').option('--format <format>', '输出格式:human | json', 'human').action((options)=>{
|
|
1398
|
+
runDoctor(options);
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
function runPath(options) {
|
|
1402
|
+
const target = resolveConfigPath(options.config);
|
|
1403
|
+
const format = parseFormat(options.format);
|
|
1404
|
+
if (true === options.exists) {
|
|
1405
|
+
const present = existsSync(target);
|
|
1406
|
+
if ('json' === format) printJson({
|
|
1407
|
+
configPath: target,
|
|
1408
|
+
exists: present
|
|
1409
|
+
});
|
|
1410
|
+
else printLine(present ? 'true' : 'false');
|
|
1411
|
+
if (!present) process.exitCode = errors_EXIT.NOT_FOUND;
|
|
1412
|
+
return;
|
|
1413
|
+
}
|
|
1414
|
+
printLine(target);
|
|
1415
|
+
}
|
|
1416
|
+
function registerPath(program) {
|
|
1417
|
+
program.command('path').description('输出配置文件绝对路径').option('-c, --config <path>', '指定配置文件路径').option('--exists', '改为输出配置文件是否存在(true/false)').option('--format <format>', '输出格式:human | json', 'human').action((options)=>{
|
|
1418
|
+
runPath(options);
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
function buildProgram() {
|
|
1422
|
+
const program = new Command();
|
|
1423
|
+
program.name('ylcwf').description('YLC 研发工作流配置:文档仓库、开发者标识、默认角色与终端仓库映射').version("0.1.0", '-v, --version', '显示版本号').showHelpAfterError();
|
|
1424
|
+
registerInit(program);
|
|
1425
|
+
registerShow(program);
|
|
1426
|
+
registerGet(program);
|
|
1427
|
+
registerSet(program);
|
|
1428
|
+
registerTerminal(program);
|
|
1429
|
+
registerResolve(program);
|
|
1430
|
+
registerDoctor(program);
|
|
1431
|
+
registerPath(program);
|
|
1432
|
+
return program;
|
|
1433
|
+
}
|
|
1434
|
+
function runBare(configPath) {
|
|
1435
|
+
showConfig(configPath, process.stdout.isTTY ? 'human' : 'json');
|
|
1436
|
+
}
|
|
1437
|
+
function splitGlobalArgs(argv) {
|
|
1438
|
+
const rest = [];
|
|
1439
|
+
let config;
|
|
1440
|
+
for(let index = 0; index < argv.length; index += 1){
|
|
1441
|
+
const arg = argv[index] ?? '';
|
|
1442
|
+
if ('-c' === arg || '--config' === arg) {
|
|
1443
|
+
const value = argv[index + 1];
|
|
1444
|
+
if (void 0 !== value) config = value;
|
|
1445
|
+
index += 1;
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
if (arg.startsWith('--config=')) {
|
|
1449
|
+
config = arg.slice(9);
|
|
1450
|
+
continue;
|
|
1451
|
+
}
|
|
1452
|
+
rest.push(arg);
|
|
1453
|
+
}
|
|
1454
|
+
return void 0 === config ? {
|
|
1455
|
+
rest
|
|
1456
|
+
} : {
|
|
1457
|
+
config,
|
|
1458
|
+
rest
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
async function main() {
|
|
1462
|
+
const { config, rest } = splitGlobalArgs(process.argv.slice(2));
|
|
1463
|
+
const wantsMeta = rest.some((arg)=>[
|
|
1464
|
+
'-h',
|
|
1465
|
+
'--help',
|
|
1466
|
+
'-v',
|
|
1467
|
+
'--version'
|
|
1468
|
+
].includes(arg));
|
|
1469
|
+
const hasSubcommand = rest.some((arg)=>!arg.startsWith('-'));
|
|
1470
|
+
if (!hasSubcommand && !wantsMeta) return void runBare(config);
|
|
1471
|
+
await buildProgram().parseAsync(process.argv);
|
|
1472
|
+
}
|
|
1473
|
+
main().catch((error)=>{
|
|
1474
|
+
if (error instanceof errors_CliError) {
|
|
1475
|
+
printError(error.message);
|
|
1476
|
+
process.exit(error.exitCode);
|
|
1477
|
+
}
|
|
1478
|
+
printError(error instanceof Error ? error.stack ?? error.message : String(error));
|
|
1479
|
+
process.exit(errors_EXIT.INVALID);
|
|
1480
|
+
});
|