chore: 增加 low-code-help 功能命令,让开发人员更傻瓜地利用工程

This commit is contained in:
wangxuefeng 2025-02-19 16:29:42 +08:00
parent c3ebe3e15c
commit 52a32bb31e
8 changed files with 230 additions and 74 deletions

View File

@ -39,6 +39,9 @@ export default ({ command, mode }: ConfigEnv): UserConfig => {
},
],
},
server: {
open: true,
},
plugins: [
vue(),
Inspector(),

View File

@ -34,7 +34,7 @@ export default defineConfig({
],
server: {
cors: true,
open: true,
// open: true,
},
compilation: {
resolve: {

View File

@ -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",

View File

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

View File

@ -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) {

View File

@ -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);
});

View File

@ -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();
}

View File

@ -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);
});