UNPKG

@scenemesh/entity-engine

Version:

一个“元数据驱动 + 组件适配 + 动态关系 + 视图管线”式的实体引擎。以 **Model + View + FieldType + SuiteAdapter + DataSource** 为五大支点,统一 CRUD / 查询 / 引用管理 / 视图渲染 / 扩展注册,支持在运行期无侵入拼装出 **表单、网格、主从、看板、仪表盘、流程/树形视图** 等多形态界面。

148 lines (147 loc) 6.65 kB
#!/usr/bin/env node "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // src/scripts/cli.ts var import_fs = __toESM(require("fs")); var import_path = __toESM(require("path")); var import_readline = __toESM(require("readline")); var import_util = require("util"); var import_child_process = require("child_process"); var execPromise = (0, import_util.promisify)(import_child_process.exec); var log = { info: (msg) => console.log(`\x1B[34m[info]\x1B[0m ${msg}`), success: (msg) => console.log(`\x1B[32m[success]\x1B[0m ${msg}`), error: (msg) => console.error(`\x1B[31m[error]\x1B[0m ${msg}`), warn: (msg) => console.warn(`\x1B[33m[warn]\x1B[0m ${msg}`) }; async function runCommand(command) { try { const { stdout, stderr } = await execPromise(command); if (stdout) console.log(stdout); if (stderr) console.error(stderr); } catch (error) { log.error(`Failed to execute command: ${command}`); throw error; } } async function main() { log.info("Setting up @scenemesh/entity-engine..."); const userSchemaPath = import_path.default.join(process.cwd(), "prisma", "schema.prisma"); const packageSchemaPath = import_path.default.join(__dirname, "..", "prisma", "schema.prisma"); let generateCommand = "npx prisma generate"; if (import_fs.default.existsSync(userSchemaPath)) { log.info("Existing prisma/schema.prisma found. Merging models..."); const existingSchema = import_fs.default.readFileSync(userSchemaPath, "utf-8"); if (existingSchema.includes("model EntityObject")) { log.info( "Entity Engine models already exist in your schema. Skipping schema modification." ); } else { let modelsToAdd = import_fs.default.readFileSync(packageSchemaPath, "utf-8"); const modelStartIndex = modelsToAdd.indexOf("model EntityObject"); if (modelStartIndex !== -1) { modelsToAdd = "\n\n" + modelsToAdd.substring(modelStartIndex); import_fs.default.appendFileSync(userSchemaPath, modelsToAdd); log.success("Entity Engine models have been added to your prisma/schema.prisma."); } } } else { log.warn("No prisma/schema.prisma found in your project."); log.info("Generating a temporary Prisma Client based on the Entity Engine schema."); generateCommand = `npx prisma generate --schema=${packageSchemaPath}`; } try { log.info(`Running command: ${generateCommand}`); await runCommand(generateCommand); log.success("Prisma Client has been successfully generated."); } catch { log.error("Prisma Client generation failed. Please run it manually."); process.exit(1); } if (import_fs.default.existsSync(userSchemaPath)) { log.info("Next step: Run 'npx prisma migrate dev' to update your database."); await maybeRunInteractive(async () => { const doMigrate = await promptYesNo("\u662F\u5426\u73B0\u5728\u6267\u884C\u6570\u636E\u5E93\u8FC1\u79FB\u4EE5\u521B\u5EFA/\u66F4\u65B0\u8868\u7ED3\u6784? (y/N): "); if (doMigrate) { try { log.info("Running: npx prisma migrate dev"); await runCommand("npx prisma migrate dev"); log.success("\u6570\u636E\u5E93\u8FC1\u79FB\u5DF2\u5B8C\u6210\u3002"); } catch (err) { log.error("\u6267\u884C prisma migrate dev \u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u68C0\u67E5\u3002"); if (err instanceof Error) log.error(err.message); } } else { log.info("\u5DF2\u8DF3\u8FC7\u6570\u636E\u5E93\u8FC1\u79FB\u3002"); } }); } else { log.warn( "Important: The generated client is temporary and only contains Entity Engine models." ); log.warn( "Once you create your own 'prisma/schema.prisma' and run 'npx prisma generate', this client will be overwritten." ); log.warn( "To integrate properly, run 'npx prisma init' and then run this setup script again." ); await maybeRunInteractive(async () => { const doPush = await promptYesNo( "\u5F53\u524D\u6CA1\u6709\u672C\u5730 schema\uFF0C\u662F\u5426\u57FA\u4E8E\u5305\u5185\u4E34\u65F6 schema \u7ACB\u5373\u521B\u5EFA\u6570\u636E\u5E93\u7ED3\u6784 (prisma db push)? (y/N): " ); if (doPush) { try { const cmd = `npx prisma db push --schema=${packageSchemaPath}`; log.info(`Running: ${cmd}`); await runCommand(cmd); log.success("\u5DF2\u6839\u636E\u4E34\u65F6 schema \u521B\u5EFA/\u540C\u6B65\u6570\u636E\u5E93\u7ED3\u6784\u3002"); } catch (err) { log.error("\u6267\u884C prisma db push \u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u68C0\u67E5\u3002"); if (err instanceof Error) log.error(err.message); } } else { log.info("\u5DF2\u8DF3\u8FC7\u4E34\u65F6 schema \u7684\u6570\u636E\u5E93\u7ED3\u6784\u521B\u5EFA\u3002"); } }); } } main(); function promptYesNo(question) { return new Promise((resolve) => { const rl = import_readline.default.createInterface({ input: process.stdin, output: process.stdout }); rl.question(question, (answer) => { rl.close(); const normalized = (answer || "").trim().toLowerCase(); resolve(normalized === "y" || normalized === "yes"); }); }); } async function maybeRunInteractive(fn) { if (!process.stdout.isTTY) { log.info("\u68C0\u6D4B\u5230\u975E\u4EA4\u4E92\u5F0F\u73AF\u5883\uFF0C\u81EA\u52A8\u8DF3\u8FC7\u53EF\u9009\u7684\u6570\u636E\u5E93\u7ED3\u6784\u521B\u5EFA\u6B65\u9AA4\u3002"); return; } await fn(); } //# sourceMappingURL=cli.js.map