Skip to content

Commit f8ccecc

Browse files
feat: add setup package
1 parent 5ff4088 commit f8ccecc

File tree

5 files changed

+84
-28
lines changed

5 files changed

+84
-28
lines changed

packages/toml/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"keywords": [],
1919
"author": "",
2020
"license": "ISC",
21+
"dependencies": {
22+
"@ast-grep/setup-lang": "workspace:*"
23+
},
2124
"peerDependencies": {
2225
"tree-sitter-cli": "0.24.6"
2326
},

packages/toml/postinstall.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,2 @@
1-
const dir = __dirname
2-
3-
const { execSync } = require('child_process')
4-
const fs = require('fs')
5-
const path = require('path')
6-
7-
async function postinstall() {
8-
const parser = path.join(dir, 'parser.so')
9-
if (fs.existsSync(parser)) {
10-
return
11-
}
12-
// resolve parser path
13-
const prebuild = resolvePrebuild()
14-
if (prebuild) {
15-
fs.copyFileSync(prebuild, parser)
16-
} else {
17-
// build parser
18-
execSync('npm run build')
19-
}
20-
}
21-
22-
function resolvePrebuild() {
23-
const os = process.platform
24-
const arch = process.arch
25-
throw new Error(`TODO: ${os} ${arch}`)
26-
}
27-
28-
postinstall()
1+
const { postinstall } = require('@ast-grep/setup-lang')
2+
postinstall(__dirname)

pnpm-lock.yaml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/setup/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { execSync } = require('child_process')
2+
const fs = require('fs')
3+
const path = require('path')
4+
5+
/**
6+
* Log to console
7+
* @param {...string} args
8+
*/
9+
function log(...args) {
10+
console.debug(`@ast-grep/lang:`, ...args)
11+
}
12+
13+
/**
14+
* Move prebuild or build parser
15+
* @param {string} dir
16+
*/
17+
async function postinstall(dir) {
18+
const parser = path.join(dir, 'parser.so')
19+
if (fs.existsSync(parser)) {
20+
log('parser already exists, skipping build')
21+
return
22+
}
23+
const prebuild = resolvePrebuild()
24+
if (prebuild) {
25+
log('copying prebuild parser')
26+
fs.copyFileSync(prebuild, parser)
27+
return
28+
}
29+
log('building parser')
30+
try {
31+
execSync('npm run build')
32+
} catch (e) {
33+
log('build failed, please ensure tree-sitter-cli is installed as peer dependency')
34+
log(e)
35+
}
36+
}
37+
38+
const PLATFORM_MAP = {
39+
darwin: 'macOS',
40+
linux: 'Linux',
41+
win32: 'Windows',
42+
}
43+
44+
const ARCH_MAP = {
45+
x64: 'x64',
46+
arm64: 'ARM64',
47+
}
48+
49+
/**
50+
* Resolve prebuild path
51+
* @param {string} dir
52+
*/
53+
function resolvePrebuild(dir) {
54+
const os = PLATFORM_MAP[process.platform]
55+
const arch = ARCH_MAP[process.arch]
56+
const prebuild = path.join(dir, 'prebuilds', `prebuild-${os}-${arch}`, 'parser.so')
57+
if (!os || !arch || !fs.existsSync(prebuild)) {
58+
log(`no prebuild for ${os} ${arch}`)
59+
return undefined
60+
}
61+
log(`found prebuild for ${os} ${arch}`)
62+
return prebuild
63+
}
64+
65+
exports.postinstall = postinstall

scripts/setup/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@ast-grep/setup-lang",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"keywords": [],
7+
"author": "",
8+
"license": "ISC"
9+
}

0 commit comments

Comments
 (0)