重构:引入统一的DirectoryService以优化目录管理

- 在InitCommand、RecallCommand、RememberCommand和PouchStateMachine中替换了直接路径处理逻辑,改为使用DirectoryService进行目录解析。
- 更新了ProjectDiscovery以使用新的getProjectRoot方法,标记旧方法为已弃用。
- 在executionContext中重构了工作目录获取逻辑,增强了兼容性和可维护性。
- 确保了对用户主目录的避免处理,提升了目录定位的智能性和可靠性。

此改动旨在提升代码的可读性和一致性,同时为未来的扩展打下基础。
This commit is contained in:
sean
2025-06-15 11:23:19 +08:00
parent 2d90a7089e
commit 041ece9af1
10 changed files with 1198 additions and 68 deletions

View File

@ -128,33 +128,15 @@ class ProjectDiscovery extends BaseDiscovery {
/**
* 查找项目根目录
* @deprecated 使用 DirectoryService.getProjectRoot() 替代
* @returns {Promise<string>} 项目根目录路径
*/
async _findProjectRoot() {
const cacheKey = 'projectRoot'
const cached = this.getFromCache(cacheKey)
if (cached) {
return cached
}
let currentDir = process.cwd()
// 向上查找包含package.json的目录
while (currentDir !== path.dirname(currentDir)) {
const packageJsonPath = path.join(currentDir, 'package.json')
if (await this._fsExists(packageJsonPath)) {
this.setCache(cacheKey, currentDir)
return currentDir
}
currentDir = path.dirname(currentDir)
}
// 如果没找到package.json返回当前工作目录
const fallbackRoot = process.cwd()
this.setCache(cacheKey, fallbackRoot)
return fallbackRoot
// 使用新的统一目录服务
const { getDirectoryService } = require('../../../utils/DirectoryService')
const directoryService = getDirectoryService()
return await directoryService.getProjectRoot()
}
/**