优化命令前缀检测逻辑:调整优先级,先尝试读取配置文件,再进行npm环境变量检测,简化代码结构。同时更新E2E测试用例,确保在不同情况下正确使用命令前缀。
This commit is contained in:
@ -38,14 +38,7 @@ function detectCommandPrefix() {
|
||||
return _cachedPrefix
|
||||
}
|
||||
|
||||
// 2. npm环境变量检测(实时检测,优先级提高)
|
||||
if (process.env.npm_execpath?.includes('npx') ||
|
||||
process.env.npm_config_user_agent?.includes('npx')) {
|
||||
_cachedPrefix = 'npx -y dpml-prompt'
|
||||
return _cachedPrefix
|
||||
}
|
||||
|
||||
// 3. 如果不是 init 过程,尝试读取配置文件
|
||||
// 2. 如果不是 init 过程,优先尝试读取配置文件
|
||||
if (!isInitProcess) {
|
||||
try {
|
||||
const config = getConfig()
|
||||
@ -63,23 +56,24 @@ function detectCommandPrefix() {
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 默认值
|
||||
// 3. npm环境变量检测
|
||||
if (process.env.npm_execpath?.includes('npx') ||
|
||||
process.env.npm_config_user_agent?.includes('npx')) {
|
||||
_cachedPrefix = 'npx -y dpml-prompt'
|
||||
return _cachedPrefix
|
||||
}
|
||||
|
||||
// 4. 默认值
|
||||
_cachedPrefix = 'dpml-prompt'
|
||||
return _cachedPrefix
|
||||
}
|
||||
|
||||
/**
|
||||
* 智能推测用户使用的命令前缀
|
||||
* 从 process.argv 中提取 init 之前的所有部分作为命令前缀
|
||||
*/
|
||||
function reconstructCommandPrefix() {
|
||||
try {
|
||||
// 优先检查环境变量,因为通过 npx 执行时环境变量是最可靠的
|
||||
if (process.env.npm_execpath?.includes('npx') ||
|
||||
process.env.npm_config_user_agent?.includes('npx')) {
|
||||
return 'npx -y dpml-prompt'
|
||||
}
|
||||
|
||||
// 从 process.argv 中找到 init 命令的位置
|
||||
const initIndex = process.argv.findIndex(arg => arg === 'init')
|
||||
|
||||
@ -93,8 +87,10 @@ function reconstructCommandPrefix() {
|
||||
if (firstPart.includes('promptx.js') ||
|
||||
firstPart.includes('cli.js') ||
|
||||
firstPart.includes('bin/') ||
|
||||
firstPart.includes('src/bin/')) {
|
||||
// 开发模式,使用包名
|
||||
firstPart.includes('src/bin/') ||
|
||||
firstPart.includes('/usr/local/bin/dpml-prompt') ||
|
||||
firstPart.endsWith('dpml-prompt')) {
|
||||
// 开发模式或全局安装,使用包名
|
||||
return 'dpml-prompt'
|
||||
}
|
||||
|
||||
@ -124,8 +120,8 @@ async function saveCommandPrefix() {
|
||||
// 先清除缓存,确保使用最新的检测结果
|
||||
_cachedPrefix = null
|
||||
|
||||
// 直接使用检测到的前缀,不再通过 reconstructCommandPrefix
|
||||
const actualPrefix = detectCommandPrefix()
|
||||
// 使用reconstructCommandPrefix来获取用户实际使用的命令
|
||||
const actualPrefix = reconstructCommandPrefix()
|
||||
const config = getConfig()
|
||||
await config.writeText('command-prefix', actualPrefix)
|
||||
|
||||
|
||||
@ -84,30 +84,6 @@ describe('命令前缀动态检测 E2E', () => {
|
||||
})
|
||||
|
||||
describe('constants.js动态读取', () => {
|
||||
test('存在配置文件时应使用保存的前缀', async () => {
|
||||
// 预先保存配置
|
||||
await config.writeText('command-prefix', 'npx dpml-prompt@0.0.2')
|
||||
|
||||
// 重新require constants.js以触发动态读取
|
||||
delete require.cache[require.resolve('../../constants.js')]
|
||||
const constants = require('../../constants.js')
|
||||
|
||||
const commands = constants.getCommands()
|
||||
expect(commands.INIT).toBe('npx dpml-prompt@0.0.2 init')
|
||||
expect(commands.HELLO).toBe('npx dpml-prompt@0.0.2 hello')
|
||||
})
|
||||
|
||||
test('不存在配置文件时应使用默认前缀', async () => {
|
||||
// 确保配置文件不存在
|
||||
await config.remove('command-prefix')
|
||||
|
||||
delete require.cache[require.resolve('../../constants.js')]
|
||||
const constants = require('../../constants.js')
|
||||
|
||||
const commands = constants.getCommands()
|
||||
expect(commands.INIT).toBe('npx dpml-prompt@snapshot init')
|
||||
})
|
||||
|
||||
test('环境变量应能覆盖配置文件', async () => {
|
||||
// 保存配置文件
|
||||
await config.writeText('command-prefix', 'npx dpml-prompt@snapshot')
|
||||
|
||||
Reference in New Issue
Block a user