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
View File
@@ -20,3 +20,4 @@ export * from "./opencode-config-dir"
export * from "./opencode-version"
export * from "./permission-compat"
export * from "./external-plugin-detector"
export * from "./zip-extractor"
+83
View File
@@ -0,0 +1,83 @@
import { spawn, spawnSync } from "bun"
import { release } from "os"
const WINDOWS_BUILD_WITH_TAR = 17134
function getWindowsBuildNumber(): number | null {
if (process.platform !== "win32") return null
const parts = release().split(".")
if (parts.length >= 3) {
const build = parseInt(parts[2], 10)
if (!isNaN(build)) return build
}
return null
}
function isPwshAvailable(): boolean {
if (process.platform !== "win32") return false
const result = spawnSync(["where", "pwsh"], { stdout: "pipe", stderr: "pipe" })
return result.exitCode === 0
}
function escapePowerShellPath(path: string): string {
return path.replace(/'/g, "''")
}
type WindowsZipExtractor = "tar" | "pwsh" | "powershell"
function getWindowsZipExtractor(): WindowsZipExtractor {
const buildNumber = getWindowsBuildNumber()
if (buildNumber !== null && buildNumber >= WINDOWS_BUILD_WITH_TAR) {
return "tar"
}
if (isPwshAvailable()) {
return "pwsh"
}
return "powershell"
}
export async function extractZip(archivePath: string, destDir: string): Promise<void> {
let proc
if (process.platform === "win32") {
const extractor = getWindowsZipExtractor()
switch (extractor) {
case "tar":
proc = spawn(["tar", "-xf", archivePath, "-C", destDir], {
stdout: "ignore",
stderr: "pipe",
})
break
case "pwsh":
proc = spawn(["pwsh", "-Command", `Expand-Archive -Path '${escapePowerShellPath(archivePath)}' -DestinationPath '${escapePowerShellPath(destDir)}' -Force`], {
stdout: "ignore",
stderr: "pipe",
})
break
case "powershell":
default:
proc = spawn(["powershell", "-Command", `Expand-Archive -Path '${escapePowerShellPath(archivePath)}' -DestinationPath '${escapePowerShellPath(destDir)}' -Force`], {
stdout: "ignore",
stderr: "pipe",
})
break
}
} else {
proc = spawn(["unzip", "-o", archivePath, "-d", destDir], {
stdout: "ignore",
stderr: "pipe",
})
}
const exitCode = await proc.exited
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text()
throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}`)
}
}