/** proto_init:新项目原型库一键引导——目录骨架 + _meta.js + 门户 index.html + git 初始化 * 门户模板取自本仓库 ../template/index.html(数据驱动,零改动可用) */ import { readFile, writeFile, mkdir, access } from "node:fs/promises"; import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { execFileSync } from "node:child_process"; const TEMPLATE_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "template"); const DEFAULT_DOMAINS = ["系统工作台", "业务管理", "数据中心", "系统设置"]; /* 通用起步域,项目可自定义 */ async function exists(p) { try { await access(p); return true; } catch { return false; } } export async function initProject({ repo_path, project_name, repo_url, domains, live_url }) { if (!repo_path || !project_name) throw new Error("repo_path 与 project_name 必填"); if (await exists(join(repo_path, "_meta.js"))) throw new Error(`目标已是原型库(存在 _meta.js): ${repo_path}`); const tplPortal = join(TEMPLATE_DIR, "index.html"); if (!(await exists(tplPortal))) throw new Error(`门户模板缺失: ${tplPortal}(检查 wedo-repo/template/)`); const domainList = (domains && domains.length) ? domains : DEFAULT_DOMAINS; await mkdir(repo_path, { recursive: true }); for (const d of domainList) await mkdir(join(repo_path, d), { recursive: true }); await mkdir(join(repo_path, "_机制示例"), { recursive: true }); const meta = `/* 原型库元数据(门户与 MCP 共读;file:// 环境用 JS 变量避免 fetch 的 CORS 限制) * 维护: 新增/更新原型后同步本文件(卡片/状态灯随 _meta.js 自动更新) */ window.PROTO_META = { system: "${project_name}", repo: { url: "${repo_url || ""}", desc: "原型二开补充库,仅供开发测试人员业务逻辑补充使用" }, live: { name: "线上系统", url: "${live_url || ""}", note: "线上/测试环境入口" }, hub: { url: "${process.env.WEDO_HUB_URL || ""}", token: "${process.env.WEDO_HUB_TOKEN || ""}", repo: "${process.env.WEDO_REPO || project_name}" }, domains: ${JSON.stringify(domainList)}, pages: [] }; `; await writeFile(join(repo_path, "_meta.js"), meta, "utf8"); await writeFile(join(repo_path, "index.html"), await readFile(tplPortal, "utf8"), "utf8"); const gitInit = []; const run = (args, cwd) => { try { execFileSync("git", args, { cwd, timeout: 30_000, stdio: ["ignore", "pipe", "pipe"] }); return true; } catch { return false; } }; if (run(["init", "-b", "main"], repo_path)) { gitInit.push("git init(main) ✓"); if (repo_url && run(["remote", "add", "origin", repo_url], repo_path)) gitInit.push(`remote origin → ${repo_url} ✓`); } return [ `✅ 原型库已初始化: ${repo_path}`, `项目: ${project_name} · 业务域: ${domainList.join(" / ")}`, "生成: _meta.js(元数据) + index.html(门户,双击即用) + 各业务域目录 + _机制示例/", ...gitInit.map(l => " " + l), "", "下一步: 1)按 reproto 流程重构首个原型放入业务域目录 2)更新 _meta.js 的 pages 3)proto_publish 首次提交", ].join("\n"); }