Files
FreeCADWasmDev/tools/reducer/create-all.mjs
wangdequan b876ad5742 feat(reducer): 二级减速器有头创建工具包与初学者教学
- tools/reducer:目录/前置/单件/批量/装配/自测,复用 cad-ui-rebuild 引擎
- 教学控制台新增「二级减速器」分组:23 件逐件课 + 装配课 + 环境课(job kind reducer-part / reducer-assembly)
- web/teaching/reducer-toolkit.html 初学者配套页(生成物)+ Playwright 双视口门禁
- 首页补工具入口;工具手册随 npm 脚本变更重生成

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 07:31:24 +08:00

120 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* Purpose: 按组或按列表串行重建多个零件(每个零件仍是一个独立运行)。
*
* 逐件调用 tools/reducer/create-part.mjs,保持与单件完全相同的判定。
* 默认遇错即停;--keep-going 继续并在末尾给出汇总。
*
* Usage:
* node tools/reducer/create-all.mjs --group gears --dry
* node tools/reducer/create-all.mjs --parts P03,P04
* node tools/reducer/create-all.mjs --all --keep-going --slow
*
* Exit: 0 全部通过 / 1 至少一件产品失败 / 2 前置缺失或参数错误
*/
import fs from "node:fs";
import path from "node:path";
import { spawn } from "node:child_process";
import { REPO_ROOT, find, groups, parts } from "./catalog.mjs";
const CREATE_PART = path.join(REPO_ROOT, "tools/reducer/create-part.mjs");
function parseArgs(argv) {
const options = { parts: [], group: null, all: false, keepGoing: false, dry: false, slow: false, out: null };
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--parts") options.parts = String(argv[++index] ?? "").split(",").map((item) => item.trim()).filter(Boolean);
else if (arg === "--group") options.group = argv[++index] ?? null;
else if (arg === "--all") options.all = true;
else if (arg === "--keep-going") options.keepGoing = true;
else if (arg === "--dry") options.dry = true;
else if (arg === "--slow") options.slow = true;
else if (arg === "--out") options.out = path.resolve(argv[++index] ?? "");
else if (arg === "--help" || arg === "-h") {
console.log("node tools/reducer/create-all.mjs [--parts P01,P02 | --group gears | --all] [--keep-going] [--dry] [--slow] [--out <dir>]");
process.exit(0);
} else {
console.error(`未知参数:${arg}`);
process.exit(2);
}
}
if (!options.parts.length && !options.group && !options.all) {
console.error("用法:--parts P01,P02 | --group <id> | --all(分组见 node tools/reducer/list.mjs)");
process.exit(2);
}
return options;
}
function selection(options) {
if (options.parts.length) return options.parts.map((key) => find(key));
if (options.group) {
const group = groups().find((item) => item.id === options.group);
if (!group) {
console.error(`没有该分组:${options.group}(可用:${groups().map((item) => item.id).join(", ")})`);
process.exit(2);
}
return group.ids.map((key) => find(key));
}
if (options.out) {
// Reports live under the shared out dir so a whole build is one directory.
return parts();
}
return parts();
}
function runPart(part, options) {
const argv = [process.execPath, CREATE_PART, part.id];
if (options.dry) argv.push("--dry");
if (options.slow) argv.push("--slow");
if (options.out) argv.push("--out", path.join(options.out, part.id));
return new Promise((resolve) => {
const child = spawn(argv[0], argv.slice(1), { cwd: REPO_ROOT, stdio: "inherit" });
child.on("error", () => resolve(2));
child.on("close", (code) => resolve(code ?? 1));
});
}
async function main() {
const options = parseArgs(process.argv.slice(2));
const selected = selection(options);
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
const outDir = options.out;
const summary = { schema: "freecad-web/reducer-toolkit/batch/v1", startedAt: new Date().toISOString(), items: [] };
for (const part of selected) {
console.log(`\n=== ${part.id} ${part.document}(${part.title},${part.steps} 步)===`);
const code = await runPart(part, options);
summary.items.push({ id: part.id, document: part.document, title: part.title, exitCode: code });
if (code === 2) {
console.error(`前置缺失,停止:${part.id}`);
break;
}
if (code !== 0 && !options.keepGoing) {
console.error(`零件 ${part.id} 未通过(exit ${code}),停止。用 --keep-going 可继续。`);
break;
}
}
summary.finishedAt = new Date().toISOString();
summary.passed = summary.items.filter((item) => item.exitCode === 0).length;
summary.failed = summary.items.filter((item) => item.exitCode === 1).length;
summary.blocked = summary.items.filter((item) => item.exitCode === 2).length;
summary.skipped = selected.length - summary.items.length;
const dir = outDir ?? path.join(REPO_ROOT, "docs/current/reducer-toolkit/runs", `${stamp}-batch`);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, "summary.json"), JSON.stringify(summary, null, 2));
console.log(`\n合计:通过 ${summary.passed} / 失败 ${summary.failed} / 前置 ${summary.blocked} / 未执行 ${summary.skipped}`);
console.log(`汇总:${path.relative(REPO_ROOT, path.join(dir, "summary.json"))}`);
if (summary.failed > 0) process.exit(1);
if (summary.blocked > 0 || summary.skipped > 0) process.exit(2);
process.exit(0);
}
main().catch((error) => {
console.error(`失败:${error?.message ?? error}`);
process.exit(error?.code === "E_PART" ? 2 : 1);
});