@ihupoo/solmet 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +154 -2
- package/README.zh.md +155 -0
- package/bin/solmet.js +11 -0
- package/package.json +39 -4
- package/skills/archive/SKILL.md +15 -0
- package/skills/check/SKILL.md +20 -0
- package/skills/grill/SKILL.md +23 -0
- package/skills/grilling/SKILL.md +22 -0
- package/skills/implement/SKILL.md +18 -0
- package/skills/map/SKILL.md +42 -0
- package/skills/memory-recording/SKILL.md +40 -0
- package/skills/spec/SKILL.md +24 -0
- package/skills/spec-writing/SKILL.md +17 -0
- package/skills/start/SKILL.md +25 -0
- package/skills/tdd/SKILL.md +14 -0
- package/src/cli.js +83 -0
- package/src/commands/archive.js +35 -0
- package/src/commands/brief.js +14 -0
- package/src/commands/check.js +71 -0
- package/src/commands/common.js +65 -0
- package/src/commands/doctor.js +208 -0
- package/src/commands/gate.js +148 -0
- package/src/commands/init.js +87 -0
- package/src/commands/map.js +64 -0
- package/src/commands/recall.js +41 -0
- package/src/commands/remember.js +98 -0
- package/src/commands/skill.js +26 -0
- package/src/commands/start.js +49 -0
- package/src/commands/status.js +76 -0
- package/src/lib/agents.js +79 -0
- package/src/lib/brief.js +78 -0
- package/src/lib/catalog.js +71 -0
- package/src/lib/check.js +360 -0
- package/src/lib/config.js +55 -0
- package/src/lib/detect.js +58 -0
- package/src/lib/fsx.js +117 -0
- package/src/lib/labels.js +92 -0
- package/src/lib/map.js +261 -0
- package/src/lib/memory.js +273 -0
- package/src/lib/paths.js +10 -0
- package/src/lib/router.js +49 -0
- package/src/lib/task.js +189 -0
- package/src/lib/title.js +43 -0
- package/src/lib/version.js +7 -0
- package/templates/NOTES.md +17 -0
- package/templates/PRD.md +25 -0
- package/templates/PROJECT-CONTEXT.md +25 -0
- package/templates/TASK.md +7 -0
package/src/lib/check.js
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { ensureDir, nowIso, readText, stamp, writeJson, writeText } from "./fsx.js";
|
|
5
|
+
import { sha256Text, writeTask } from "./task.js";
|
|
6
|
+
import { shortTitle } from "./title.js";
|
|
7
|
+
import { checkBi, classificationBi } from "./labels.js";
|
|
8
|
+
|
|
9
|
+
const ORDER = ["build", "typecheck", "lint", "test"];
|
|
10
|
+
|
|
11
|
+
function tail(text, limit = 4000) {
|
|
12
|
+
const value = String(text || "").trim();
|
|
13
|
+
return value.length > limit ? `...${value.slice(-limit)}` : value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function runOne(name, command, cwd) {
|
|
17
|
+
const startedAt = nowIso();
|
|
18
|
+
const result = spawnSync(command, {
|
|
19
|
+
cwd,
|
|
20
|
+
shell: true,
|
|
21
|
+
encoding: "utf8",
|
|
22
|
+
timeout: 10 * 60 * 1000,
|
|
23
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
24
|
+
windowsHide: true,
|
|
25
|
+
});
|
|
26
|
+
const exitCode = result.error
|
|
27
|
+
? result.error.code === "ETIMEDOUT" ? 124 : 1
|
|
28
|
+
: result.status ?? 1;
|
|
29
|
+
return {
|
|
30
|
+
name,
|
|
31
|
+
command,
|
|
32
|
+
started_at: startedAt,
|
|
33
|
+
finished_at: nowIso(),
|
|
34
|
+
exit_code: exitCode,
|
|
35
|
+
stdout_tail: tail(result.stdout),
|
|
36
|
+
stderr_tail: tail(result.stderr),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function runChecks(root, config, taskDir, options = {}) {
|
|
41
|
+
const commands = config.commands || {};
|
|
42
|
+
const selected = Array.isArray(options.names) && options.names.length
|
|
43
|
+
? options.names.filter((name) => String(commands[name] || "").trim())
|
|
44
|
+
: ORDER.filter((name) => String(commands[name] || "").trim());
|
|
45
|
+
if (selected.length === 0) {
|
|
46
|
+
throw new Error("没有配置检查命令;请编辑 .solmet/config.json 的 commands");
|
|
47
|
+
}
|
|
48
|
+
const evidenceDir = path.join(taskDir, "evidence");
|
|
49
|
+
ensureDir(evidenceDir);
|
|
50
|
+
const results = [];
|
|
51
|
+
for (const name of selected) {
|
|
52
|
+
const result = runOne(name, commands[name], root);
|
|
53
|
+
const log = path.join(evidenceDir, `${stamp()}-${name}.log`);
|
|
54
|
+
writeText(
|
|
55
|
+
log,
|
|
56
|
+
[
|
|
57
|
+
`command: ${result.command}`,
|
|
58
|
+
`exit_code: ${result.exit_code}`,
|
|
59
|
+
`started_at: ${result.started_at}`,
|
|
60
|
+
`finished_at: ${result.finished_at}`,
|
|
61
|
+
"",
|
|
62
|
+
"--- stdout ---",
|
|
63
|
+
result.stdout_tail,
|
|
64
|
+
"",
|
|
65
|
+
"--- stderr ---",
|
|
66
|
+
result.stderr_tail,
|
|
67
|
+
"",
|
|
68
|
+
].join("\n")
|
|
69
|
+
);
|
|
70
|
+
results.push({ ...result, log });
|
|
71
|
+
if (result.exit_code !== 0 && options.stopOnFailure !== false) break;
|
|
72
|
+
}
|
|
73
|
+
return results;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function vcsCommand(root, config, args) {
|
|
77
|
+
const cwd = config.vcs_root || root;
|
|
78
|
+
const command = config.vcs === "git" ? "git" : config.vcs === "svn" ? "svn" : "";
|
|
79
|
+
if (!command) return null;
|
|
80
|
+
const result = spawnSync(command, args, {
|
|
81
|
+
cwd,
|
|
82
|
+
encoding: "utf8",
|
|
83
|
+
timeout: 15000,
|
|
84
|
+
windowsHide: true,
|
|
85
|
+
});
|
|
86
|
+
return result.status === 0 ? result : null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function changedFiles(root, config) {
|
|
90
|
+
const result = config.vcs === "git"
|
|
91
|
+
? vcsCommand(root, config, ["status", "--porcelain"])
|
|
92
|
+
: vcsCommand(root, config, ["status"]);
|
|
93
|
+
if (!result) return [];
|
|
94
|
+
return String(result.stdout || "")
|
|
95
|
+
.split(/\r?\n/)
|
|
96
|
+
.map((line) => line.replace(/^..\s*/, "").trim())
|
|
97
|
+
.filter(Boolean)
|
|
98
|
+
.map((line) => line.replace(/^.* -> /, "").replaceAll("\\", "/"));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function vcsRevision(root, config) {
|
|
102
|
+
const result = config.vcs === "git"
|
|
103
|
+
? vcsCommand(root, config, ["rev-parse", "HEAD"])
|
|
104
|
+
: vcsCommand(root, config, ["info", "--show-item", "revision"]);
|
|
105
|
+
return result ? String(result.stdout || "").trim() : "";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function vcsDirty(root, config) {
|
|
109
|
+
const result = config.vcs === "git"
|
|
110
|
+
? vcsCommand(root, config, ["status", "--porcelain"])
|
|
111
|
+
: vcsCommand(root, config, ["status"]);
|
|
112
|
+
return result ? Boolean(String(result.stdout || "").trim()) : null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function collectProvenance(root, config) {
|
|
116
|
+
return {
|
|
117
|
+
node: process.version,
|
|
118
|
+
os: `${process.platform} ${process.arch}`,
|
|
119
|
+
package_manager: config.package_manager || "",
|
|
120
|
+
config_sha256: sha256Text(JSON.stringify(config)),
|
|
121
|
+
vcs: config.vcs || "",
|
|
122
|
+
vcs_revision: vcsRevision(root, config),
|
|
123
|
+
vcs_dirty: vcsDirty(root, config),
|
|
124
|
+
changed_files: changedFiles(root, config),
|
|
125
|
+
at: nowIso(),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function errorSignature(result) {
|
|
130
|
+
const text = `${result.stderr_tail || ""}\n${result.stdout_tail || ""}`;
|
|
131
|
+
const lines = text
|
|
132
|
+
.split(/\r?\n/)
|
|
133
|
+
.map((line) => line.trim())
|
|
134
|
+
.filter((line) => /error|Error|ERROR|失败/.test(line))
|
|
135
|
+
.slice(0, 4);
|
|
136
|
+
return lines.join("\n")
|
|
137
|
+
.replace(/Worker \d+/g, "Worker N")
|
|
138
|
+
.replace(/0x[0-9a-f]+/gi, "0xADDR")
|
|
139
|
+
.trim();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function errorFiles(result) {
|
|
143
|
+
const text = `${result.stderr_tail || ""}\n${result.stdout_tail || ""}`;
|
|
144
|
+
const files = new Set();
|
|
145
|
+
const matches = text.matchAll(/(?:error\s+in\s+|at\s+)([^\s:()]+\.(?:ts|tsx|js|jsx|vue|scss|css))/gi);
|
|
146
|
+
for (const match of matches) {
|
|
147
|
+
files.add(match[1].replace(/^\.\//, "").replaceAll("\\", "/"));
|
|
148
|
+
}
|
|
149
|
+
return [...files];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function classifyCheck(result, baseline, provenance = {}) {
|
|
153
|
+
if (result.exit_code === 0) return "pass";
|
|
154
|
+
const baselineCommand = baseline?.commands?.find((item) => item.name === result.name);
|
|
155
|
+
if (baselineCommand && baselineCommand.exit_code !== 0) {
|
|
156
|
+
if (baselineCommand.signature && baselineCommand.signature === errorSignature(result)) {
|
|
157
|
+
return "pre-existing";
|
|
158
|
+
}
|
|
159
|
+
if (baselineCommand.command === result.command && baselineCommand.exit_code === result.exit_code && baselineCommand.signature) {
|
|
160
|
+
return "pre-existing";
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const files = errorFiles(result);
|
|
164
|
+
const changed = provenance.changed_files || [];
|
|
165
|
+
if (files.length && changed.length) {
|
|
166
|
+
const touchesChanged = files.some((file) => changed.some((item) => item.endsWith(file) || file.endsWith(item)));
|
|
167
|
+
if (!touchesChanged) return "unrelated";
|
|
168
|
+
return "new";
|
|
169
|
+
}
|
|
170
|
+
if (baselineCommand && baselineCommand.exit_code === 0) return "new";
|
|
171
|
+
return "unknown";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function writeBaseline(root, config, task, names) {
|
|
175
|
+
const results = runChecks(root, config, task.dir, { names, stopOnFailure: false });
|
|
176
|
+
const provenance = collectProvenance(root, config);
|
|
177
|
+
const baseline = {
|
|
178
|
+
schema: 1,
|
|
179
|
+
at: provenance.at,
|
|
180
|
+
task: task.meta.id,
|
|
181
|
+
node: provenance.node,
|
|
182
|
+
os: provenance.os,
|
|
183
|
+
package_manager: provenance.package_manager,
|
|
184
|
+
config_sha256: provenance.config_sha256,
|
|
185
|
+
vcs: provenance.vcs,
|
|
186
|
+
vcs_revision: provenance.vcs_revision,
|
|
187
|
+
vcs_dirty: provenance.vcs_dirty,
|
|
188
|
+
commands: results.map((result) => ({
|
|
189
|
+
name: result.name,
|
|
190
|
+
command: result.command,
|
|
191
|
+
exit_code: result.exit_code,
|
|
192
|
+
started_at: result.started_at,
|
|
193
|
+
finished_at: result.finished_at,
|
|
194
|
+
stdout_tail: result.stdout_tail,
|
|
195
|
+
stderr_tail: result.stderr_tail,
|
|
196
|
+
log: result.log,
|
|
197
|
+
signature: errorSignature(result),
|
|
198
|
+
})),
|
|
199
|
+
};
|
|
200
|
+
const evidenceDir = path.join(task.dir, "evidence");
|
|
201
|
+
ensureDir(evidenceDir);
|
|
202
|
+
const file = path.join(evidenceDir, "baseline.json");
|
|
203
|
+
writeJson(file, baseline);
|
|
204
|
+
task.meta.baseline = {
|
|
205
|
+
at: baseline.at,
|
|
206
|
+
config_sha256: baseline.config_sha256,
|
|
207
|
+
commands: baseline.commands.map(({ name, command, exit_code, signature }) => ({
|
|
208
|
+
name,
|
|
209
|
+
command,
|
|
210
|
+
exit_code,
|
|
211
|
+
signature,
|
|
212
|
+
})),
|
|
213
|
+
evidence: "evidence/baseline.json",
|
|
214
|
+
};
|
|
215
|
+
writeTask(task, task.meta, task.body);
|
|
216
|
+
return { baseline, results, file };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function logRelative(task, check) {
|
|
220
|
+
const raw = String(check.log || "");
|
|
221
|
+
if (!raw) return "";
|
|
222
|
+
const absolute = path.isAbsolute(raw) ? raw : path.resolve(task.dir, raw);
|
|
223
|
+
const rel = path.relative(task.dir, absolute).replaceAll(path.sep, "/");
|
|
224
|
+
return rel.startsWith("..") || path.isAbsolute(rel) ? raw : rel;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function section(text, names) {
|
|
228
|
+
const lines = String(text || "").replace(/\r\n/g, "\n").split("\n");
|
|
229
|
+
const start = lines.findIndex((line) => {
|
|
230
|
+
const heading = line.match(/^(#{2,6})\s+(.+)$/);
|
|
231
|
+
if (!heading) return false;
|
|
232
|
+
const name = heading[2].replace(/[((].*?[))]/g, "").trim();
|
|
233
|
+
return names.some((item) => name.toLowerCase().startsWith(item.toLowerCase()));
|
|
234
|
+
});
|
|
235
|
+
if (start < 0) return "";
|
|
236
|
+
const level = lines[start].match(/^#+/)[0].length;
|
|
237
|
+
const body = [];
|
|
238
|
+
for (let index = start + 1; index < lines.length; index += 1) {
|
|
239
|
+
const heading = lines[index].match(/^(#{1,6})\s+/);
|
|
240
|
+
if (heading && heading[1].length <= level) break;
|
|
241
|
+
body.push(lines[index]);
|
|
242
|
+
}
|
|
243
|
+
return body.join("\n").trim();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function bullets(text, limit = 6) {
|
|
247
|
+
const items = String(text || "").split(/\r?\n/).filter((line) => /^\s*[-*]\s+/.test(line));
|
|
248
|
+
if (items.length) return items.slice(0, limit).join("\n");
|
|
249
|
+
const paragraphs = String(text || "").split(/\r?\n\s*\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
250
|
+
return paragraphs.slice(0, limit).join("\n\n");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function writeChanges(task) {
|
|
254
|
+
const meta = task.meta;
|
|
255
|
+
const prdFile = path.join(task.dir, meta.artifacts?.prd || "PRD.md");
|
|
256
|
+
const prd = readText(prdFile);
|
|
257
|
+
const checks = Array.isArray(meta.checks) ? meta.checks : [];
|
|
258
|
+
const title = shortTitle(meta, prd);
|
|
259
|
+
const checkLines = checks.length
|
|
260
|
+
? checks.map((check) => {
|
|
261
|
+
const classification = check.exit_code === 0 || !check.classification
|
|
262
|
+
? ""
|
|
263
|
+
: `,${classificationBi(check.classification)}`;
|
|
264
|
+
const result = check.exit_code === 0 ? "通过(PASS)" : `失败(FAIL,exit ${check.exit_code}${classification})`;
|
|
265
|
+
const log = logRelative(task, check);
|
|
266
|
+
return `- ${checkBi(check.name)}:${result}(\`${check.command}\`${log ? `,日志 \`${log}\`` : ""})`;
|
|
267
|
+
})
|
|
268
|
+
: ["- 未运行检查。"];
|
|
269
|
+
const verified = meta.gates?.verified && typeof meta.gates.verified === "object" ? meta.gates.verified : {};
|
|
270
|
+
const confirmation = String(verified.confirmation || "").trim();
|
|
271
|
+
const appUrl = String(verified.app_url || "").trim();
|
|
272
|
+
const verificationLines = [
|
|
273
|
+
"- 验收依据:PRD「人工验收」清单。",
|
|
274
|
+
confirmation
|
|
275
|
+
? `- 确认记录:${verified.by || "-"} · ${verified.at || "-"} · “${confirmation}”`
|
|
276
|
+
: "- 确认记录:待用户按 PRD 清单核对;记录验收门禁时写入确认。",
|
|
277
|
+
appUrl ? `- 应用地址:${appUrl}` : "- 应用地址:未记录(可选)。",
|
|
278
|
+
];
|
|
279
|
+
const nonGoals = section(prd, ["非目标", "Out of Scope"]);
|
|
280
|
+
const impact = [
|
|
281
|
+
nonGoals ? `非目标(不做):\n${bullets(nonGoals)}` : "",
|
|
282
|
+
"影响范围:以修改说明和 PRD 的方案/非目标为准。",
|
|
283
|
+
"可逆性:按可逆变更处理;如涉及数据、权限、发布或跨团队接口,请补充影响范围与回滚步骤。",
|
|
284
|
+
].filter(Boolean).join("\n\n");
|
|
285
|
+
const exceptions = Array.isArray(meta.exceptions) ? meta.exceptions : [];
|
|
286
|
+
const exceptionLines = exceptions.length
|
|
287
|
+
? exceptions.map((item) => {
|
|
288
|
+
const signature = item.signature ? `;签名:${String(item.signature).slice(0, 120)}` : "";
|
|
289
|
+
return `- ${item.name}:${classificationBi(item.classification || "pre-existing")};${item.reason || "用户确认例外"}${signature}`;
|
|
290
|
+
}).join("\n")
|
|
291
|
+
: "无。";
|
|
292
|
+
const changed = meta.checks?.[0]?.provenance?.changed_files || [];
|
|
293
|
+
const changedLines = changed.length
|
|
294
|
+
? changed.map((file) => `- ${file}`).join("\n")
|
|
295
|
+
: "- 未记录(无 VCS 或工作区状态不可用);提交前用 `git status` / `svn status` 核对。";
|
|
296
|
+
const text = `# 变更说明 · ${title}
|
|
297
|
+
|
|
298
|
+
> 由 solmet 生成,可直接用作 commit message 或 PR 描述草稿;修改后请重新运行 \`solmet check\` 刷新。
|
|
299
|
+
|
|
300
|
+
## 修改说明
|
|
301
|
+
|
|
302
|
+
${meta.request || meta.title}
|
|
303
|
+
|
|
304
|
+
## 影响分析
|
|
305
|
+
|
|
306
|
+
${impact}
|
|
307
|
+
|
|
308
|
+
## 检查结果
|
|
309
|
+
|
|
310
|
+
${checkLines.join("\n")}
|
|
311
|
+
|
|
312
|
+
## 人工验收
|
|
313
|
+
|
|
314
|
+
${verificationLines.join("\n")}
|
|
315
|
+
|
|
316
|
+
## 变更文件
|
|
317
|
+
|
|
318
|
+
${changedLines}
|
|
319
|
+
|
|
320
|
+
## 例外
|
|
321
|
+
|
|
322
|
+
${exceptionLines}
|
|
323
|
+
`;
|
|
324
|
+
const file = path.join(task.dir, meta.artifacts?.changes || "CHANGES.md");
|
|
325
|
+
writeText(file, text);
|
|
326
|
+
return file;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function hasContent(text) {
|
|
330
|
+
return String(text || "")
|
|
331
|
+
.replace(/<!--[\s\S]*?-->/g, "")
|
|
332
|
+
.replace(/待补充/g, "")
|
|
333
|
+
.trim().length > 0;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function validateDocs(task) {
|
|
337
|
+
const errors = [];
|
|
338
|
+
const prd = path.join(task.dir, task.meta.artifacts?.prd || "PRD.md");
|
|
339
|
+
if (!fs.existsSync(prd)) return ["缺少 PRD.md"];
|
|
340
|
+
const prdText = fs.readFileSync(prd, "utf8");
|
|
341
|
+
if (prdText.includes("solmet:draft") || prdText.includes("<任务标题>")) {
|
|
342
|
+
errors.push("PRD.md 仍有草稿标记或占位符");
|
|
343
|
+
}
|
|
344
|
+
const required = [
|
|
345
|
+
["问题/Problem", ["问题", "Problem"]],
|
|
346
|
+
["方案/Solution", ["方案", "Solution", "Approach"]],
|
|
347
|
+
["非目标/Out of Scope", ["非目标", "Out of Scope"]],
|
|
348
|
+
];
|
|
349
|
+
for (const [label, names] of required) {
|
|
350
|
+
if (!hasContent(section(prdText, names))) errors.push(`PRD.md 缺少非空的“${label}”小节`);
|
|
351
|
+
}
|
|
352
|
+
const acceptance = section(prdText, ["人工验收", "Manual Acceptance", "验收清单"]);
|
|
353
|
+
const items = acceptance
|
|
354
|
+
.split(/\r?\n/)
|
|
355
|
+
.filter((line) => /^\s*(?:[-*]|\d+\.)\s+\S/.test(line))
|
|
356
|
+
.filter((line) => hasContent(line.replace(/^\s*(?:[-*]|\d+\.)\s+/, "")));
|
|
357
|
+
if (!acceptance) errors.push("PRD 缺少“人工验收”小节");
|
|
358
|
+
else if (!items.length) errors.push("PRD 的“人工验收”至少需要 1 条可观察项");
|
|
359
|
+
return errors;
|
|
360
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { SOLMET_DIR } from "./paths.js";
|
|
3
|
+
import { readJson, writeJson, nowIso } from "./fsx.js";
|
|
4
|
+
|
|
5
|
+
export function stateDir(root) {
|
|
6
|
+
return path.join(root, SOLMET_DIR);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function configFile(root) {
|
|
10
|
+
return path.join(stateDir(root), "config.json");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function findProjectRoot(start = process.cwd()) {
|
|
14
|
+
let current = path.resolve(start);
|
|
15
|
+
while (true) {
|
|
16
|
+
if (readJson(configFile(current), null)) return current;
|
|
17
|
+
const parent = path.dirname(current);
|
|
18
|
+
if (parent === current) return null;
|
|
19
|
+
current = parent;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function requireProjectRoot(start = process.cwd()) {
|
|
24
|
+
const root = findProjectRoot(start);
|
|
25
|
+
if (!root) {
|
|
26
|
+
throw new Error("未找到 solmet 项目;请先运行 solmet init");
|
|
27
|
+
}
|
|
28
|
+
return root;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function readConfig(root) {
|
|
32
|
+
const value = readJson(configFile(root), null);
|
|
33
|
+
if (!value) throw new Error("缺少 .solmet/config.json;请先运行 solmet init");
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function writeConfig(root, config) {
|
|
38
|
+
writeJson(configFile(root), { ...config, updated_at: nowIso() });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function defaultConfig(root, detected) {
|
|
42
|
+
const now = nowIso();
|
|
43
|
+
return {
|
|
44
|
+
schema: 1,
|
|
45
|
+
product: "solmet",
|
|
46
|
+
project_root: root,
|
|
47
|
+
agent: detected.agent,
|
|
48
|
+
vcs: detected.vcs,
|
|
49
|
+
vcs_root: detected.vcsRoot || "",
|
|
50
|
+
package_manager: detected.packageManager,
|
|
51
|
+
commands: detected.commands,
|
|
52
|
+
created_at: now,
|
|
53
|
+
updated_at: now,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { readJson } from "./fsx.js";
|
|
4
|
+
|
|
5
|
+
export function detectPackageManager(root) {
|
|
6
|
+
if (fs.existsSync(path.join(root, "pnpm-lock.yaml"))) return "pnpm";
|
|
7
|
+
if (fs.existsSync(path.join(root, "yarn.lock"))) return "yarn";
|
|
8
|
+
if (fs.existsSync(path.join(root, "bun.lockb"))) return "bun";
|
|
9
|
+
if (fs.existsSync(path.join(root, "package-lock.json"))) return "npm";
|
|
10
|
+
return fs.existsSync(path.join(root, "package.json")) ? "npm" : "";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function detectVcsInfo(root) {
|
|
14
|
+
let current = path.resolve(root);
|
|
15
|
+
while (true) {
|
|
16
|
+
if (fs.existsSync(path.join(current, ".git"))) return { vcs: "git", root: current };
|
|
17
|
+
if (fs.existsSync(path.join(current, ".svn"))) return { vcs: "svn", root: current };
|
|
18
|
+
const parent = path.dirname(current);
|
|
19
|
+
if (parent === current) return { vcs: "none", root: "" };
|
|
20
|
+
current = parent;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function detectVcs(root) {
|
|
25
|
+
return detectVcsInfo(root).vcs;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function detectCommands(root, packageManager) {
|
|
29
|
+
const pkg = readJson(path.join(root, "package.json"), {});
|
|
30
|
+
const scripts = pkg.scripts || {};
|
|
31
|
+
const run = (name) => (packageManager ? `${packageManager} run ${name}` : "");
|
|
32
|
+
return {
|
|
33
|
+
build: scripts.build ? run("build") : "",
|
|
34
|
+
typecheck: scripts.typecheck ? run("typecheck") : "",
|
|
35
|
+
lint: scripts.lint ? run("lint") : "",
|
|
36
|
+
test: scripts.test ? run("test") : "",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function detectAgent(explicit = "") {
|
|
41
|
+
if (explicit) return explicit;
|
|
42
|
+
if (process.env.QODER_AGENT) return "qoder";
|
|
43
|
+
if (process.env.CLAUDE_CODE) return "claude";
|
|
44
|
+
return "qoder";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function detectProject(root, options = {}) {
|
|
48
|
+
const packageManager = detectPackageManager(root);
|
|
49
|
+
const vcsInfo = detectVcsInfo(root);
|
|
50
|
+
return {
|
|
51
|
+
root,
|
|
52
|
+
packageManager,
|
|
53
|
+
commands: detectCommands(root, packageManager),
|
|
54
|
+
vcs: vcsInfo.vcs,
|
|
55
|
+
vcsRoot: vcsInfo.root,
|
|
56
|
+
agent: detectAgent(options.agent),
|
|
57
|
+
};
|
|
58
|
+
}
|
package/src/lib/fsx.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export function ensureDir(dir) {
|
|
5
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function readText(file, fallback = "") {
|
|
9
|
+
try {
|
|
10
|
+
return fs.readFileSync(file, "utf8");
|
|
11
|
+
} catch {
|
|
12
|
+
return fallback;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function writeText(file, text) {
|
|
17
|
+
ensureDir(path.dirname(file));
|
|
18
|
+
fs.writeFileSync(file, text, "utf8");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function writeIfMissing(file, text) {
|
|
22
|
+
if (!fs.existsSync(file)) {
|
|
23
|
+
writeText(file, text);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function readJson(file, fallback = null) {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
32
|
+
} catch {
|
|
33
|
+
return fallback;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function writeJson(file, value) {
|
|
38
|
+
writeText(file, JSON.stringify(value, null, 2) + "\n");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function copyDirIfMissing(source, target) {
|
|
42
|
+
const exists = fs.existsSync(target);
|
|
43
|
+
if (exists && fs.readdirSync(target).length > 0) return false;
|
|
44
|
+
fs.cpSync(source, target, { recursive: true });
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function listDirs(dir) {
|
|
49
|
+
try {
|
|
50
|
+
return fs
|
|
51
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
52
|
+
.filter((entry) => entry.isDirectory())
|
|
53
|
+
.map((entry) => path.join(dir, entry.name));
|
|
54
|
+
} catch {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function listFiles(dir, extension = "") {
|
|
60
|
+
try {
|
|
61
|
+
return fs
|
|
62
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
63
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(extension))
|
|
64
|
+
.map((entry) => path.join(dir, entry.name));
|
|
65
|
+
} catch {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function listFilesDeep(dir, extension = "") {
|
|
71
|
+
const files = [];
|
|
72
|
+
const walk = (current) => {
|
|
73
|
+
let entries;
|
|
74
|
+
try {
|
|
75
|
+
entries = fs.readdirSync(current, { withFileTypes: true });
|
|
76
|
+
} catch {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
for (const entry of entries) {
|
|
80
|
+
const full = path.join(current, entry.name);
|
|
81
|
+
if (entry.isDirectory()) walk(full);
|
|
82
|
+
else if (entry.isFile() && entry.name.endsWith(extension)) files.push(full);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
walk(dir);
|
|
86
|
+
return files;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function moveDir(source, target) {
|
|
90
|
+
ensureDir(path.dirname(target));
|
|
91
|
+
fs.renameSync(source, target);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function nowIso() {
|
|
95
|
+
return new Date().toISOString();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function today() {
|
|
99
|
+
return new Date().toISOString().slice(0, 10);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function stamp() {
|
|
103
|
+
return new Date().toISOString().replace(/[:.]/g, "-");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function relative(root, file) {
|
|
107
|
+
return path.relative(root, file).replaceAll(path.sep, "/");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function resolveInside(root, value) {
|
|
111
|
+
const resolved = path.resolve(root, value);
|
|
112
|
+
const prefix = path.resolve(root) + path.sep;
|
|
113
|
+
if (resolved !== path.resolve(root) && !resolved.startsWith(prefix)) {
|
|
114
|
+
throw new Error(`路径越界:${value}`);
|
|
115
|
+
}
|
|
116
|
+
return resolved;
|
|
117
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const STATUS = {
|
|
2
|
+
draft: ["草稿", "draft"],
|
|
3
|
+
"spec-ready": ["规格已确认", "spec-ready"],
|
|
4
|
+
implementing: ["实现中", "implementing"],
|
|
5
|
+
verifying: ["待验收", "verifying"],
|
|
6
|
+
verified: ["已验收", "verified"],
|
|
7
|
+
archived: ["已归档", "archived"],
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const GATE = {
|
|
11
|
+
spec: ["规格门禁", "spec"],
|
|
12
|
+
verified: ["验收门禁", "verified"],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const GATE_STATUS = {
|
|
16
|
+
approved: ["已通过", "approved"],
|
|
17
|
+
pending: ["待确认", "pending"],
|
|
18
|
+
stale: ["已过期", "stale"],
|
|
19
|
+
rejected: ["已驳回", "rejected"],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const CHECK = {
|
|
23
|
+
build: ["构建", "build"],
|
|
24
|
+
typecheck: ["类型检查", "typecheck"],
|
|
25
|
+
lint: ["静态检查", "lint"],
|
|
26
|
+
test: ["测试", "test"],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const NEXT_SKILL = {
|
|
30
|
+
spec: ["编写规格", "spec"],
|
|
31
|
+
implement: ["实现", "implement"],
|
|
32
|
+
check: ["检查与验收", "check"],
|
|
33
|
+
archive: ["归档", "archive"],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function pair(value, table) {
|
|
37
|
+
const entry = table[value];
|
|
38
|
+
if (!entry) return String(value ?? "");
|
|
39
|
+
return `${entry[0]}(${entry[1]})`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function bi(zh, en) {
|
|
43
|
+
return `${zh}(${en})`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function statusBi(value) {
|
|
47
|
+
return pair(value, STATUS);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function gateBi(value) {
|
|
51
|
+
return pair(value, GATE);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function gateStatusBi(value) {
|
|
55
|
+
return pair(value, GATE_STATUS);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function checkBi(value) {
|
|
59
|
+
return pair(value, CHECK);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function checkResultBi(exitCode) {
|
|
63
|
+
return exitCode === 0 ? "通过(PASS)" : `失败(FAIL,exit ${exitCode})`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function nextSkillBi(value) {
|
|
67
|
+
return pair(value, NEXT_SKILL);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function artifactBi(state) {
|
|
71
|
+
if (state === "yes") return "有(yes)";
|
|
72
|
+
if (state === "no") return "无(no)";
|
|
73
|
+
return String(state ?? "");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function levelBi(level) {
|
|
77
|
+
return {
|
|
78
|
+
PASS: "通过(PASS)",
|
|
79
|
+
WARN: "警告(WARN)",
|
|
80
|
+
FAIL: "失败(FAIL)",
|
|
81
|
+
}[level] || String(level ?? "");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function classificationBi(value) {
|
|
85
|
+
return {
|
|
86
|
+
pass: "通过(pass)",
|
|
87
|
+
"pre-existing": "既有失败(pre-existing)",
|
|
88
|
+
unrelated: "无关失败(unrelated)",
|
|
89
|
+
new: "本次引入(new)",
|
|
90
|
+
unknown: "待确认(unknown)",
|
|
91
|
+
}[value] || String(value ?? "");
|
|
92
|
+
}
|