|
1 | 1 | const path = require('node:path')
|
| 2 | +const fs = require('node:fs') |
| 3 | +const { resolvePrebuild } = require('@ast-grep/setup-lang') |
2 | 4 |
|
3 |
| -function getNativePath() { |
4 |
| - if (process.platform === 'win32') { |
5 |
| - if (process.arch === 'x64') { |
6 |
| - return 'prebuilds/prebuild-Windows-X64/parser.so' |
7 |
| - } |
8 |
| - |
9 |
| - throw new Error(`Unsupported architecture on Windows: ${process.arch}`) |
10 |
| - } else if (process.platform === 'darwin') { |
11 |
| - if (process.arch === 'arm64') { |
12 |
| - return 'prebuilds/prebuild-macOS-ARM64/parser.so'; |
13 |
| - } |
14 |
| - |
15 |
| - throw new Error(`Unsupported architecture on macOS: ${process.arch}`) |
16 |
| - } else if (process.platform === 'linux') { |
17 |
| - if (process.arch === 'x64') { |
18 |
| - return 'prebuilds/prebuild-Linux-X64/parser.so'; |
19 |
| - } |
| 5 | +function getLibPath() { |
| 6 | + const prebuild = resolvePrebuild(__dirname) |
| 7 | + if (prebuild) { |
| 8 | + return prebuild; |
| 9 | + } |
20 | 10 |
|
21 |
| - throw new Error(`Unsupported architecture on Linux: ${process.arch}`) |
| 11 | + const native = path.join(__dirname, 'parser.so'); |
| 12 | + if (fs.existsSync(native)) { |
| 13 | + return native; |
22 | 14 | }
|
23 | 15 |
|
24 |
| - throw new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`) |
| 16 | + throw new Error('No parser found. Please ensure the parser is built or a prebuild is available.'); |
25 | 17 | }
|
26 | 18 |
|
27 |
| -const libPath = path.join(__dirname, getNativePath()) |
| 19 | +let libPath; |
28 | 20 |
|
29 | 21 | module.exports = {
|
30 |
| - libraryPath: libPath, |
| 22 | + get libraryPath() { |
| 23 | + if (!libPath) { |
| 24 | + libPath = getLibPath(); |
| 25 | + } |
| 26 | + return libPath; |
| 27 | + }, |
31 | 28 | extensions: ['c', 'h'],
|
32 | 29 | languageSymbol: 'tree_sitter_c',
|
33 | 30 | expandoChar: '_',
|
|
0 commit comments