|
1 | | -import { createRequire } from "module" |
2 | | -import { dirname, join } from "path" |
3 | | -import { existsSync } from "fs" |
| 1 | +import { existsSync, statSync } from "fs" |
4 | 2 | import { getCachedBinaryPath } from "./downloader" |
5 | 3 |
|
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 | | - |
25 | 4 | function isValidBinary(filePath: string): boolean { |
26 | 5 | try { |
27 | | - const stats = require("fs").statSync(filePath) |
28 | | - return stats.size > 10000 |
| 6 | + return statSync(filePath).size > 10000 |
29 | 7 | } catch { |
30 | 8 | return false |
31 | 9 | } |
32 | 10 | } |
33 | 11 |
|
34 | 12 | 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 | | - |
46 | 13 | const cachedPath = getCachedBinaryPath() |
47 | 14 | if (cachedPath && isValidBinary(cachedPath)) { |
48 | 15 | return cachedPath |
49 | 16 | } |
50 | 17 |
|
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 | | - |
81 | 18 | return null |
82 | 19 | } |
83 | 20 |
|
|
0 commit comments