From 2ace9ba492720e2f74055f9e328359ad21714f68 Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Tue, 19 Aug 2025 17:23:40 +0800 Subject: [PATCH] refactor(CodeToolsService): replace npm package version fetching with direct API call; simplify command construction for installation --- src/main/services/CodeToolsService.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/services/CodeToolsService.ts b/src/main/services/CodeToolsService.ts index ccfe1af214..7811e73e41 100644 --- a/src/main/services/CodeToolsService.ts +++ b/src/main/services/CodeToolsService.ts @@ -114,9 +114,21 @@ class CodeToolsService { } else { logger.info(`Fetching latest version for ${packageName} from npm`) try { - const bunPath = await this.getBunPath() - const { stdout } = await execAsync(`"${bunPath}" info ${packageName} version`, { timeout: 15000 }) - latestVersion = stdout.trim().replace(/["']/g, '') + // Get registry URL + const registryUrl = await this.getNpmRegistryUrl() + + // Fetch package info directly from npm registry API + const packageUrl = `${registryUrl}/${packageName}/latest` + const response = await fetch(packageUrl, { + signal: AbortSignal.timeout(15000) + }) + + if (!response.ok) { + throw new Error(`Failed to fetch package info: ${response.statusText}`) + } + + const packageInfo = await response.json() + latestVersion = packageInfo.version logger.info(`${packageName} latest version: ${latestVersion}`) // Cache the result @@ -179,7 +191,7 @@ class CodeToolsService { ? `set "BUN_INSTALL=${bunInstallPath}" && set "NPM_CONFIG_REGISTRY=${registryUrl}" &&` : `export BUN_INSTALL="${bunInstallPath}" && export NPM_CONFIG_REGISTRY="${registryUrl}" &&` - const updateCommand = `${installEnvPrefix} "${bunPath}" install -g ${packageName}` + const updateCommand = `${installEnvPrefix} ${bunPath} install -g ${packageName}` logger.info(`Executing update command: ${updateCommand}`) await execAsync(updateCommand, { timeout: 60000 }) @@ -283,7 +295,7 @@ class CodeToolsService { } // Build command to execute - let baseCommand = `${bunPath} "${executablePath}"` + let baseCommand = `${bunPath} ${executablePath}` const bunInstallPath = path.join(os.homedir(), '.cherrystudio') if (isInstalled) { @@ -299,7 +311,7 @@ class CodeToolsService { ? `set "BUN_INSTALL=${bunInstallPath}" && set "NPM_CONFIG_REGISTRY=${registryUrl}" &&` : `export BUN_INSTALL="${bunInstallPath}" && export NPM_CONFIG_REGISTRY="${registryUrl}" &&` - const installCommand = `${installEnvPrefix} "${bunPath}" install -g ${packageName}` + const installCommand = `${installEnvPrefix} ${bunPath} install -g ${packageName}` baseCommand = `echo "Installing ${packageName}..." && ${installCommand} && echo "Installation complete, starting ${cliTool}..." && "${baseCommand}"` } @@ -322,7 +334,8 @@ end tell` case 'win32': { // Windows - Use temp bat file for debugging const envPrefix = buildEnvPrefix(true) - const command = envPrefix ? `${envPrefix} && ${baseCommand}` : baseCommand + let command = envPrefix ? `${envPrefix} && ${baseCommand}` : baseCommand + command = command.replace(bunPath, '') // Create temp bat file for debugging and avoid complex command line escaping issues const tempDir = path.join(os.tmpdir(), 'cherrystudio')