import { Command } from "commander"; import { formatCompact } from '@zhin.js/logger'; import { logger } from "../utils/logger.js"; import { output } from "../utils/output.js"; import fs from "fs-extra"; import path from "path"; import { performSmartBuild, shouldUseSmartBuildInCwd, printEmptyPluginDirsError, watchClientBundle, } from "../libs/plugin-package-build.js"; export const buildCommand = new Command("build") .description("构建插件:按目录自动编译 src(tsc)与 client(esbuild);也可在 zhin workspace 下批量构建 plugins/*") .addHelpText( "after", "\n另见: zhin runtime build 按依赖顺序对整个 workspace 中带 build 脚本的包运行 pnpm run build(先打印构建计划);\n" + " 本命令针对单个插件(或 plugins/* 批量),不走 workspace 依赖顺序。", ) .argument("[plugin]", "插件路径(相对 plugins/,如 adapters/icqq);省略时在智能目录下构建当前包,或在 workspace 根构建全部插件") .option("--clean", "清理 lib/ 与 dist/ 后再构建", false) .option("--production", "生产构建(client 压缩,默认开启)", true) .option("--no-production", "开发构建(不压缩 client)") .option( "--watch", "仅监听 client/ 增量写入 dist(需存在 client;不支持批量构建)", false, ) .action( async ( pluginName: string | undefined, options: { clean: boolean; production: boolean; watch: boolean; }, ) => { try { const cwd = process.cwd(); if (options.watch) { if (pluginName) { logger.error("--watch 不能与 [plugin] 参数同时使用"); process.exit(1); } if (!shouldUseSmartBuildInCwd(cwd)) { logger.error( "--watch 仅适用于显式声明 package.json#zhin 的插件包,或依赖 zhin.js 且含 src/client 的应用根目录", ); process.exit(1); } if (!fs.existsSync(path.join(cwd, "client"))) { logger.error("--watch 需要存在 client/ 目录"); process.exit(1); } logger.info(formatCompact( { cmd: "build", op: "watch", hint: "Ctrl+C 退出" })); await watchClientBundle(cwd); return; } if (!pluginName && shouldUseSmartBuildInCwd(cwd)) { let label = path.basename(cwd); try { const pkg = (await fs.readJson( path.join(cwd, "package.json"), )) as { name?: string }; if (pkg.name) label = pkg.name; } catch { /* keep basename */ } logger.info(formatCompact( { cmd: "build", op: "smart", target: label })); await performSmartBuild(cwd, String(label), { clean: options.clean, production: options.production, }); logger.success(`${label} 构建完成`); return; } if (!isWorkspaceProject(cwd)) { logger.error( "当前目录不是 Zhin workspace 根(需存在 pnpm-workspace.yaml,且 package.json 的 dependencies / devDependencies / peerDependencies 之一声明 zhin.js),且也不满足插件/应用智能构建条件。", ); output.line( "若要在 monorepo 根执行 zhin build:请在根 package.json 声明 zhin.js。若你在开发独立插件:请在插件根目录执行,并显式声明 package.json#zhin。", ); process.exit(1); } const pluginsDir = path.join(cwd, "plugins"); if (!fs.existsSync(pluginsDir)) { logger.error("未找到 plugins 目录"); process.exit(1); } if (pluginName) { const pluginPath = path.join(pluginsDir, pluginName); if (!fs.existsSync(pluginPath)) { logger.error(`未找到插件: ${pluginName}`); process.exit(1); } await buildOnePluginPath(pluginPath, pluginName, options); } else { const plugins = await fs.readdir(pluginsDir); const validPlugins = plugins.filter((p) => { const pluginPath = path.join(pluginsDir, p); const stat = fs.statSync(pluginPath); return ( stat.isDirectory() && fs.existsSync(path.join(pluginPath, "package.json")) ); }); if (validPlugins.length === 0) { logger.warn(formatCompact( { cmd: "build", op: "no_plugins" })); return; } logger.info(formatCompact( { cmd: "build", op: "batch", count: validPlugins.length })); for (const plugin of validPlugins) { const pluginPath = path.join(pluginsDir, plugin); await buildOnePluginPath(pluginPath, plugin, options); } } } catch (error) { if ( error instanceof Error && error.message === "empty plugin dirs" ) { process.exit(1); } logger.error(`构建失败: ${error}`); process.exit(1); } }, ); async function buildOnePluginPath( pluginPath: string, pluginName: string, options: { clean: boolean; production: boolean }, ): Promise { logger.info(formatCompact( { cmd: "build", op: "build", plugin: pluginName })); const hasSrc = fs.existsSync(path.join(pluginPath, "src")); const hasClient = fs.existsSync(path.join(pluginPath, "client")); if (!hasSrc && !hasClient) { printEmptyPluginDirsError(pluginName); throw new Error("empty plugin dirs"); } if (options.production) { logger.info(formatCompact( { cmd: "build", op: "production" })); } try { await performSmartBuild(pluginPath, pluginName, { clean: options.clean, production: options.production, }); } catch (e) { logger.error(`✗ ${pluginName} 构建失败: ${e}`); throw e; } logger.success(`${pluginName} 构建完成`); } function isWorkspaceProject(dir: string): boolean { const workspaceYamlPath = path.join(dir, "pnpm-workspace.yaml"); const packageJsonPath = path.join(dir, "package.json"); if (!fs.existsSync(workspaceYamlPath) || !fs.existsSync(packageJsonPath)) { return false; } try { const packageJson = fs.readJsonSync(packageJsonPath) as { dependencies?: Record; devDependencies?: Record; peerDependencies?: Record; }; return Boolean( packageJson.dependencies?.["zhin.js"] || packageJson.devDependencies?.["zhin.js"] || packageJson.peerDependencies?.["zhin.js"], ); } catch { return false; } }