#!/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 ]"); process.exit(0); } else { console.error(`未知参数:${arg}`); process.exit(2); } } if (!options.parts.length && !options.group && !options.all) { console.error("用法:--parts P01,P02 | --group | --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); });