fix: use version-aware zip extraction on Windows (#563)
This commit is contained in:
parent
f615b012e7
commit
10a5bab94d
@ -3,6 +3,7 @@ import { existsSync, mkdirSync, chmodSync, unlinkSync, appendFileSync } from "fs
|
|||||||
import { join } from "path"
|
import { join } from "path"
|
||||||
import { homedir, tmpdir } from "os"
|
import { homedir, tmpdir } from "os"
|
||||||
import { createRequire } from "module"
|
import { createRequire } from "module"
|
||||||
|
import { extractZip } from "../../shared"
|
||||||
|
|
||||||
const DEBUG = process.env.COMMENT_CHECKER_DEBUG === "1"
|
const DEBUG = process.env.COMMENT_CHECKER_DEBUG === "1"
|
||||||
const DEBUG_FILE = join(tmpdir(), "comment-checker-debug.log")
|
const DEBUG_FILE = join(tmpdir(), "comment-checker-debug.log")
|
||||||
@ -95,29 +96,7 @@ async function extractTarGz(archivePath: string, destDir: string): Promise<void>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract zip archive using system commands.
|
|
||||||
*/
|
|
||||||
async function extractZip(archivePath: string, destDir: string): Promise<void> {
|
|
||||||
debugLog("Extracting zip:", archivePath, "to", destDir)
|
|
||||||
|
|
||||||
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()
|
|
||||||
throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download the comment-checker binary from GitHub Releases.
|
* Download the comment-checker binary from GitHub Releases.
|
||||||
|
|||||||
@ -20,3 +20,4 @@ export * from "./opencode-config-dir"
|
|||||||
export * from "./opencode-version"
|
export * from "./opencode-version"
|
||||||
export * from "./permission-compat"
|
export * from "./permission-compat"
|
||||||
export * from "./external-plugin-detector"
|
export * from "./external-plugin-detector"
|
||||||
|
export * from "./zip-extractor"
|
||||||
|
|||||||
83
src/shared/zip-extractor.ts
Normal file
83
src/shared/zip-extractor.ts
Normal 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}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,8 +1,8 @@
|
|||||||
import { spawn } from "bun"
|
|
||||||
import { existsSync, mkdirSync, chmodSync, unlinkSync } from "fs"
|
import { existsSync, mkdirSync, chmodSync, unlinkSync } from "fs"
|
||||||
import { join } from "path"
|
import { join } from "path"
|
||||||
import { homedir } from "os"
|
import { homedir } from "os"
|
||||||
import { createRequire } from "module"
|
import { createRequire } from "module"
|
||||||
|
import { extractZip } from "../../shared"
|
||||||
|
|
||||||
const REPO = "ast-grep/ast-grep"
|
const REPO = "ast-grep/ast-grep"
|
||||||
|
|
||||||
@ -56,30 +56,7 @@ export function getCachedBinaryPath(): string | null {
|
|||||||
return existsSync(binaryPath) ? binaryPath : 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> {
|
export async function downloadAstGrep(version: string = DEFAULT_VERSION): Promise<string | null> {
|
||||||
const platformKey = `${process.platform}-${process.arch}`
|
const platformKey = `${process.platform}-${process.arch}`
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { existsSync, mkdirSync, chmodSync, unlinkSync, readdirSync } from "node:fs"
|
import { existsSync, mkdirSync, chmodSync, unlinkSync, readdirSync } from "node:fs"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
import { spawn } from "bun"
|
import { spawn } from "bun"
|
||||||
|
import { extractZip as extractZipBase } from "../../shared"
|
||||||
|
|
||||||
export function findFileRecursive(dir: string, filename: string): string | null {
|
export function findFileRecursive(dir: string, filename: string): string | null {
|
||||||
try {
|
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> {
|
async function extractZip(archivePath: string, destDir: string): Promise<void> {
|
||||||
if (process.platform === "win32") {
|
await extractZipBase(archivePath, destDir)
|
||||||
await extractZipWindows(archivePath, destDir)
|
|
||||||
} else {
|
const binaryName = process.platform === "win32" ? "rg.exe" : "rg"
|
||||||
await extractZipUnix(archivePath, destDir)
|
const foundPath = findFileRecursive(destDir, binaryName)
|
||||||
|
if (foundPath) {
|
||||||
|
const destPath = join(destDir, binaryName)
|
||||||
|
if (foundPath !== destPath) {
|
||||||
|
const { renameSync } = await import("node:fs")
|
||||||
|
renameSync(foundPath, destPath)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user