@rowansenne/workflow-run 0.35.0 → 0.36.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 +23 -1
- package/README.md +2 -0
- package/bin/lib/artifact-context.mjs +1 -1
- package/bin/lib/docsrepo.mjs +4 -6
- package/bin/lib/doctor.mjs +0 -3
- package/bin/lib/install.mjs +15 -15
- package/create-release-md-doc/SKILL.md.tpl +1 -1
- package/create-tech-md-doc/SKILL.md.tpl +1 -1
- package/dev/references/delivery.md +3 -3
- package/docs/architecture.html +3 -2
- package/docs/development.md +10 -4
- package/docs/full-install.html +90 -125
- package/docs/installation.md +1 -1
- package/docs/loan-service-install.html +90 -0
- package/docs/team-guide.md +3 -2
- package/docs/workflow-control-roadmap.md +144 -0
- package/docs/workflow-core-phase-one-report.md +41 -0
- package/docs/workflow-core-phase-one.md +317 -0
- package/docs/workflows.md +2 -2
- package/examples/workflow-core.mjs +26 -0
- package/package.json +2 -1
- package/project-review/SKILL.md.tpl +1 -1
- package/project-review/references/checklist.md.tpl +1 -1
- package/project-review/references/reporting.md +2 -2
- package/workflow/core/errors.mjs +61 -0
- package/workflow/core/index.mjs +286 -0
- package/workflow/core/review-handoff.mjs +101 -0
- package/workflow/integrations/skill-document-policy.mjs +28 -0
- package/workflow/lib/artifact-finalize.mjs +49 -10
- package/workflow/lib/code-snapshot.mjs +5 -0
- package/workflow/lib/meta-store.mjs +12 -1
- package/workflow/lib/project.mjs +165 -14
- package/workflow/lib/requirements.mjs +57 -100
- package/workflow/lib/review-scope.mjs +262 -0
- package/workflow/lib/runs.mjs +11 -1
- package/workflow/lib/workspace-read.mjs +13 -4
- package/workflow/references/architecture.md +10 -5
- package/workflow/references/core-api.md +196 -0
- package/workflow/references/execution.md +4 -4
- package/workflow/references/profile.md +20 -0
- package/workflow/references/requirements.md +12 -12
- package/workflow/scripts/artifact.mjs +36 -14
- package/workflow/scripts/requirement.mjs +36 -40
- package/workflow/scripts/resolve.mjs +2 -2
- package/workflow/scripts/review-handoff.mjs +49 -73
- package/workflow/scripts/review-scope.mjs +78 -294
- package/workflow/scripts/run.mjs +21 -9
package/workflow/lib/project.mjs
CHANGED
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { spawnSync } from 'node:child_process';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
6
5
|
|
|
7
|
-
const SKILLS_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
8
6
|
const INSTALL_BASES = ['.agents/skills', '.claude/skills', '.qwen/skills', '.codebuddy/skills'];
|
|
7
|
+
const INSTALL_ALIASES = {
|
|
8
|
+
codex: '.agents/skills',
|
|
9
|
+
doubao: '.agents/skills',
|
|
10
|
+
claude: '.claude/skills',
|
|
11
|
+
qwen: '.qwen/skills',
|
|
12
|
+
workbuddy: '.codebuddy/skills',
|
|
13
|
+
};
|
|
9
14
|
const git = (cwd, args) => spawnSync('git', args, { cwd, encoding: 'utf8' });
|
|
10
15
|
const readJson = (f) => { try { return JSON.parse(fs.readFileSync(f, 'utf8')); } catch { return null; } };
|
|
11
16
|
const toPosix = (p) => p.split(path.sep).join('/');
|
|
@@ -16,6 +21,7 @@ export const HOME = 'workflow-run';
|
|
|
16
21
|
export const REQ_DIR = `${HOME}/docs/requirements`;
|
|
17
22
|
export const TEMPLATES_DIR = `${HOME}/templates`;
|
|
18
23
|
export const TMP_DIR = `${HOME}/tmp`;
|
|
24
|
+
export const PROFILE_SOURCE = `${HOME}/profile-source.json`;
|
|
19
25
|
export const IGNORE_LINE = `${HOME}/docs/`;
|
|
20
26
|
export const homeDir = (root) => path.join(root, HOME);
|
|
21
27
|
// workflow-run/ 自己是 git 仓库(团队文档仓库克隆到这里)= 独立文档仓库模式
|
|
@@ -32,21 +38,166 @@ export function dirName(name) {
|
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
// ------------------------------------------------------------------ 配置与布局
|
|
35
|
-
|
|
41
|
+
const profileError = (message, code, details = {}) => Object.assign(new Error(message), { code, details });
|
|
42
|
+
const candidateDirs = (root) => INSTALL_BASES
|
|
43
|
+
.map((base) => ({ base, dir: path.join(root, base, 'workflow') }))
|
|
44
|
+
.filter(({ dir }) => { try { return fs.statSync(dir).isDirectory(); } catch { return false; } });
|
|
45
|
+
const samePath = (a, b) => path.relative(path.resolve(a), path.resolve(b)) === '';
|
|
46
|
+
const stableJson = (value) => {
|
|
47
|
+
if (Array.isArray(value)) return value.map(stableJson);
|
|
48
|
+
if (!value || typeof value !== 'object') return value;
|
|
49
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, stableJson(value[key])]));
|
|
50
|
+
};
|
|
51
|
+
const profileFiles = (dir) => ['profile.json', 'profile.local.json'].map((name) => {
|
|
52
|
+
const file = path.join(dir, name);
|
|
53
|
+
if (!fs.existsSync(file)) return null;
|
|
54
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
55
|
+
try { return JSON.stringify(stableJson(JSON.parse(text))); } catch { return `raw:${text}`; }
|
|
56
|
+
});
|
|
57
|
+
// --copy 初次安装的各副本通常完全相同;只有 profile.json / profile.local.json
|
|
58
|
+
// 这两个团队与个人配置都相同时,任选一个才不改变画像语义。
|
|
59
|
+
const equivalentProfiles = (candidates) => {
|
|
60
|
+
if (candidates.length < 2) return true;
|
|
61
|
+
const first = profileFiles(candidates[0].dir);
|
|
62
|
+
return candidates.slice(1).every(({ dir }) => {
|
|
63
|
+
const current = profileFiles(dir);
|
|
64
|
+
return current.every((value, index) => value === first[index]);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const sourceFile = (root) => path.join(root, PROFILE_SOURCE);
|
|
68
|
+
const PROFILE_SOURCE_HINT = '运行 requirement.mjs config --profile-source <codex|claude|qwen|workbuddy|路径> 明确选择';
|
|
69
|
+
const sourceError = (message, details) => profileError(
|
|
70
|
+
`${message};${PROFILE_SOURCE_HINT}`,
|
|
71
|
+
'PROJECT_CONFIG_AMBIGUOUS',
|
|
72
|
+
{ reason: 'invalid_source', source_file: PROFILE_SOURCE, hint: PROFILE_SOURCE_HINT, ...details },
|
|
73
|
+
);
|
|
74
|
+
const readProfileSource = (root, candidates) => {
|
|
75
|
+
const file = sourceFile(root);
|
|
76
|
+
if (!fs.existsSync(file)) return null;
|
|
77
|
+
let value;
|
|
78
|
+
try {
|
|
79
|
+
value = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
80
|
+
} catch {
|
|
81
|
+
throw sourceError(`profile 来源文件不是合法 JSON:${PROFILE_SOURCE}`, { paths: candidates.map(({ base }) => `${base}/workflow`) });
|
|
82
|
+
}
|
|
83
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || typeof value.profile_dir !== 'string' || !value.profile_dir.trim()) {
|
|
84
|
+
throw sourceError(`profile 来源文件缺少非空 profile_dir:${PROFILE_SOURCE}`, { paths: candidates.map(({ base }) => `${base}/workflow`) });
|
|
85
|
+
}
|
|
86
|
+
const configured = candidates.find(({ dir }) => samePath(dir, path.resolve(root, value.profile_dir)));
|
|
87
|
+
if (!configured) {
|
|
88
|
+
throw sourceError(`profile 来源不存在或不是受支持的安装目录:${value.profile_dir}`, {
|
|
89
|
+
profile_dir: value.profile_dir,
|
|
90
|
+
paths: candidates.map(({ base }) => `${base}/workflow`),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return { ...configured, selected_by: 'profile-source', source_file: PROFILE_SOURCE };
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// 项目配置只能来自目标业务项目。规范位置优先;没有规范位置时,只允许一个真实配置目录。
|
|
97
|
+
// 多个不同拷贝会改变基线、产物项目和工具链,不能按目录枚举顺序静默任选。
|
|
98
|
+
export function resolveProfileDir(root) {
|
|
99
|
+
const candidates = candidateDirs(root);
|
|
100
|
+
const configured = readProfileSource(root, candidates);
|
|
101
|
+
if (configured) return { ...configured, source: configured.base, candidates };
|
|
102
|
+
if (!candidates.length) {
|
|
103
|
+
return { dir: path.join(root, INSTALL_BASES[0], 'workflow'), source: null, selected_by: null, candidates: [] };
|
|
104
|
+
}
|
|
105
|
+
const canonical = candidates.find(({ base }) => base === INSTALL_BASES[0]);
|
|
106
|
+
if (canonical) return { ...canonical, source: canonical.base, selected_by: 'canonical', candidates };
|
|
107
|
+
|
|
108
|
+
const unique = [];
|
|
109
|
+
const byRealPath = new Map();
|
|
110
|
+
for (const candidate of candidates) {
|
|
111
|
+
let real;
|
|
112
|
+
try {
|
|
113
|
+
real = fs.realpathSync.native(candidate.dir);
|
|
114
|
+
} catch {
|
|
115
|
+
real = path.resolve(candidate.dir);
|
|
116
|
+
}
|
|
117
|
+
if (!byRealPath.has(real)) byRealPath.set(real, candidate);
|
|
118
|
+
}
|
|
119
|
+
unique.push(...byRealPath.values());
|
|
120
|
+
if (unique.length > 1 && !equivalentProfiles(unique)) {
|
|
121
|
+
const paths = unique.map(({ dir }) => path.relative(root, dir).split(path.sep).join('/'));
|
|
122
|
+
throw profileError(
|
|
123
|
+
`目标项目存在多个内容不同的 workflow 配置目录,无法唯一选择:${paths.join('、')};${PROFILE_SOURCE_HINT}`,
|
|
124
|
+
'PROJECT_CONFIG_AMBIGUOUS',
|
|
125
|
+
{
|
|
126
|
+
reason: 'different_profiles',
|
|
127
|
+
paths,
|
|
128
|
+
source_file: PROFILE_SOURCE,
|
|
129
|
+
hint: PROFILE_SOURCE_HINT,
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
...unique[0],
|
|
135
|
+
source: unique[0].base,
|
|
136
|
+
selected_by: unique.length === 1 ? 'only_candidate' : 'equivalent_profiles',
|
|
137
|
+
candidates,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function profileSources(root) {
|
|
142
|
+
return candidateDirs(root).map(({ base, dir }) => ({
|
|
143
|
+
env: Object.entries(INSTALL_ALIASES).find(([, value]) => value === base)?.[0] ?? base,
|
|
144
|
+
base,
|
|
145
|
+
dir: relOf(root, dir),
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 显式选择只接受目标项目内受支持的安装目录;auto 清除选择并恢复自动解析。
|
|
150
|
+
export function setProfileSource(root, value) {
|
|
151
|
+
const file = sourceFile(root);
|
|
152
|
+
const requested = String(value ?? '').trim();
|
|
153
|
+
if (!requested) throw new Error('profile 来源不能为空');
|
|
154
|
+
if (['auto', 'default'].includes(requested.toLowerCase())) {
|
|
155
|
+
if (fs.existsSync(file)) fs.unlinkSync(file);
|
|
156
|
+
return { ok: true, cleared: true, file: null, candidates: profileSources(root) };
|
|
157
|
+
}
|
|
158
|
+
const alias = INSTALL_ALIASES[requested.toLowerCase()];
|
|
159
|
+
const requestedPath = alias ? path.join(root, alias, 'workflow') : path.resolve(root, requested);
|
|
160
|
+
const supported = candidateDirs(root).find(({ dir }) => samePath(dir, requestedPath));
|
|
161
|
+
if (!supported) {
|
|
162
|
+
throw profileError(
|
|
163
|
+
`profile 来源不存在或不是受支持的安装目录:${requested};${PROFILE_SOURCE_HINT}`,
|
|
164
|
+
'PROJECT_CONFIG_AMBIGUOUS',
|
|
165
|
+
{ reason: 'invalid_source', requested, hint: PROFILE_SOURCE_HINT, paths: profileSources(root).map(({ dir }) => dir), source_file: PROFILE_SOURCE },
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
169
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
170
|
+
fs.writeFileSync(tmp, `${JSON.stringify({ profile_dir: relOf(root, supported.dir) }, null, 2)}\n`);
|
|
171
|
+
fs.renameSync(tmp, file);
|
|
172
|
+
return {
|
|
173
|
+
ok: true,
|
|
174
|
+
cleared: false,
|
|
175
|
+
file: PROFILE_SOURCE,
|
|
176
|
+
profile_dir: relOf(root, supported.dir),
|
|
177
|
+
candidates: profileSources(root),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
36
181
|
export function profileDir(root) {
|
|
37
|
-
|
|
38
|
-
if (fs.existsSync(path.join(own, 'profile.json')) || fs.existsSync(path.join(own, 'profile.local.json'))) return own;
|
|
39
|
-
for (const b of INSTALL_BASES) { const d = path.join(root, b, 'workflow'); if (fs.existsSync(path.join(d, 'profile.json'))) return d; }
|
|
40
|
-
return own;
|
|
182
|
+
return resolveProfileDir(root).dir;
|
|
41
183
|
}
|
|
42
184
|
|
|
43
185
|
// workflow/profile.json 的 artifacts,profile.local.json(个人设置,不提交)覆盖其上
|
|
44
186
|
export function artifactsConfig(root) {
|
|
45
|
-
const
|
|
187
|
+
const resolved = resolveProfileDir(root);
|
|
188
|
+
const dir = resolved.dir;
|
|
46
189
|
const team = readJson(path.join(dir, 'profile.json'))?.artifacts ?? {};
|
|
47
190
|
const mine = readJson(path.join(dir, 'profile.local.json'))?.artifacts ?? {};
|
|
48
191
|
const c = { ...team, ...mine };
|
|
49
|
-
return {
|
|
192
|
+
return {
|
|
193
|
+
project: c.project ? String(c.project) : '',
|
|
194
|
+
commit_prompt: c.commit_prompt !== false,
|
|
195
|
+
profile_dir: dir,
|
|
196
|
+
profile_source: {
|
|
197
|
+
selected_by: resolved.selected_by ?? null,
|
|
198
|
+
...(resolved.source_file ? { config: resolved.source_file } : {}),
|
|
199
|
+
},
|
|
200
|
+
};
|
|
50
201
|
}
|
|
51
202
|
|
|
52
203
|
// 产物位置:两种模式都是 workflow-run/docs/requirements/<需求>/<项目>/。
|
|
@@ -126,8 +277,8 @@ export const SHELL_NOTES = {
|
|
|
126
277
|
powershell: 'Windows PowerShell 5.1:没有 && 和 ||,多条命令用 ; 分开;不要用 2>/dev/null、grep、head 等 Unix 命令',
|
|
127
278
|
pwsh: 'PowerShell 7:支持 &&;不要用 2>/dev/null、grep、head 等 Unix 命令',
|
|
128
279
|
};
|
|
129
|
-
export function readEnv() {
|
|
130
|
-
const rec = readJson(path.join(
|
|
280
|
+
export function readEnv(root) {
|
|
281
|
+
const rec = readJson(path.join(profileDir(root), 'profile.local.json'))?.env;
|
|
131
282
|
const env = rec?.os ? { os: rec.os, shell: rec.shell ?? null, source: 'install' } : { os: OS_NAMES[process.platform] ?? process.platform, shell: null, source: 'runtime' };
|
|
132
283
|
const note = SHELL_NOTES[env.shell];
|
|
133
284
|
return { ...env, ...(note ? { note } : {}) };
|
|
@@ -153,7 +304,7 @@ export function expandPath(v, vars = {}, env = process.env) {
|
|
|
153
304
|
// 「没声明」和「显式写空」是两回事:本机写 "" 表示**不使用**团队那个值(用工具自己的默认配置),
|
|
154
305
|
// 所以要按 null / "" 区分来源,不能只看值真不真。
|
|
155
306
|
export function readToolchain(root) {
|
|
156
|
-
const dir =
|
|
307
|
+
const dir = profileDir(root);
|
|
157
308
|
const team = readJson(path.join(dir, 'profile.json')) ?? {};
|
|
158
309
|
const mine = readJson(path.join(dir, 'profile.local.json')) ?? {};
|
|
159
310
|
const declared = (o) => typeof o === 'string';
|
|
@@ -206,13 +357,13 @@ export function checkToolchain(root, { env = process.env } = {}) {
|
|
|
206
357
|
}
|
|
207
358
|
|
|
208
359
|
export function readProfile(root) {
|
|
209
|
-
const f = path.join(
|
|
360
|
+
const f = path.join(profileDir(root), 'profile.json');
|
|
210
361
|
const profile = readJson(f);
|
|
211
362
|
const get = (k) => k.split('.').reduce((o, p) => (o == null ? undefined : o[p]), profile);
|
|
212
363
|
const isEmpty = (v) => v == null || v === '' || (Array.isArray(v) && !v.length);
|
|
213
364
|
const empty = profile ? PROFILE_FIELDS.filter((k) => isEmpty(get(k))) : PROFILE_FIELDS;
|
|
214
365
|
return {
|
|
215
|
-
path: relOf(root, f), exists: !!profile, profile: profile ?? {}, empty, env: readEnv(), toolchain: readToolchain(root),
|
|
366
|
+
path: relOf(root, f), exists: !!profile, profile: profile ?? {}, empty, env: readEnv(root), toolchain: readToolchain(root),
|
|
216
367
|
complete: !!profile && PROFILE_REQUIRED.every((k) => !empty.includes(k)),
|
|
217
368
|
};
|
|
218
369
|
}
|
|
@@ -13,55 +13,83 @@ const relOf = (root, abs) => path.relative(root, abs).split(path.sep).join('/');
|
|
|
13
13
|
const toPosix = (p) => p.split(path.sep).join('/');
|
|
14
14
|
const now = () => new Date().toISOString();
|
|
15
15
|
const isDir = (p) => { try { return fs.statSync(p).isDirectory(); } catch { return false; } };
|
|
16
|
-
const skipDir = (d) => !d.isDirectory() || d.name.startsWith('.');
|
|
17
16
|
|
|
18
17
|
// 同种产物只留最新版,文件名固定(review.md、tech-doc.md …),文档里的引用不会失效。版本号记在 meta.json 的 versions。
|
|
19
|
-
// path 时把当前版复制到 workflow-run/tmp/old-artifacts/<需求目录(相对 requirements/)>/<种类>_v<N>.md(不提交),
|
|
20
|
-
// 写完 sync <path> 时新版内容和这份副本不同才算新版本(versions 加一);没写就放弃的话版本号不变
|
|
21
18
|
export const OLD_DIR = `${TMP_DIR}/old-artifacts`;
|
|
22
19
|
export const archivePath = (root, dirAbs, kind, v) => path.join(root, OLD_DIR, path.relative(path.join(root, REQ_DIR), dirAbs), `${FILES[kind]}_v${v}.md`);
|
|
23
|
-
const versionsOf = (dirAbs) => readJson(path.join(dirAbs, 'meta.json'))?.versions ?? {};
|
|
24
20
|
|
|
25
|
-
export function indexArtifacts(dir, versions = {}) {
|
|
21
|
+
export function indexArtifacts(dir, versions = {}, { requireVersion = false } = {}) {
|
|
26
22
|
return fs.readdirSync(dir).map((name) => {
|
|
27
23
|
const p = parseFile(name);
|
|
28
24
|
if (!p) return null;
|
|
29
25
|
const text = fs.readFileSync(path.join(dir, name), 'utf8');
|
|
30
|
-
|
|
26
|
+
const fixed = name === `${FILES[p.kind]}.md`;
|
|
27
|
+
const registered = !fixed || Object.hasOwn(versions, p.kind);
|
|
28
|
+
return {
|
|
29
|
+
kind: p.kind,
|
|
30
|
+
version: fixed ? (registered ? versions[p.kind] : null) : p.version,
|
|
31
|
+
file: name,
|
|
32
|
+
branch: parseBranch(text),
|
|
33
|
+
date: headerDate(text),
|
|
34
|
+
wiki: parseWiki(text),
|
|
35
|
+
docnexus: parseDocnexus(text),
|
|
36
|
+
...(requireVersion && !registered ? { registered: false } : {}),
|
|
37
|
+
};
|
|
31
38
|
}).filter(Boolean).sort((a, b) => Object.keys(FILES).indexOf(a.kind) - Object.keys(FILES).indexOf(b.kind) || a.version - b.version);
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
export function sync(root, dirRel, { bump = false } = {}) {
|
|
41
|
+
// 只重建派生索引。versions、runs、绑定、批准和证据均保持原值;版本无法确认时标为未登记。
|
|
42
|
+
export function reindexRequirement(root, dirRel) {
|
|
37
43
|
const dir = path.resolve(root, dirRel);
|
|
38
44
|
const f = path.join(dir, 'meta.json');
|
|
39
|
-
|
|
45
|
+
let changed = false;
|
|
40
46
|
const result = updateMeta(f, (old) => {
|
|
41
47
|
const versions = { ...(old.versions ?? {}) };
|
|
42
|
-
|
|
43
|
-
for (const kind of Object.keys(FILES)) {
|
|
44
|
-
const cur = path.join(dir, `${FILES[kind]}.md`);
|
|
45
|
-
const v = versions[kind] ?? 1;
|
|
46
|
-
const arc = archivePath(root, dir, kind, v);
|
|
47
|
-
if (!fs.existsSync(cur) || !fs.existsSync(arc) || fs.readFileSync(arc).equals(fs.readFileSync(cur))) continue;
|
|
48
|
-
versions[kind] = v + 1;
|
|
49
|
-
archived.push({ kind, version: v, file: relOf(root, arc) });
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
const artifacts = indexArtifacts(dir, versions);
|
|
53
|
-
const branches = old.branches ?? []; // 绑定只由 bind 更新;文档头不隐式修改需求归属。
|
|
48
|
+
const artifacts = indexArtifacts(dir, versions, { requireVersion: true });
|
|
54
49
|
const latest = Object.fromEntries(Object.keys(FILES).map((k) => [k, artifacts.filter((a) => a.kind === k).at(-1)?.file ?? null]));
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
changed = JSON.stringify(old.artifacts ?? null) !== JSON.stringify(artifacts)
|
|
51
|
+
|| JSON.stringify(old.latest ?? null) !== JSON.stringify(latest);
|
|
52
|
+
const updateIndexTime = changed || !old.indexed_at;
|
|
57
53
|
const meta = {
|
|
58
54
|
...old,
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
latest,
|
|
56
|
+
...(Object.keys(versions).length ? { versions } : {}),
|
|
57
|
+
artifacts,
|
|
58
|
+
...(updateIndexTime ? { indexed_at: now() } : {}),
|
|
61
59
|
};
|
|
62
60
|
return meta;
|
|
63
61
|
}, { root });
|
|
64
|
-
return
|
|
62
|
+
return {
|
|
63
|
+
...result,
|
|
64
|
+
changed,
|
|
65
|
+
unregistered: result.artifacts.filter((artifact) => artifact.registered === false),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function reindex(root, { branch = null, all = false } = {}) {
|
|
70
|
+
const L = layout(root);
|
|
71
|
+
const targets = all ? requirements(root, L) : [find(root, branch, L)].filter(Boolean);
|
|
72
|
+
if (!all && !targets.length) throw new Error('当前分支没有绑定需求');
|
|
73
|
+
const results = [];
|
|
74
|
+
for (const target of targets) {
|
|
75
|
+
try {
|
|
76
|
+
const meta = reindexRequirement(root, target.dir);
|
|
77
|
+
results.push({
|
|
78
|
+
ok: true,
|
|
79
|
+
dir: target.dir,
|
|
80
|
+
artifacts: meta.artifacts.length,
|
|
81
|
+
artifact_count: meta.artifacts.length,
|
|
82
|
+
latest: meta.latest,
|
|
83
|
+
versions: meta.versions ?? {},
|
|
84
|
+
changed: meta.changed,
|
|
85
|
+
unregistered: meta.unregistered.map(({ kind, file }) => ({ kind, file })),
|
|
86
|
+
});
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!all) throw error;
|
|
89
|
+
results.push({ ok: false, dir: target.dir, error: error.message, ...(error.code ? { code: error.code } : {}) });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return all ? { ok: results.every((entry) => entry.ok), results } : results[0];
|
|
65
93
|
}
|
|
66
94
|
|
|
67
95
|
const notReady = (L) => ({ ok: false, mode: L.mode, ...(L.need_project ? { need_project: true, suggested_project: L.suggested_project } : {}), hint: L.hint });
|
|
@@ -71,7 +99,7 @@ export function which(root, branch) {
|
|
|
71
99
|
if (!L.ok) return notReady(L);
|
|
72
100
|
const r = find(root, branch, L);
|
|
73
101
|
const where = L.mode === 'repo' ? { mode: 'repo', repo: L.repo_rel, project: L.project } : { mode: 'project', project: L.project };
|
|
74
|
-
if (r) return { bound: true, ...where, branch, name: r.name, dir: r.dir, meta: r.meta
|
|
102
|
+
if (r) return { bound: true, ...where, branch, name: r.name, dir: r.dir, meta: r.meta };
|
|
75
103
|
const recent = requirements(root, L).sort((a, b) => String(b.updated).localeCompare(String(a.updated))).slice(0, 5).map(({ name, dir, branches, updated }) => ({ name, dir, branches, updated }));
|
|
76
104
|
// 其他项目已有的需求(跨服务需求,本项目可以用同一个名字加入);文档仓库里常见,随代码提交时一般没有
|
|
77
105
|
const mine = new Set(requirements(root, L).map((x) => x.slug));
|
|
@@ -111,29 +139,7 @@ export function bind(root, { name = null, branch, base = null }) {
|
|
|
111
139
|
return value;
|
|
112
140
|
}, { root });
|
|
113
141
|
const rel = relOf(root, dir);
|
|
114
|
-
return { ok: true, mode: L.mode, created: !existedHere, joined: !!target && !existedHere, name: meta.name, named_by: meta.named_by, dir: rel, project: L.project, branches:
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// 本次落盘路径:文件名固定(review.md …),已有时先把当前版复制到 tmp/old-artifacts/<种类>_v<N>.md(previous,复审对照用),
|
|
118
|
-
// 并带上它第一行的 wiki 发布记录;新版直接写到 path(覆盖当前版),写完 sync <path> 确认版本号
|
|
119
|
-
export function nextPath(root, kind, branch) {
|
|
120
|
-
if (!FILES[kind]) return { code: 1, out: { ok: false, error: `未知产物种类「${kind}」:${Object.keys(FILES).join(' / ')}` } };
|
|
121
|
-
const L = layout(root);
|
|
122
|
-
if (!L.ok) return { code: 2, out: notReady(L) };
|
|
123
|
-
const r = find(root, branch, L);
|
|
124
|
-
if (!r) return { code: 2, out: { ok: false, ...which(root, branch) } };
|
|
125
|
-
if (r.meta.pending_finalization) return { code: 1, out: { ok: false, hint: `产物收尾尚未完成,先运行 artifact.mjs resume --id ${r.meta.pending_finalization.run_id}` } };
|
|
126
|
-
const abs = path.join(r.abs, `${FILES[kind]}.md`);
|
|
127
|
-
let v = 1; let previous = null; let record = null;
|
|
128
|
-
if (fs.existsSync(abs)) {
|
|
129
|
-
const cur = versionsOf(r.abs)[kind] ?? 1;
|
|
130
|
-
const arc = archivePath(root, r.abs, kind, cur);
|
|
131
|
-
fs.mkdirSync(path.dirname(arc), { recursive: true });
|
|
132
|
-
fs.copyFileSync(abs, arc);
|
|
133
|
-
v = cur + 1; previous = relOf(root, arc);
|
|
134
|
-
record = /^<!-- md-to-wiki: [^\n]*-->/.exec(fs.readFileSync(abs, 'utf8'))?.[0] ?? null;
|
|
135
|
-
}
|
|
136
|
-
return { code: 0, out: { ok: true, mode: L.mode, name: r.name, dir: r.dir, path: relOf(root, abs), cite: L.ref(abs), version: v, previous, wiki_record: record } };
|
|
142
|
+
return { ok: true, mode: L.mode, created: !existedHere, joined: !!target && !existedHere, name: meta.name, named_by: meta.named_by, dir: rel, project: L.project, branches: meta.branches };
|
|
137
143
|
}
|
|
138
144
|
|
|
139
145
|
export function list(root) {
|
|
@@ -173,52 +179,3 @@ export function pending(root, branch, dirRel = null) {
|
|
|
173
179
|
...(kn.length ? { knowledge_files: kn, knowledge_commands: [`git -C ${JSON.stringify(L.repo_rel)} add -- docnexus`, `git -C ${JSON.stringify(L.repo_rel)} commit -m "knowledge: <沉淀了什么>"`] } : {}),
|
|
174
180
|
};
|
|
175
181
|
}
|
|
176
|
-
|
|
177
|
-
// ------------------------------------------------------------------ 旧结构迁移
|
|
178
|
-
// 旧版本随代码提交时没有 <项目名>/ 这一层:文件直接放在 docs/requirements/<需求>/ 下。
|
|
179
|
-
// 列出这样的需求目录(目录里直接有文件的;隐藏文件不算)
|
|
180
|
-
export function legacyRequirements(root) {
|
|
181
|
-
const base = path.join(root, REQ_DIR);
|
|
182
|
-
if (!isDir(base)) return [];
|
|
183
|
-
return fs.readdirSync(base, { withFileTypes: true }).filter((d) => !skipDir(d)).map((d) => d.name)
|
|
184
|
-
.filter((slug) => fs.readdirSync(path.join(base, slug), { withFileTypes: true }).some((e) => e.isFile() && !e.name.startsWith('.')));
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// 把旧结构的需求目录挪到 <需求>/<项目名>/:被 git 跟踪的用 git mv(保留历史),其余直接移动;
|
|
188
|
-
// tmp/old-artifacts/<需求>/ 下归档的上一版也一起挪,复审对照还能找到。
|
|
189
|
-
// 目标里已有同名文件时这个需求整个跳过,留给用户处理。项目名没配置时按 layout 自动取的写进 profile.json。
|
|
190
|
-
// 返回 { project, moved: [{ from, to }], skipped: [{ dir, reason }] }
|
|
191
|
-
export function migrateLayout(root) {
|
|
192
|
-
const L = layout(root);
|
|
193
|
-
const slugs = legacyRequirements(root);
|
|
194
|
-
if (!slugs.length) return { project: L.project, moved: [], skipped: [] };
|
|
195
|
-
if (!L.project) return { project: null, moved: [], skipped: slugs.map((s) => ({ dir: `${REQ_DIR}/${s}`, reason: '还没配置项目名' })) };
|
|
196
|
-
const repoOf = (abs) => (L.mode === 'repo' ? L.repo : root);
|
|
197
|
-
const tracked = (abs) => git(repoOf(abs), ['ls-files', '--error-unmatch', '--', path.relative(repoOf(abs), abs)]).status === 0;
|
|
198
|
-
const moved = []; const skipped = [];
|
|
199
|
-
const move = (from, to, useGit) => {
|
|
200
|
-
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
201
|
-
const cwd = repoOf(from);
|
|
202
|
-
if (useGit && tracked(from) && git(cwd, ['mv', '--', path.relative(cwd, from), path.relative(cwd, to)]).status === 0) return;
|
|
203
|
-
fs.renameSync(from, to);
|
|
204
|
-
};
|
|
205
|
-
for (const slug of slugs) {
|
|
206
|
-
const dir = path.join(L.base, slug);
|
|
207
|
-
const files = fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isFile() && !e.name.startsWith('.')).map((e) => e.name);
|
|
208
|
-
const target = path.join(dir, L.project);
|
|
209
|
-
const clash = files.filter((f) => fs.existsSync(path.join(target, f)));
|
|
210
|
-
if (clash.length) { skipped.push({ dir: relOf(root, dir), reason: `${relOf(root, target)} 里已有 ${clash.join('、')}` }); continue; }
|
|
211
|
-
for (const f of files) {
|
|
212
|
-
move(path.join(dir, f), path.join(target, f), true);
|
|
213
|
-
moved.push({ from: relOf(root, path.join(dir, f)), to: relOf(root, path.join(target, f)) });
|
|
214
|
-
}
|
|
215
|
-
const m = readJson(path.join(target, 'meta.json'));
|
|
216
|
-
if (m && m.project !== L.project) fs.writeFileSync(path.join(target, 'meta.json'), `${JSON.stringify({ ...m, project: L.project }, null, 2)}\n`);
|
|
217
|
-
const old = path.join(root, OLD_DIR, slug);
|
|
218
|
-
if (isDir(old)) {
|
|
219
|
-
for (const e of fs.readdirSync(old, { withFileTypes: true })) if (e.isFile()) move(path.join(old, e.name), path.join(old, L.project, e.name), false);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (moved.length && !L.project_configured && fs.existsSync(path.join(profileDir(root), 'profile.json'))) setConfig(root, { project: L.project });
|
|
223
|
-
return { project: L.project, moved, skipped };
|
|
224
|
-
}
|