From 1780e2971d79d0e4975b7f93d5d5f23376badbc4 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Fri, 5 Dec 2025 22:12:02 +0900 Subject: [PATCH] refactor(ast-grep): simplify binary resolution, rely on auto-download - Remove hardcoded homebrew paths - Remove npm package path resolution (prone to placeholder issues) - Only check cached binary (~/.cache/oh-my-opencode/bin/sg) - If not found, cli.ts will auto-download from GitHub releases The download logic in cli.ts handles all cases properly. --- src/tools/ast-grep/constants.ts | 67 +-------------------------------- 1 file changed, 2 insertions(+), 65 deletions(-) diff --git a/src/tools/ast-grep/constants.ts b/src/tools/ast-grep/constants.ts index e345751..a567347 100644 --- a/src/tools/ast-grep/constants.ts +++ b/src/tools/ast-grep/constants.ts @@ -1,83 +1,20 @@ -import { createRequire } from "module" -import { dirname, join } from "path" -import { existsSync } from "fs" +import { existsSync, statSync } from "fs" import { getCachedBinaryPath } from "./downloader" -type Platform = "darwin" | "linux" | "win32" | "unsupported" - -function getPlatformPackageName(): string | null { - const platform = process.platform as Platform - const arch = process.arch - - const platformMap: Record = { - "darwin-arm64": "@ast-grep/cli-darwin-arm64", - "darwin-x64": "@ast-grep/cli-darwin-x64", - "linux-arm64": "@ast-grep/cli-linux-arm64-gnu", - "linux-x64": "@ast-grep/cli-linux-x64-gnu", - "win32-x64": "@ast-grep/cli-win32-x64-msvc", - "win32-arm64": "@ast-grep/cli-win32-arm64-msvc", - "win32-ia32": "@ast-grep/cli-win32-ia32-msvc", - } - - return platformMap[`${platform}-${arch}`] ?? null -} - function isValidBinary(filePath: string): boolean { try { - const stats = require("fs").statSync(filePath) - return stats.size > 10000 + return statSync(filePath).size > 10000 } catch { return false } } export function findSgCliPathSync(): string | null { - const binaryName = process.platform === "win32" ? "sg.exe" : "sg" - - if (process.platform === "darwin") { - const homebrewPaths = ["/opt/homebrew/bin/sg", "/usr/local/bin/sg"] - for (const path of homebrewPaths) { - if (existsSync(path) && isValidBinary(path)) { - return path - } - } - } - const cachedPath = getCachedBinaryPath() if (cachedPath && isValidBinary(cachedPath)) { return cachedPath } - try { - const require = createRequire(import.meta.url) - const cliPkgPath = require.resolve("@ast-grep/cli/package.json") - const cliDir = dirname(cliPkgPath) - const sgPath = join(cliDir, binaryName) - - if (existsSync(sgPath) && isValidBinary(sgPath)) { - return sgPath - } - } catch { - // @ast-grep/cli not installed - } - - const platformPkg = getPlatformPackageName() - if (platformPkg) { - try { - const require = createRequire(import.meta.url) - const pkgPath = require.resolve(`${platformPkg}/package.json`) - const pkgDir = dirname(pkgPath) - const astGrepName = process.platform === "win32" ? "ast-grep.exe" : "ast-grep" - const binaryPath = join(pkgDir, astGrepName) - - if (existsSync(binaryPath) && isValidBinary(binaryPath)) { - return binaryPath - } - } catch { - // Platform-specific package not installed - } - } - return null }