Revert "feat(third_party) Port get-ripgrep." (#8923)

This commit is contained in:
joshualitt
2025-09-19 11:08:41 -07:00
committed by GitHub
parent 2bf226e918
commit 7681c595b5
16 changed files with 129 additions and 308 deletions

View File

@@ -1,20 +1,19 @@
{
"name": "get-ripgrep",
"name": "@lvce-editor/ripgrep",
"version": "0.0.0-dev",
"description": "A module for downloading ripgrep at runtime a Node project",
"main": "dist/index.js",
"files": [
"dist"
],
"description": "A module for using ripgrep in a Node project",
"main": "src/index.js",
"typings": "src/index.d.ts",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/lvce-editor/ripgrep"
},
"scripts": {
"build": "node ../../scripts/build_package.js",
"prepublishOnly": "npm run build",
"format": "prettier --write .",
"lint": "eslint . --ext .ts,.tsx",
"test": "vitest run",
"test:ci": "vitest run --coverage",
"typecheck": "tsc --noEmit"
"postinstall": "node ./src/postinstall.js",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
"format": "prettier --write ."
},
"keywords": [
"lvce-editor",
@@ -23,17 +22,20 @@
"author": "Lvce Editor",
"license": "MIT",
"dependencies": {
"@lvce-editor/verror": "^1.6.0",
"execa": "^9.5.2",
"extract-zip": "^2.0.1",
"fs-extra": "^11.3.0",
"got": "^14.4.5",
"path-exists": "^5.0.0",
"tempy": "^3.1.0",
"xdg-basedir": "^5.1.0"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.14",
"@types/node": "^22.13.0",
"vitest": "^3.1.1",
"jest": "^29.7.0",
"prettier": "^3.4.2",
"typescript": "^5.7.3"
},

View File

@@ -4,6 +4,7 @@
* Copyright 2023 Lvce Editor
* SPDX-License-Identifier: MIT
*/
import { VError } from '@lvce-editor/verror'
import { execa } from 'execa'
import extractZip from 'extract-zip'
import fsExtra from 'fs-extra'
@@ -12,19 +13,22 @@ import * as os from 'node:os'
import { dirname, join } from 'node:path'
import { pathExists } from 'path-exists'
import { pipeline } from 'node:stream/promises'
import { temporaryFile } from 'tempy'
import { fileURLToPath } from 'node:url'
import { xdgCache } from 'xdg-basedir'
import path from 'path'
const { mkdir, createWriteStream, move } = fsExtra
const __dirname = dirname(fileURLToPath(import.meta.url))
const REPOSITORY = `microsoft/ripgrep-prebuilt`
const VERSION = process.env['RIPGREP_VERSION'] || 'v13.0.0-10'
const VERSION = process.env.RIPGREP_VERSION || 'v13.0.0-10'
console.log({ VERSION })
const BIN_PATH = join(__dirname, '../bin')
const getTarget = () => {
const arch = process.env['npm_config_arch'] || os.arch()
const platform = process.env['platform'] || os.platform()
const arch = process.env.npm_config_arch || os.arch()
const platform = process.env.platform || os.platform()
switch (platform) {
case 'darwin':
switch (arch) {
@@ -59,60 +63,61 @@ const getTarget = () => {
return 'i686-unknown-linux-musl.tar.gz'
}
default:
throw new Error('Unknown platform: ' + platform)
throw new VError('Unknown platform: ' + platform)
}
}
async function downloadFile(url: string, outFile: string): Promise<void> {
let tmpDir = undefined
export const downloadFile = async (url, outFile) => {
try {
tmpDir = await fsExtra.mkdtemp('download-ripgrep')
const tmpFile = path.join(tmpDir, 'tmp-file')
await pipeline(got.stream(url), fsExtra.createWriteStream(tmpFile))
await fsExtra.mkdir(dirname(outFile), { recursive: true })
await fsExtra.move(tmpFile, outFile)
const tmpFile = temporaryFile()
await pipeline(got.stream(url), createWriteStream(tmpFile))
await mkdir(dirname(outFile), { recursive: true })
await move(tmpFile, outFile)
} catch (error) {
throw new Error(`Failed to download "${url}": ${error}`)
} finally {
if (tmpDir) {
await fsExtra.rm(tmpDir, { recursive: true, force: true })
}
throw new VError(error, `Failed to download "${url}"`)
}
}
async function unzip(inFile: string, outDir: string): Promise<void> {
/**
* @param {string} inFile
* @param {string} outDir
*/
const unzip = async (inFile, outDir) => {
try {
await fsExtra.mkdir(outDir, { recursive: true })
await mkdir(outDir, { recursive: true })
await extractZip(inFile, { dir: outDir })
} catch (error) {
throw new Error(`Failed to unzip "${inFile}": ${error}`)
throw new VError(error, `Failed to unzip "${inFile}"`)
}
}
async function untarGz(inFile: string, outDir: string): Promise<void> {
/**
* @param {string} inFile
* @param {string} outDir
*/
const untarGz = async (inFile, outDir) => {
try {
await fsExtra.mkdir(outDir, { recursive: true })
await mkdir(outDir, { recursive: true })
await execa('tar', ['xvf', inFile, '-C', outDir])
} catch (error) {
throw new Error(`Failed to extract "${inFile}": ${error}`)
throw new VError(error, `Failed to extract "${inFile}"`)
}
}
export async function downloadRipGrep(overrideBinPath?: string): Promise<void> {
export const downloadRipGrep = async () => {
const target = getTarget()
const url = `https://github.com/${REPOSITORY}/releases/download/${VERSION}/ripgrep-${VERSION}-${target}`
const downloadPath = `${xdgCache}/vscode-ripgrep/ripgrep-${VERSION}-${target}`
const binPath = overrideBinPath ?? BIN_PATH
if (!(await pathExists(downloadPath))) {
await downloadFile(url, downloadPath)
} else {
console.info(`File ${downloadPath} has been cached`)
}
if (downloadPath.endsWith('.tar.gz')) {
await untarGz(downloadPath, binPath)
await untarGz(downloadPath, BIN_PATH)
} else if (downloadPath.endsWith('.zip')) {
await unzip(downloadPath, binPath)
await unzip(downloadPath, BIN_PATH)
} else {
throw new Error(`Invalid downloadPath ${downloadPath}`)
throw new VError(`Invalid downloadPath ${downloadPath}`)
}
}

View File

@@ -1,172 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Writable, Readable } from 'node:stream'
import fs from 'node:fs'
import { downloadRipGrep } from './downloadRipGrep.js'
import { execa } from 'execa'
import extractZip from 'extract-zip'
import fsExtra from 'fs-extra'
import got from 'got'
import * as os from 'node:os'
import { pathExists } from 'path-exists'
import { pipeline } from 'node:stream/promises'
// Mock dependencies before any imports
vi.mock('execa', () => ({
execa: vi.fn(),
}))
vi.mock('extract-zip', () => ({
default: vi.fn(),
}))
vi.mock('fs-extra', () => ({
default: {
mkdir: vi.fn(),
createWriteStream: vi.fn(() => new Writable()),
move: vi.fn(),
mkdtemp: vi.fn(async (prefix) => `${prefix}`),
rm: vi.fn(),
},
}))
vi.mock('got', () => ({
default: {
stream: vi.fn(),
},
}))
vi.mock('node:os', () => ({
platform: vi.fn(),
arch: vi.fn(),
}))
vi.mock('path-exists', () => ({
pathExists: vi.fn(),
}))
vi.mock('node:stream/promises', () => ({
pipeline: vi.fn(),
}))
vi.mock('xdg-basedir', () => ({
xdgCache: `./mocked/cache`,
}))
describe('downloadRipGrep', () => {
beforeEach(() => {
vi.resetAllMocks()
// Mock got.stream to return a real, empty readable stream
const mockStream = new Readable({
read() {
this.push(null) // Signal end of stream
},
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(got.stream).mockReturnValue(mockStream as any)
vi.mocked(pipeline).mockResolvedValue(undefined)
})
afterEach(() => {
vi.restoreAllMocks()
fs.rmSync('./test/bin', { recursive: true, force: true })
fs.rmSync('./mocked', { recursive: true, force: true })
})
const testMatrix = [
{
platform: 'darwin',
arch: 'arm64',
target: 'aarch64-apple-darwin.tar.gz',
},
{ platform: 'darwin', arch: 'x64', target: 'x86_64-apple-darwin.tar.gz' },
{ platform: 'win32', arch: 'x64', target: 'x86_64-pc-windows-msvc.zip' },
{ platform: 'win32', arch: 'arm', target: 'aarch64-pc-windows-msvc.zip' },
{
platform: 'linux',
arch: 'x64',
target: 'x86_64-unknown-linux-musl.tar.gz',
},
{
platform: 'linux',
arch: 'arm64',
target: 'aarch64-unknown-linux-gnu.tar.gz',
},
]
for (const { platform, arch, target } of testMatrix) {
it(`should download and extract for ${platform}-${arch}`, async () => {
vi.mocked(os.platform).mockReturnValue(platform as NodeJS.Platform)
vi.mocked(os.arch).mockReturnValue(arch)
vi.mocked(pathExists).mockResolvedValue(false)
const binPath = './test/bin'
await downloadRipGrep(binPath)
const version = 'v13.0.0-10'
const expectedUrl = `https://github.com/microsoft/ripgrep-prebuilt/releases/download/${version}/ripgrep-${version}-${target}`
const expectedDownloadPath = `./mocked/cache/vscode-ripgrep/ripgrep-${version}-${target}`
// Check download
expect(got.stream).toHaveBeenCalledWith(expectedUrl)
expect(pipeline).toHaveBeenCalled()
// Check extraction
if (target.endsWith('.tar.gz')) {
expect(execa).toHaveBeenCalledWith('tar', [
'xvf',
expectedDownloadPath,
'-C',
binPath,
])
expect(extractZip).not.toHaveBeenCalled()
} else if (target.endsWith('.zip')) {
expect(extractZip).toHaveBeenCalledWith(expectedDownloadPath, {
dir: binPath,
})
expect(execa).not.toHaveBeenCalled()
}
})
}
it('should use the cached file if it exists', async () => {
vi.mocked(os.platform).mockReturnValue('linux')
vi.mocked(os.arch).mockReturnValue('x64')
vi.mocked(pathExists).mockResolvedValue(true)
const binPath = './test/bin'
await downloadRipGrep(binPath)
expect(got.stream).not.toHaveBeenCalled()
expect(pipeline).not.toHaveBeenCalled()
expect(execa).toHaveBeenCalled() // Still extracts
})
it('should throw an error for an unknown platform', async () => {
vi.mocked(os.platform).mockReturnValue('sunos' as NodeJS.Platform) // an unsupported platform
vi.mocked(os.arch).mockReturnValue('x64')
await expect(downloadRipGrep('./test/bin')).rejects.toThrow(
'Unknown platform: sunos',
)
})
it('should clean up temporary files on successful download', async () => {
vi.mocked(os.platform).mockReturnValue('linux')
vi.mocked(os.arch).mockReturnValue('x64')
vi.mocked(pathExists).mockResolvedValue(false)
await downloadRipGrep('./test/bin')
expect(fsExtra.mkdtemp).toHaveBeenCalledWith('download-ripgrep')
expect(fsExtra.rm).toHaveBeenCalledWith('download-ripgrep', {
recursive: true,
force: true,
})
})
})

17
third_party/get-ripgrep/src/index.js vendored Normal file
View File

@@ -0,0 +1,17 @@
/* eslint-disable */
/**
* @license
* Copyright 2023 Lvce Editor
* SPDX-License-Identifier: MIT
*/
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
export const rgPath = join(
__dirname,
'..',
'bin',
`rg${process.platform === 'win32' ? '.exe' : ''}`,
)

View File

@@ -1,7 +0,0 @@
/* eslint-disable */
/**
* @license
* Copyright 2023 Lvce Editor
* SPDX-License-Identifier: MIT
*/
export { downloadRipGrep } from './downloadRipGrep.js'

View File

@@ -1,13 +0,0 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"lib": ["DOM", "DOM.Iterable", "ES2021"],
"composite": true,
"types": ["node", "vitest/globals"],
"rootDir": "src",
"declaration": true
},
"include": ["src/**/*.ts", "src/**/*.test.ts"],
"exclude": ["node_modules", "dist"]
}