|
1 | 1 | // config.ts — 全局 & 项目配置 CRUD |
2 | 2 | import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs' |
3 | | -import { join } from 'path' |
| 3 | +import { join, dirname, resolve, parse } from 'path' |
4 | 4 | import { homedir } from 'os' |
5 | 5 | import type { GlobalConfig, ProjectConfig, ProjectEntry } from '../types/config.js' |
6 | 6 | import { DEFAULT_GLOBAL_CONFIG, DEFAULT_PROJECT_CONFIG } from '../types/config.js' |
@@ -98,10 +98,27 @@ export function writeProjectConfig(projectDir: string, config: ProjectConfig): v |
98 | 98 | // ─── 路径解析 ─── |
99 | 99 |
|
100 | 100 | /** |
101 | | - * 解析项目目录:优先命令行 -p 参数,其次全局 current |
| 101 | + * 从指定目录向上查找包含 .fba.json 的项目根目录 |
| 102 | + */ |
| 103 | +export function findProjectDirUpwards(startDir?: string): string | null { |
| 104 | + let dir = resolve(startDir ?? process.cwd()) |
| 105 | + const root = parse(dir).root |
| 106 | + while (true) { |
| 107 | + if (existsSync(join(dir, '.fba.json'))) return dir |
| 108 | + const parent = dirname(dir) |
| 109 | + if (parent === dir || parent === root) break |
| 110 | + dir = parent |
| 111 | + } |
| 112 | + return null |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * 解析项目目录:优先命令行 -p 参数,其次 cwd 向上查找,最后全局 current |
102 | 117 | */ |
103 | 118 | export function resolveProjectDir(cliProjectDir?: string): string | null { |
104 | 119 | if (cliProjectDir) return cliProjectDir |
| 120 | + const fromCwd = findProjectDirUpwards() |
| 121 | + if (fromCwd) return fromCwd |
105 | 122 | return getCurrentProjectPath() |
106 | 123 | } |
107 | 124 |
|
|
0 commit comments