Skip to content

Commit bfc6a9e

Browse files
yuWormclaude
andcommitted
feat: support use . to set current dir as default project and auto-detect project from cwd
resolveProjectDir now walks up from cwd to find .fba.json, so commands work from any subdirectory inside a project. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 739e0c8 commit bfc6a9e

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

src/commands/use.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
// use.ts — 切换默认项目
2+
import { resolve } from 'path'
23
import * as clack from '@clack/prompts'
34
import chalk from 'chalk'
4-
import { readGlobalConfig, setCurrentProject } from '../lib/config.js'
5+
import { readGlobalConfig, setCurrentProject, findProjectDirUpwards } from '../lib/config.js'
56
import { t } from '../lib/i18n.js'
67

7-
export async function useAction() {
8+
export async function useAction(dir?: string) {
89
const config = readGlobalConfig()
910

11+
// fba use . 或 fba use <dir>:从指定目录向上查找项目
12+
if (dir) {
13+
const startDir = resolve(dir)
14+
const projectDir = findProjectDirUpwards(startDir)
15+
if (!projectDir) {
16+
console.log(chalk.red(t('useNotFbaProject')))
17+
return
18+
}
19+
20+
const entry = config.projects.find(p => p.path === projectDir)
21+
if (!entry) {
22+
console.log(chalk.red(t('useNotRegistered')))
23+
console.log(chalk.dim(t('useHintAdd')))
24+
return
25+
}
26+
27+
setCurrentProject(projectDir)
28+
console.log(chalk.green(`${t('projectSwitched')} ${entry.name}`))
29+
return
30+
}
31+
32+
// 交互式选择
1033
if (config.projects.length === 0) {
1134
console.log(chalk.dim(t('projectListEmpty')))
1235
return

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ program
193193
})
194194

195195
program
196-
.command('use')
196+
.command('use [dir]')
197197
.description(t('cmdUse'))
198-
.action(async () => {
198+
.action(async (dir?: string) => {
199199
const { useAction } = await import('./commands/use.js')
200-
await useAction()
200+
await useAction(dir)
201201
})
202202

203203
program

src/lib/config.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// config.ts — 全局 & 项目配置 CRUD
22
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
3-
import { join } from 'path'
3+
import { join, dirname, resolve, parse } from 'path'
44
import { homedir } from 'os'
55
import type { GlobalConfig, ProjectConfig, ProjectEntry } from '../types/config.js'
66
import { DEFAULT_GLOBAL_CONFIG, DEFAULT_PROJECT_CONFIG } from '../types/config.js'
@@ -98,10 +98,27 @@ export function writeProjectConfig(projectDir: string, config: ProjectConfig): v
9898
// ─── 路径解析 ───
9999

100100
/**
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
102117
*/
103118
export function resolveProjectDir(cliProjectDir?: string): string | null {
104119
if (cliProjectDir) return cliProjectDir
120+
const fromCwd = findProjectDirUpwards()
121+
if (fromCwd) return fromCwd
105122
return getCurrentProjectPath()
106123
}
107124

src/lib/i18n.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ const messages = {
194194
projectListEmpty: "暂无已注册项目",
195195
goClipboard: "已复制到剪贴板!粘贴以导航:",
196196

197+
// Use
198+
useNotFbaProject: "当前目录不是 FBA 项目(未找到 .fba.json)",
199+
useNotRegistered: "该项目尚未注册到管理列表",
200+
useHintAdd: '运行 "fba-cli add" 将项目添加到管理列表',
201+
197202
// Errors & hints
198203
projectDirNotExist: "项目目录不存在",
199204
backendDirNotFound: "后端目录未找到",
@@ -497,6 +502,11 @@ const messages = {
497502
projectListEmpty: "No projects registered",
498503
goClipboard: "Copied to clipboard! Paste to navigate:",
499504

505+
// Use
506+
useNotFbaProject: "Current directory is not an FBA project (.fba.json not found)",
507+
useNotRegistered: "This project is not registered in the managed list",
508+
useHintAdd: 'Run "fba-cli add" to add the project to the managed list',
509+
500510
projectDirNotExist: "Project directory does not exist",
501511
backendDirNotFound: "Backend directory not found",
502512
frontendDirNotFound: "Frontend directory not found",

0 commit comments

Comments
 (0)