重构:引入统一的DirectoryService以优化目录管理
- 在InitCommand、RecallCommand、RememberCommand和PouchStateMachine中替换了直接路径处理逻辑,改为使用DirectoryService进行目录解析。 - 更新了ProjectDiscovery以使用新的getProjectRoot方法,标记旧方法为已弃用。 - 在executionContext中重构了工作目录获取逻辑,增强了兼容性和可维护性。 - 确保了对用户主目录的避免处理,提升了目录定位的智能性和可靠性。 此改动旨在提升代码的可读性和一致性,同时为未来的扩展打下基础。
This commit is contained in:
@ -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()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user