From 91029b1c77eda775fc3683878514f1590f9bafa0 Mon Sep 17 00:00:00 2001 From: sean Date: Wed, 4 Jun 2025 16:25:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=BD=E4=BB=A4=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E6=A3=80=E6=B5=8B=E9=80=BB=E8=BE=91=EF=BC=9A=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=BC=98=E5=85=88=E7=BA=A7=EF=BC=8C=E5=85=88=E5=B0=9D?= =?UTF-8?q?=E8=AF=95=E8=AF=BB=E5=8F=96=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=86=8D=E8=BF=9B=E8=A1=8Cnpm=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E6=A3=80=E6=B5=8B=EF=BC=8C=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=E3=80=82=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0E2E=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=9C=A8=E4=B8=8D=E5=90=8C=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=E6=AD=A3=E7=A1=AE=E4=BD=BF=E7=94=A8=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=89=8D=E7=BC=80=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants.js | 34 ++++++++----------- src/tests/commands/command-prefix.e2e.test.js | 24 ------------- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/src/constants.js b/src/constants.js index dfc3fbd..21e49b0 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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,8 +56,15 @@ function detectCommandPrefix() { } } + // 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 = 'npx -y dpml-prompt' + _cachedPrefix = 'dpml-prompt' return _cachedPrefix } @@ -74,12 +74,6 @@ function detectCommandPrefix() { */ 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) diff --git a/src/tests/commands/command-prefix.e2e.test.js b/src/tests/commands/command-prefix.e2e.test.js index 802d91b..753d5f8 100644 --- a/src/tests/commands/command-prefix.e2e.test.js +++ b/src/tests/commands/command-prefix.e2e.test.js @@ -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')