chore: 工程能力增强,可交互式使用常用功能

This commit is contained in:
wangxuefeng
2025-02-19 15:22:48 +08:00
parent eab709f94f
commit c3ebe3e15c
7 changed files with 434 additions and 40 deletions

View File

@@ -0,0 +1,69 @@
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(); // 确保最终停止加载动画
}

71
scripts/index.mjs Normal file
View File

@@ -0,0 +1,71 @@
import inquirer from "inquirer";
import { execSync } from "child_process";
import path from "node:path";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
// 修正后的目录获取逻辑
const dirname = path.dirname(fileURLToPath(import.meta.url));
const EXCLUDE_FILES = ["index.mjs", "utils"]; // 排除自身和其他非命令文件
// 获取当前目录下的命令文件
const commands = fs
.readdirSync(dirname) // 直接读取当前目录
.filter((file) => {
const fullPath = path.join(dirname, file);
return (
fs.statSync(fullPath).isFile() &&
!EXCLUDE_FILES.includes(file) &&
path.extname(file) === ".mjs"
);
})
.map((file) => ({
name: `📦 ${path.basename(file, ".mjs")}`,
value: path.basename(file, ".mjs"),
}));
// 提供交互式选择
inquirer
.prompt([
{
type: "list",
name: "command",
message: "选择要执行的命令",
choices: commands,
},
])
.then((answers) => {
console.log("answers", answers);
// 获取用户选择的命令
const selectedCommand = `node ${path.join(dirname, answers.command)}.mjs`;
// 执行 Bash 命令
console.log("🚀 执行命令:", selectedCommand); // 添加命令预览
execSync(selectedCommand, {
stdio: "inherit",
});
})
.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); // 确保进程正常退出
});
// 添加全局未捕获异常处理
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);
});

8
scripts/utils/index.mjs Normal file
View File

@@ -0,0 +1,8 @@
import ora from "ora";
export function getLoadAnimation(text) {
return ora({
text,
color: "cyan",
spinner: "dots",
});
}