fix: use version-aware zip extraction on Windows (#563)

This commit is contained in:
popododo0720
2026-01-11 18:21:48 +09:00
committed by GitHub
parent a0469381a7
commit 91251e173c
5 changed files with 98 additions and 91 deletions
+1 -24
View File
@@ -1,8 +1,8 @@
import { spawn } from "bun"
import { existsSync, mkdirSync, chmodSync, unlinkSync } from "fs"
import { join } from "path"
import { homedir } from "os"
import { createRequire } from "module"
import { extractZip } from "../../shared"
const REPO = "ast-grep/ast-grep"
@@ -56,30 +56,7 @@ export function getCachedBinaryPath(): string | null {
return existsSync(binaryPath) ? binaryPath : null
}
async function extractZip(archivePath: string, destDir: string): Promise<void> {
const proc =
process.platform === "win32"
? spawn(
[
"powershell",
"-command",
`Expand-Archive -Path '${archivePath}' -DestinationPath '${destDir}' -Force`,
],
{ stdout: "pipe", stderr: "pipe" }
)
: spawn(["unzip", "-o", archivePath, "-d", destDir], { stdout: "pipe", stderr: "pipe" })
const exitCode = await proc.exited
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text()
const toolHint =
process.platform === "win32"
? "Ensure PowerShell is available on your system."
: "Please install 'unzip' (e.g., apt install unzip, brew install unzip)."
throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}\n\n${toolHint}`)
}
}
export async function downloadAstGrep(version: string = DEFAULT_VERSION): Promise<string | null> {
const platformKey = `${process.platform}-${process.arch}`
+11 -44
View File
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync, chmodSync, unlinkSync, readdirSync } from "node:fs"
import { join } from "node:path"
import { spawn } from "bun"
import { extractZip as extractZipBase } from "../../shared"
export function findFileRecursive(dir: string, filename: string): string | null {
try {
@@ -74,51 +75,17 @@ async function extractTarGz(archivePath: string, destDir: string): Promise<void>
}
}
async function extractZipWindows(archivePath: string, destDir: string): Promise<void> {
const proc = spawn(
["powershell", "-Command", `Expand-Archive -Path '${archivePath}' -DestinationPath '${destDir}' -Force`],
{ stdout: "pipe", stderr: "pipe" }
)
const exitCode = await proc.exited
if (exitCode !== 0) {
throw new Error("Failed to extract zip with PowerShell")
}
const foundPath = findFileRecursive(destDir, "rg.exe")
if (foundPath) {
const destPath = join(destDir, "rg.exe")
if (foundPath !== destPath) {
const { renameSync } = await import("node:fs")
renameSync(foundPath, destPath)
}
}
}
async function extractZipUnix(archivePath: string, destDir: string): Promise<void> {
const proc = spawn(["unzip", "-o", archivePath, "-d", destDir], {
stdout: "pipe",
stderr: "pipe",
})
const exitCode = await proc.exited
if (exitCode !== 0) {
throw new Error("Failed to extract zip")
}
const foundPath = findFileRecursive(destDir, "rg")
if (foundPath) {
const destPath = join(destDir, "rg")
if (foundPath !== destPath) {
const { renameSync } = await import("node:fs")
renameSync(foundPath, destPath)
}
}
}
async function extractZip(archivePath: string, destDir: string): Promise<void> {
if (process.platform === "win32") {
await extractZipWindows(archivePath, destDir)
} else {
await extractZipUnix(archivePath, destDir)
await extractZipBase(archivePath, destDir)
const binaryName = process.platform === "win32" ? "rg.exe" : "rg"
const foundPath = findFileRecursive(destDir, binaryName)
if (foundPath) {
const destPath = join(destDir, binaryName)
if (foundPath !== destPath) {
const { renameSync } = await import("node:fs")
renameSync(foundPath, destPath)
}
}
}