From 52a32bb31e07de2ca7e69fa8da74386cb309235e Mon Sep 17 00:00:00 2001 From: wangxuefeng Date: Wed, 19 Feb 2025 16:29:42 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=20low-code-help=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=91=BD=E4=BB=A4=EF=BC=8C=E8=AE=A9=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E4=BA=BA=E5=91=98=E6=9B=B4=E5=82=BB=E7=93=9C=E5=9C=B0?= =?UTF-8?q?=E5=88=A9=E7=94=A8=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/platform/vite.config.ts | 3 ++ apps/y-code-v1/farm.config.ts | 2 +- package.json | 1 - scripts/dev-select-app.mjs | 69 ------------------------ scripts/index.mjs | 18 +++++-- scripts/low-code-start.mjs | 85 +++++++++++++++++++++++++++++ scripts/report-script-list.mjs | 28 ++++++++++ scripts/select-dev-app.mjs | 98 ++++++++++++++++++++++++++++++++++ 8 files changed, 230 insertions(+), 74 deletions(-) delete mode 100644 scripts/dev-select-app.mjs create mode 100644 scripts/low-code-start.mjs create mode 100644 scripts/report-script-list.mjs create mode 100644 scripts/select-dev-app.mjs diff --git a/apps/platform/vite.config.ts b/apps/platform/vite.config.ts index 121926a..b845904 100644 --- a/apps/platform/vite.config.ts +++ b/apps/platform/vite.config.ts @@ -39,6 +39,9 @@ export default ({ command, mode }: ConfigEnv): UserConfig => { }, ], }, + server: { + open: true, + }, plugins: [ vue(), Inspector(), diff --git a/apps/y-code-v1/farm.config.ts b/apps/y-code-v1/farm.config.ts index 2b579de..e1c2757 100644 --- a/apps/y-code-v1/farm.config.ts +++ b/apps/y-code-v1/farm.config.ts @@ -34,7 +34,7 @@ export default defineConfig({ ], server: { cors: true, - open: true, + // open: true, }, compilation: { resolve: { diff --git a/package.json b/package.json index 7975629..3855251 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ }, "scripts": { "low-code:help": "node ./scripts/index.mjs", - "dev:select-app": "node ./scripts/dev-select-app.mjs", "preinstall": "npx only-allow pnpm && pnpm i -g turbo rimraf", "dev": "turbo run dev", "build": "turbo run build", diff --git a/scripts/dev-select-app.mjs b/scripts/dev-select-app.mjs deleted file mode 100644 index 4fea83d..0000000 --- a/scripts/dev-select-app.mjs +++ /dev/null @@ -1,69 +0,0 @@ -import inquirer from "inquirer"; -import { exec, execSync } from "child_process"; -import path from "node:path"; -import fs from "node:fs"; -import { fileURLToPath } from "node:url"; -import { getLoadAnimation } from "./utils/index.mjs"; -import { ExitPromptError } from "inquirer"; - -const loadAnimation = getLoadAnimation("正在获取应用列表..."); - -// 替换原有的 __dirname 定义 -const dirname = path.dirname(fileURLToPath(import.meta.url)); - -// 获取 apps 目录 -const appsDir = path.join(dirname, "../apps"); - -const EXCLUDE_DIRS = ["platform"]; -// 通过 apps 目录生成 commands 对象 -try { - loadAnimation.start(); // 启动加载动画 - const commands = fs - .readdirSync(appsDir) - .filter((app) => { - const isDirectory = fs.statSync(path.join(appsDir, app)).isDirectory(); - return isDirectory && !EXCLUDE_DIRS.includes(app); - }) - .map((app) => ({ - name: `📦 ${app}`, // 显示带图标的友好名称 - value: app, // 实际值保持原目录名 - })); - - // console.log(commands); - - // 提供交互式选择 - inquirer - .prompt([ - { - type: "list", - name: "command", - message: "选择要独立运行的子应用", - choices: commands, - }, - ]) - .then((answers) => { - loadAnimation.start("正在启动子应用..."); - // 获取用户选择的命令 - const selectedCommand = `turbo run dev --filter=@sy/low-code-${answers.command}`; - - // 执行 Bash 命令 - console.log("🚀 执行命令:", selectedCommand); // 添加命令预览 - execSync(selectedCommand, { - stdio: "inherit", - cwd: path.join(appsDir, ".."), - }); - loadAnimation.succeed("子应用启动成功"); - }) - .catch((error) => { - if (error instanceof ExitPromptError) { - loadAnimation.stop(); // 停止加载动画 - console.log("\n👋 已退出选择操作"); - process.exit(0); // 正常退出 - } else { - loadAnimation.fail("选择失败"); - console.error("错误详情:", error.message); // 仅显示错误消息 - } - }); -} finally { - loadAnimation.stop(); // 确保最终停止加载动画 -} diff --git a/scripts/index.mjs b/scripts/index.mjs index 71fbd1a..075f217 100644 --- a/scripts/index.mjs +++ b/scripts/index.mjs @@ -41,9 +41,21 @@ inquirer // 执行 Bash 命令 console.log("🚀 执行命令:", selectedCommand); // 添加命令预览 - execSync(selectedCommand, { - stdio: "inherit", - }); + try { + execSync(selectedCommand, { + stdio: "inherit", + // 添加终止信号处理 + killSignal: "SIGTERM", + timeout: 0, + }); + } catch (error) { + console.log("error", error); + if (error.signal === "SIGTERM") { + console.log("\n🛑 命令执行被终止"); + } else { + console.log("\n🛑 执行错误,错误堆栈:", error.stack, error.message); + } + } }) .catch((error) => { if (error.isTtyError) { diff --git a/scripts/low-code-start.mjs b/scripts/low-code-start.mjs new file mode 100644 index 0000000..8265254 --- /dev/null +++ b/scripts/low-code-start.mjs @@ -0,0 +1,85 @@ +import { execSync } from "child_process"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { getLoadAnimation } from "./utils/index.mjs"; + +const loadAnimation = getLoadAnimation("正在启动应用..."); + +// 基础路径配置 +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const rootDir = path.join(dirname, "../"); + +try { + loadAnimation.start(); + + // 检查 turbo 是否可用 + try { + execSync("turbo --version", { stdio: "ignore" }); + } catch { + loadAnimation.stop(); + console.error("❌ 未找到 turbo,请先执行以下命令安装:"); + console.log("npm install -g turbo"); + process.exit(1); + } + + const { spawn } = await import("child_process"); + const child = spawn("turbo", ["run", "dev"], { + stdio: "inherit", + cwd: rootDir, + shell: true, // 启用 shell 解析 + env: { + ...process.env, + PATH: `${process.env.PATH}:${path.join(rootDir, "node_modules/.bin")}`, + }, + }); + + // 增加错误事件监听 + child.on("error", (err) => { + loadAnimation.fail(`启动失败: ${err.message}`); + process.exit(1); + }); + + console.log("🚀 执行命令: turbo run dev"); + + // 监听子进程退出 + child.on("exit", (code) => { + if (code === 0) { + loadAnimation.succeed("应用启动成功"); + } else { + loadAnimation.fail(`应用异常退出,代码 ${code}`); + } + process.exit(code || 0); // 确保父进程退出 + }); + + // 监听终止信号 + process.on("SIGINT", () => { + loadAnimation.stop(); + console.log("\n🛑 收到终止信号,正在清理进程..."); + child.kill("SIGINT"); // 先终止子进程 + process.exit(0); + }); +} catch (error) { + loadAnimation.stop(); + if (error.message.includes("User force closed the prompt")) { + console.log("\n🚪 已主动退出程序"); + } else { + console.error("执行错误:", error.message); + } + process.exit(0); +} finally { + loadAnimation.stop(); +} + +// 保留全局异常处理 +process.on("uncaughtException", (error) => { + loadAnimation.stop(); + console.error("未捕获异常:", error.message); + process.exit(1); +}); + +// 保留终止信号处理 +process.on("SIGINT", () => { + loadAnimation.stop(); + console.log("\n🛑 收到终止信号"); + process.exit(0); +}); diff --git a/scripts/report-script-list.mjs b/scripts/report-script-list.mjs new file mode 100644 index 0000000..f1f6575 --- /dev/null +++ b/scripts/report-script-list.mjs @@ -0,0 +1,28 @@ +import path from "node:path"; +import fs from "node:fs"; +import { fileURLToPath } from "node:url"; +import { getLoadAnimation } from "./utils/index.mjs"; + +const loadAnimation = getLoadAnimation("正在扫描命令文件..."); +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const scriptsDir = path.join(dirname, "../scripts"); + +try { + loadAnimation.start(); + const commandFiles = fs + .readdirSync(scriptsDir) + .filter((file) => { + const isMjsFile = path.extname(file) === ".mjs"; + const isNotIndex = file !== "index.mjs"; + return isMjsFile && isNotIndex; + }) + .map((file) => path.basename(file, ".mjs")); + + loadAnimation.succeed("找到以下可用脚本:"); + console.log("\n" + commandFiles.map((name) => ` 📦 ${name}`).join("\n")); +} catch (error) { + loadAnimation.fail("扫描脚本失败"); + console.error(error); +} finally { + loadAnimation.stop(); +} diff --git a/scripts/select-dev-app.mjs b/scripts/select-dev-app.mjs new file mode 100644 index 0000000..afe62a1 --- /dev/null +++ b/scripts/select-dev-app.mjs @@ -0,0 +1,98 @@ +import inquirer from "inquirer"; +import { exec, execSync } from "child_process"; +import path from "node:path"; +import fs from "node:fs"; +import { fileURLToPath } from "node:url"; + +// 替换原有的 __dirname 定义 +const dirname = path.dirname(fileURLToPath(import.meta.url)); + +// 获取 apps 目录 +const appsDir = path.join(dirname, "../apps"); + +const EXCLUDE_DIRS = ["platform"]; +// 通过 apps 目录生成 commands 对象 +try { + const commands = fs + .readdirSync(appsDir) + .filter((app) => { + const isDirectory = fs.statSync(path.join(appsDir, app)).isDirectory(); + return isDirectory && !EXCLUDE_DIRS.includes(app); + }) + .map((app) => ({ + name: `📦 ${app}`, // 显示带图标的友好名称 + value: app, // 实际值保持原目录名 + })); + + // console.log(commands); + + // 提供交互式选择 + inquirer + .prompt([ + { + type: "list", + name: "command", + message: "选择要独立运行的子应用", + choices: commands, + }, + ]) + .then(async (answers) => { + const command = `turbo run dev --filter=@sy/low-code-${answers.command}`; + + // 使用 spawn 替代 execSync + const { spawn } = await import("child_process"); + const child = spawn(command.split(" ")[0], command.split(" ").slice(1), { + stdio: "inherit", + cwd: path.join(appsDir, ".."), + shell: true, + env: { + ...process.env, + PATH: `${process.env.PATH}:${path.join(appsDir, "../node_modules/.bin")}`, + }, + }); + + // 添加子进程事件监听 + child.on("error", (err) => { + process.exit(1); + }); + + child.on("exit", (code) => { + if (code === 0) { + } else { + } + process.exit(code || 0); + }); + + // 修改 SIGINT 处理逻辑 + process.on("SIGINT", () => { + console.log("\n🛑 正在终止子应用进程..."); + child.kill("SIGINT"); + process.exit(0); + }); + }) + .catch((error) => { + if (error.isTtyError) { + console.log("\n👋 终端环境异常"); + } else if (error.message.includes("User force closed the prompt")) { + console.log("\n🚪 已主动退出程序"); + } else { + console.error("执行错误:", error.message); + } + process.exit(0); + }); +} finally { +} + +// 添加全局异常处理 +process.on("uncaughtException", (error) => { + if (error.message.includes("User force closed the prompt")) { + console.log("\n🔌 连接已断开"); + process.exit(0); + } +}); + +// 添加 SIGINT 监听 +process.on("SIGINT", () => { + console.log("\n🛑 收到终止信号"); + process.exit(0); +});