|
| 1 | +// add.ts — 添加现有 FBA 项目到管理列表 |
| 2 | +import * as clack from "@clack/prompts"; |
| 3 | +import chalk from "chalk"; |
| 4 | +import { basename, join, resolve } from "path"; |
| 5 | +import { existsSync, readdirSync, readFileSync, statSync } from "fs"; |
| 6 | +import { t } from "../lib/i18n.js"; |
| 7 | +import { |
| 8 | + readGlobalConfig, |
| 9 | + addProject, |
| 10 | + writeProjectConfig, |
| 11 | +} from "../lib/config.js"; |
| 12 | +import type { ProjectConfig } from "../types/config.js"; |
| 13 | +import type { DatabaseType } from "../lib/infra.js"; |
| 14 | + |
| 15 | +// ─── 项目结构探测 ─── |
| 16 | + |
| 17 | +interface DetectResult { |
| 18 | + backendName: string | null; |
| 19 | + frontendName: string | null; |
| 20 | + hasInfra: boolean; |
| 21 | + infraServices: string[]; |
| 22 | + dbType: DatabaseType | null; |
| 23 | + serverPort: number | null; |
| 24 | + webPort: number | null; |
| 25 | +} |
| 26 | + |
| 27 | +function isBackendDir(dir: string): boolean { |
| 28 | + return ( |
| 29 | + existsSync(join(dir, "pyproject.toml")) && |
| 30 | + existsSync(join(dir, "backend")) && |
| 31 | + statSync(join(dir, "backend")).isDirectory() |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function isFrontendDir(dir: string): boolean { |
| 36 | + const webAppDir = join(dir, "apps", "web-antdv-next"); |
| 37 | + return existsSync(webAppDir) && statSync(webAppDir).isDirectory(); |
| 38 | +} |
| 39 | + |
| 40 | +function parseBackendEnv( |
| 41 | + backendDir: string, |
| 42 | +): { dbType: DatabaseType | null } { |
| 43 | + const envPath = join(backendDir, "backend", ".env"); |
| 44 | + if (!existsSync(envPath)) return { dbType: null }; |
| 45 | + |
| 46 | + try { |
| 47 | + const content = readFileSync(envPath, "utf-8"); |
| 48 | + const dbTypeMatch = content.match( |
| 49 | + /DATABASE_TYPE\s*=\s*['"]?(\w+)['"]?/, |
| 50 | + ); |
| 51 | + const dbType = |
| 52 | + dbTypeMatch?.[1] === "mysql" |
| 53 | + ? "mysql" |
| 54 | + : dbTypeMatch?.[1] === "postgresql" |
| 55 | + ? "postgresql" |
| 56 | + : null; |
| 57 | + return { dbType }; |
| 58 | + } catch { |
| 59 | + return { dbType: null }; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function parseFrontendEnv( |
| 64 | + frontendDir: string, |
| 65 | +): { webPort: number | null } { |
| 66 | + const envPath = join( |
| 67 | + frontendDir, |
| 68 | + "apps", |
| 69 | + "web-antdv-next", |
| 70 | + ".env.development", |
| 71 | + ); |
| 72 | + if (!existsSync(envPath)) return { webPort: null }; |
| 73 | + |
| 74 | + try { |
| 75 | + const content = readFileSync(envPath, "utf-8"); |
| 76 | + const portMatch = content.match(/VITE_PORT\s*=\s*(\d+)/); |
| 77 | + return { webPort: portMatch?.[1] ? parseInt(portMatch[1]) : null }; |
| 78 | + } catch { |
| 79 | + return { webPort: null }; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function parseInfraServices(projectDir: string): string[] { |
| 84 | + const composePath = join(projectDir, "infra", "docker-compose.yml"); |
| 85 | + if (!existsSync(composePath)) return []; |
| 86 | + |
| 87 | + try { |
| 88 | + const content = readFileSync(composePath, "utf-8"); |
| 89 | + const services: string[] = []; |
| 90 | + if (content.includes("fba-postgres")) services.push("postgres"); |
| 91 | + if (content.includes("fba-mysql")) services.push("mysql"); |
| 92 | + if (content.includes("fba-redis")) services.push("redis"); |
| 93 | + if (content.includes("fba-rabbitmq")) services.push("rabbitmq"); |
| 94 | + return services; |
| 95 | + } catch { |
| 96 | + return []; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function detectProject(projectDir: string): DetectResult { |
| 101 | + const result: DetectResult = { |
| 102 | + backendName: null, |
| 103 | + frontendName: null, |
| 104 | + hasInfra: false, |
| 105 | + infraServices: [], |
| 106 | + dbType: null, |
| 107 | + serverPort: null, |
| 108 | + webPort: null, |
| 109 | + }; |
| 110 | + |
| 111 | + // 扫描直接子目录 |
| 112 | + let entries: string[]; |
| 113 | + try { |
| 114 | + entries = readdirSync(projectDir).filter((name) => { |
| 115 | + const full = join(projectDir, name); |
| 116 | + return !name.startsWith(".") && statSync(full).isDirectory(); |
| 117 | + }); |
| 118 | + } catch { |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + for (const entry of entries) { |
| 123 | + const fullPath = join(projectDir, entry); |
| 124 | + |
| 125 | + if (!result.backendName && isBackendDir(fullPath)) { |
| 126 | + result.backendName = entry; |
| 127 | + const { dbType } = parseBackendEnv(fullPath); |
| 128 | + result.dbType = dbType; |
| 129 | + } |
| 130 | + |
| 131 | + if (!result.frontendName && isFrontendDir(fullPath)) { |
| 132 | + result.frontendName = entry; |
| 133 | + const { webPort } = parseFrontendEnv(fullPath); |
| 134 | + result.webPort = webPort; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // 检测基础设施 |
| 139 | + const infraDir = join(projectDir, "infra"); |
| 140 | + if (existsSync(infraDir) && statSync(infraDir).isDirectory()) { |
| 141 | + result.hasInfra = true; |
| 142 | + result.infraServices = parseInfraServices(projectDir); |
| 143 | + } |
| 144 | + |
| 145 | + return result; |
| 146 | +} |
| 147 | + |
| 148 | +// ─── 命令入口 ─── |
| 149 | + |
| 150 | +export async function addAction() { |
| 151 | + clack.intro(chalk.bgCyan(" fba-cli add ")); |
| 152 | + |
| 153 | + // 1. 输入项目目录 |
| 154 | + const projectDirInput = await clack.text({ |
| 155 | + message: t("addProjectDir"), |
| 156 | + placeholder: process.cwd(), |
| 157 | + defaultValue: process.cwd(), |
| 158 | + validate: (v) => { |
| 159 | + const dir = v?.trim() ? resolve(v.trim()) : process.cwd(); |
| 160 | + if (!existsSync(dir)) return t("projectRootNotExist"); |
| 161 | + if (!statSync(dir).isDirectory()) return t("projectRootNotDirectory"); |
| 162 | + return undefined; |
| 163 | + }, |
| 164 | + }); |
| 165 | + if (clack.isCancel(projectDirInput)) { |
| 166 | + clack.outro(chalk.dim("Cancelled")); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + const projectDir = projectDirInput?.trim() |
| 171 | + ? resolve(String(projectDirInput).trim()) |
| 172 | + : process.cwd(); |
| 173 | + |
| 174 | + // 检查是否已注册 |
| 175 | + const globalConfig = readGlobalConfig(); |
| 176 | + if (globalConfig.projects.some((p) => p.path === projectDir)) { |
| 177 | + clack.log.error(chalk.red(t("addAlreadyRegistered"))); |
| 178 | + clack.outro(""); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + // 2. 扫描项目结构 |
| 183 | + const spinner = clack.spinner(); |
| 184 | + spinner.start(t("addScanning")); |
| 185 | + const detected = detectProject(projectDir); |
| 186 | + spinner.stop(t("addScanning")); |
| 187 | + |
| 188 | + // 3. 验证结果 |
| 189 | + if (!detected.backendName) { |
| 190 | + clack.log.error(chalk.red(t("addNoBackend"))); |
| 191 | + clack.outro(""); |
| 192 | + return; |
| 193 | + } |
| 194 | + if (!detected.frontendName) { |
| 195 | + clack.log.error(chalk.red(t("addNoFrontend"))); |
| 196 | + clack.outro(""); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + // 4. 展示探测结果 |
| 201 | + clack.log.step(t("addConfirmDetected")); |
| 202 | + clack.log.info( |
| 203 | + `${chalk.bold(t("addBackendDetected"))}: ${chalk.green(detected.backendName)}`, |
| 204 | + ); |
| 205 | + clack.log.info( |
| 206 | + `${chalk.bold(t("addFrontendDetected"))}: ${chalk.green(detected.frontendName)}`, |
| 207 | + ); |
| 208 | + clack.log.info( |
| 209 | + `${chalk.bold(t("addInfraDetected"))}: ${detected.hasInfra ? chalk.green(t("addInfraYes")) : chalk.dim(t("addInfraNo"))}` + |
| 210 | + (detected.infraServices.length > 0 |
| 211 | + ? ` (${detected.infraServices.join(", ")})` |
| 212 | + : ""), |
| 213 | + ); |
| 214 | + if (detected.dbType) { |
| 215 | + clack.log.info( |
| 216 | + `${chalk.bold(t("addDbTypeDetected"))}: ${chalk.green(detected.dbType)}`, |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | + const confirmed = await clack.confirm({ |
| 221 | + message: t("addConfirmDetected"), |
| 222 | + initialValue: true, |
| 223 | + }); |
| 224 | + if (clack.isCancel(confirmed) || !confirmed) { |
| 225 | + clack.outro(chalk.dim("Cancelled")); |
| 226 | + return; |
| 227 | + } |
| 228 | + |
| 229 | + // 5. 收集需要用户输入的配置 |
| 230 | + const userConfig = await clack.group( |
| 231 | + { |
| 232 | + projectName: () => |
| 233 | + clack.text({ |
| 234 | + message: t("addProjectName"), |
| 235 | + placeholder: basename(projectDir), |
| 236 | + defaultValue: basename(projectDir), |
| 237 | + validate: (v) => { |
| 238 | + if (!v?.trim()) return t("projectNameRequired"); |
| 239 | + return undefined; |
| 240 | + }, |
| 241 | + }), |
| 242 | + dbType: () => { |
| 243 | + if (detected.dbType) return Promise.resolve(detected.dbType); |
| 244 | + return clack.select({ |
| 245 | + message: t("dbTypeSelect"), |
| 246 | + options: [ |
| 247 | + { value: "postgresql", label: t("infraPostgres") }, |
| 248 | + { value: "mysql", label: t("infraMysql") }, |
| 249 | + ], |
| 250 | + }); |
| 251 | + }, |
| 252 | + serverPort: () => |
| 253 | + clack.text({ |
| 254 | + message: t("addServerPort"), |
| 255 | + defaultValue: "8000", |
| 256 | + }), |
| 257 | + webPort: () => |
| 258 | + clack.text({ |
| 259 | + message: t("addWebPort"), |
| 260 | + defaultValue: detected.webPort ? String(detected.webPort) : "5173", |
| 261 | + }), |
| 262 | + }, |
| 263 | + { |
| 264 | + onCancel: () => { |
| 265 | + clack.outro(chalk.dim("Cancelled")); |
| 266 | + process.exit(0); |
| 267 | + }, |
| 268 | + }, |
| 269 | + ); |
| 270 | + |
| 271 | + const projectName = String(userConfig.projectName).trim(); |
| 272 | + const dbType = String(userConfig.dbType) as DatabaseType; |
| 273 | + const serverPort = parseInt(String(userConfig.serverPort)) || 8000; |
| 274 | + const webPort = parseInt(String(userConfig.webPort)) || 5173; |
| 275 | + |
| 276 | + // 6. 写入项目配置 |
| 277 | + const projConfig: ProjectConfig = { |
| 278 | + name: projectName, |
| 279 | + backend_name: detected.backendName, |
| 280 | + frontend_name: detected.frontendName, |
| 281 | + server_port: serverPort, |
| 282 | + web_port: webPort, |
| 283 | + infra: detected.hasInfra, |
| 284 | + infra_services: detected.infraServices, |
| 285 | + db_type: dbType, |
| 286 | + }; |
| 287 | + writeProjectConfig(projectDir, projConfig); |
| 288 | + |
| 289 | + // 7. 注册到全局配置 |
| 290 | + addProject({ |
| 291 | + name: projectName, |
| 292 | + path: projectDir, |
| 293 | + createdAt: new Date().toISOString(), |
| 294 | + }); |
| 295 | + |
| 296 | + // 8. 完成 |
| 297 | + clack.log.success(chalk.green.bold(t("addSuccess"))); |
| 298 | + clack.note( |
| 299 | + [ |
| 300 | + `${chalk.bold(t("addProjectName"))}: ${projectName}`, |
| 301 | + `${chalk.bold(t("addBackendDetected"))}: ${detected.backendName}`, |
| 302 | + `${chalk.bold(t("addFrontendDetected"))}: ${detected.frontendName}`, |
| 303 | + `${chalk.bold(t("addDbTypeDetected"))}: ${dbType}`, |
| 304 | + `${chalk.bold(t("serverPort"))}: ${serverPort}`, |
| 305 | + `${chalk.bold(t("webPort"))}: ${webPort}`, |
| 306 | + ].join("\n"), |
| 307 | + projectDir, |
| 308 | + ); |
| 309 | + clack.outro(chalk.cyan(t("happyCoding"))); |
| 310 | +} |
0 commit comments