Skip to content

Commit 2cf03e2

Browse files
macabeusHerringtonDarkholme
authored andcommitted
apply pr suggestions
1 parent ad99863 commit 2cf03e2

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

packages/c/index.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
const path = require('node:path')
2+
const fs = require('node:fs')
3+
const { resolvePrebuild } = require('@ast-grep/setup-lang')
24

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+
}
2010

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;
2214
}
2315

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.');
2517
}
2618

27-
const libPath = path.join(__dirname, getNativePath())
19+
let libPath;
2820

2921
module.exports = {
30-
libraryPath: libPath,
22+
get libraryPath() {
23+
if (!libPath) {
24+
libPath = getLibPath();
25+
}
26+
return libPath;
27+
},
3128
extensions: ['c', 'h'],
3229
languageSymbol: 'tree_sitter_c',
3330
expandoChar: '_',

scripts/setup/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ interface SetupConfig {
1919
*/
2020
function postinstall(config: SetupConfig) {
2121
const dir = config.dirname
22-
const parser = path.join(dir, 'parser.so')
23-
if (fs.existsSync(parser)) {
24-
log('parser already exists, skipping build')
25-
return
26-
}
2722
const prebuild = resolvePrebuild(dir)
2823
if (prebuild) {
29-
log('copying prebuild parser')
30-
fs.copyFileSync(prebuild, parser)
24+
log('prebuild found, do not need to build')
3125
return
3226
}
3327
try {
@@ -84,4 +78,4 @@ function resolvePrebuild(dir: string) {
8478
return prebuild
8579
}
8680

87-
export { postinstall }
81+
export { postinstall, resolvePrebuild }

0 commit comments

Comments
 (0)