low-code/scripts/dev-select-app.mjs

70 lines
2.1 KiB
JavaScript

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(); // 确保最终停止加载动画
}