Skip to content

Commit 1780e29

Browse files
committed
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.
1 parent ded9770 commit 1780e29

File tree

1 file changed

+2
-65
lines changed

1 file changed

+2
-65
lines changed

src/tools/ast-grep/constants.ts

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,20 @@
1-
import { createRequire } from "module"
2-
import { dirname, join } from "path"
3-
import { existsSync } from "fs"
1+
import { existsSync, statSync } from "fs"
42
import { getCachedBinaryPath } from "./downloader"
53

6-
type Platform = "darwin" | "linux" | "win32" | "unsupported"
7-
8-
function getPlatformPackageName(): string | null {
9-
const platform = process.platform as Platform
10-
const arch = process.arch
11-
12-
const platformMap: Record<string, string> = {
13-
"darwin-arm64": "@ast-grep/cli-darwin-arm64",
14-
"darwin-x64": "@ast-grep/cli-darwin-x64",
15-
"linux-arm64": "@ast-grep/cli-linux-arm64-gnu",
16-
"linux-x64": "@ast-grep/cli-linux-x64-gnu",
17-
"win32-x64": "@ast-grep/cli-win32-x64-msvc",
18-
"win32-arm64": "@ast-grep/cli-win32-arm64-msvc",
19-
"win32-ia32": "@ast-grep/cli-win32-ia32-msvc",
20-
}
21-
22-
return platformMap[`${platform}-${arch}`] ?? null
23-
}
24-
254
function isValidBinary(filePath: string): boolean {
265
try {
27-
const stats = require("fs").statSync(filePath)
28-
return stats.size > 10000
6+
return statSync(filePath).size > 10000
297
} catch {
308
return false
319
}
3210
}
3311

3412
export function findSgCliPathSync(): string | null {
35-
const binaryName = process.platform === "win32" ? "sg.exe" : "sg"
36-
37-
if (process.platform === "darwin") {
38-
const homebrewPaths = ["/opt/homebrew/bin/sg", "/usr/local/bin/sg"]
39-
for (const path of homebrewPaths) {
40-
if (existsSync(path) && isValidBinary(path)) {
41-
return path
42-
}
43-
}
44-
}
45-
4613
const cachedPath = getCachedBinaryPath()
4714
if (cachedPath && isValidBinary(cachedPath)) {
4815
return cachedPath
4916
}
5017

51-
try {
52-
const require = createRequire(import.meta.url)
53-
const cliPkgPath = require.resolve("@ast-grep/cli/package.json")
54-
const cliDir = dirname(cliPkgPath)
55-
const sgPath = join(cliDir, binaryName)
56-
57-
if (existsSync(sgPath) && isValidBinary(sgPath)) {
58-
return sgPath
59-
}
60-
} catch {
61-
// @ast-grep/cli not installed
62-
}
63-
64-
const platformPkg = getPlatformPackageName()
65-
if (platformPkg) {
66-
try {
67-
const require = createRequire(import.meta.url)
68-
const pkgPath = require.resolve(`${platformPkg}/package.json`)
69-
const pkgDir = dirname(pkgPath)
70-
const astGrepName = process.platform === "win32" ? "ast-grep.exe" : "ast-grep"
71-
const binaryPath = join(pkgDir, astGrepName)
72-
73-
if (existsSync(binaryPath) && isValidBinary(binaryPath)) {
74-
return binaryPath
75-
}
76-
} catch {
77-
// Platform-specific package not installed
78-
}
79-
}
80-
8118
return null
8219
}
8320

0 commit comments

Comments
 (0)