From 92996667d5194db9a9619e27e088c28994ea6a10 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 23 Dec 2025 17:36:57 +0800 Subject: [PATCH] refactor: remove CommonJS ripgrep launcher and spawn binary directly Replace the ripgrep_launcher.cjs intermediary with direct binary execution. This simplifies the ripgrep module by spawning the binary directly instead of going through a Node.js subprocess wrapper. Add platform-specific binary path resolution to handle both Unix and Windows environments. --- cli/scripts/ripgrep_launcher.cjs | 33 --------------------------- cli/scripts/unpack-tools.cjs | 5 ++-- cli/src/modules/ripgrep/index.ts | 11 +++++++-- cli/src/runtime/assets.ts | 10 ++------ cli/src/runtime/embeddedAssets.bun.ts | 3 --- 5 files changed, 13 insertions(+), 49 deletions(-) delete mode 100644 cli/scripts/ripgrep_launcher.cjs diff --git a/cli/scripts/ripgrep_launcher.cjs b/cli/scripts/ripgrep_launcher.cjs deleted file mode 100644 index 96b6abe7..00000000 --- a/cli/scripts/ripgrep_launcher.cjs +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env node - -/** - * Ripgrep runner - executed as a subprocess to run the native module - * This file is intentionally written in CommonJS to avoid ESM complexities - */ - -const path = require('path'); - -// Load the native module from unpacked directory -const modulePath = path.join(__dirname, '..', 'tools', 'unpacked', 'ripgrep.node'); -const ripgrepNative = require(modulePath); - -// Get arguments from command line (skip node and script name) -const args = process.argv.slice(2); - -// Parse the JSON-encoded arguments -let parsedArgs; -try { - parsedArgs = JSON.parse(args[0]); -} catch (error) { - console.error('Failed to parse arguments:', error.message); - process.exit(1); -} - -// Run ripgrep -try { - const exitCode = ripgrepNative.ripgrepMain(parsedArgs); - process.exit(exitCode); -} catch (error) { - console.error('Ripgrep error:', error.message); - process.exit(1); -} diff --git a/cli/scripts/unpack-tools.cjs b/cli/scripts/unpack-tools.cjs index 5862bd87..8e820c6a 100644 --- a/cli/scripts/unpack-tools.cjs +++ b/cli/scripts/unpack-tools.cjs @@ -57,8 +57,7 @@ function areToolsUnpacked(toolsDir) { const expectedFiles = [ path.join(unpackedPath, difftBinary), - path.join(unpackedPath, rgBinary), - path.join(unpackedPath, 'ripgrep.node') + path.join(unpackedPath, rgBinary) ]; return expectedFiles.every(file => fs.existsSync(file)); @@ -160,4 +159,4 @@ if (require.main === module) { console.error('Error:', error); process.exit(1); }); -} \ No newline at end of file +} diff --git a/cli/src/modules/ripgrep/index.ts b/cli/src/modules/ripgrep/index.ts index c3bc816e..050faa3a 100644 --- a/cli/src/modules/ripgrep/index.ts +++ b/cli/src/modules/ripgrep/index.ts @@ -4,6 +4,7 @@ import { spawn } from 'child_process'; import { join, resolve } from 'path'; +import { platform } from 'os'; import { runtimePath } from '@/projectPath'; import { withBunRuntimeEnv } from '@/utils/bunRuntime'; @@ -17,10 +18,16 @@ export interface RipgrepOptions { cwd?: string } +function getBinaryPath(): string { + const platformName = platform(); + const binaryName = platformName === 'win32' ? 'rg.exe' : 'rg'; + return resolve(join(runtimePath(), 'tools', 'unpacked', binaryName)); +} + export function run(args: string[], options?: RipgrepOptions): Promise { - const runnerPath = resolve(join(runtimePath(), 'scripts', 'ripgrep_launcher.cjs')); + const binaryPath = getBinaryPath(); return new Promise((resolve, reject) => { - const child = spawn(process.execPath, [runnerPath, JSON.stringify(args)], { + const child = spawn(binaryPath, args, { stdio: ['pipe', 'pipe', 'pipe'], cwd: options?.cwd, env: withBunRuntimeEnv() diff --git a/cli/src/runtime/assets.ts b/cli/src/runtime/assets.ts index 321907db..4f5cde1a 100644 --- a/cli/src/runtime/assets.ts +++ b/cli/src/runtime/assets.ts @@ -61,8 +61,7 @@ function areToolsUnpacked(unpackedPath: string): boolean { const expectedFiles = [ join(unpackedPath, difftBinary), - join(unpackedPath, rgBinary), - join(unpackedPath, 'ripgrep.node') + join(unpackedPath, rgBinary) ]; return expectedFiles.every((file) => existsSync(file)); @@ -115,12 +114,7 @@ function unpackTools(runtimeRoot: string): void { } function runtimeAssetsReady(runtimeRoot: string): boolean { - const requiredScripts = [ - join(runtimeRoot, 'scripts', 'ripgrep_launcher.cjs') - ]; - - return requiredScripts.every((script) => existsSync(script)) && - areToolsUnpacked(join(runtimeRoot, 'tools', 'unpacked')); + return areToolsUnpacked(join(runtimeRoot, 'tools', 'unpacked')); } export async function ensureRuntimeAssets(): Promise { diff --git a/cli/src/runtime/embeddedAssets.bun.ts b/cli/src/runtime/embeddedAssets.bun.ts index 9e826bef..8cfc7ab8 100644 --- a/cli/src/runtime/embeddedAssets.bun.ts +++ b/cli/src/runtime/embeddedAssets.bun.ts @@ -1,7 +1,5 @@ import { feature } from 'bun:bundle'; -import ripgrepLauncher from '../../scripts/ripgrep_launcher.cjs' assert { type: 'file' }; - import difftasticArchiveLicense from '../../tools/archives/difftastic-LICENSE' assert { type: 'file' }; import ripgrepArchiveLicense from '../../tools/archives/ripgrep-LICENSE' assert { type: 'file' }; import difftasticLicense from '../../tools/licenses/difftastic-LICENSE' assert { type: 'file' }; @@ -20,7 +18,6 @@ function asset(relativePath: string, sourcePath: string): EmbeddedAsset { } const COMMON_ASSETS: EmbeddedAsset[] = [ - asset('scripts/ripgrep_launcher.cjs', ripgrepLauncher), asset('tools/archives/difftastic-LICENSE', difftasticArchiveLicense), asset('tools/archives/ripgrep-LICENSE', ripgrepArchiveLicense), asset('tools/licenses/difftastic-LICENSE', difftasticLicense),