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
+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)
}
}
}