重构:引入DirectoryService以优化路径解析和项目根目录查找
- 在多个协议实现中(如ProjectProtocol、PackageProtocol等)引入DirectoryService,替换了直接的路径处理逻辑,增强了路径解析的智能性和可靠性。 - 更新了相关方法以支持异步操作,确保在查找项目根目录和注册表路径时能够优雅地处理错误并回退到默认路径。 - 在PromptXConfig中动态计算.promptx目录路径,提升了配置管理的灵活性。 此改动旨在提升代码的可读性和一致性,同时为未来的扩展打下基础。
This commit is contained in:
@ -4,6 +4,7 @@ const fsPromises = require('fs').promises
|
||||
const ResourceProtocol = require('./ResourceProtocol')
|
||||
const { QueryParams } = require('../types')
|
||||
const logger = require('../../../utils/logger')
|
||||
const { getDirectoryService } = require('../../../utils/DirectoryService')
|
||||
|
||||
/**
|
||||
* 包协议实现
|
||||
@ -16,6 +17,7 @@ class PackageProtocol extends ResourceProtocol {
|
||||
|
||||
// 包安装模式检测缓存
|
||||
this.installModeCache = new Map()
|
||||
this.directoryService = getDirectoryService()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,13 +56,13 @@ class PackageProtocol extends ResourceProtocol {
|
||||
/**
|
||||
* 检测当前包安装模式
|
||||
*/
|
||||
detectInstallMode () {
|
||||
async detectInstallMode () {
|
||||
const cacheKey = 'currentInstallMode'
|
||||
if (this.installModeCache.has(cacheKey)) {
|
||||
return this.installModeCache.get(cacheKey)
|
||||
}
|
||||
|
||||
const mode = this._performInstallModeDetection()
|
||||
const mode = await this._performInstallModeDetection()
|
||||
this.installModeCache.set(cacheKey, mode)
|
||||
return mode
|
||||
}
|
||||
@ -68,8 +70,19 @@ class PackageProtocol extends ResourceProtocol {
|
||||
/**
|
||||
* 执行安装模式检测
|
||||
*/
|
||||
_performInstallModeDetection () {
|
||||
const cwd = process.cwd()
|
||||
async _performInstallModeDetection () {
|
||||
let cwd
|
||||
try {
|
||||
const context = {
|
||||
startDir: process.cwd(),
|
||||
platform: process.platform,
|
||||
avoidUserHome: true
|
||||
}
|
||||
cwd = await this.directoryService.getProjectRoot(context)
|
||||
} catch (error) {
|
||||
cwd = process.cwd()
|
||||
}
|
||||
|
||||
const execPath = process.argv[0]
|
||||
const scriptPath = process.argv[1]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user