init.js 3.1 KB

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