Skip to content

Commit e91f3bd

Browse files
committed
feat(build): add build script for WASM parsers and GitHub Actions workflow
1 parent 67ae3c4 commit e91f3bd

24 files changed

+2190
-33
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: build-and-publish
2+
on: workflow_dispatch
3+
jobs:
4+
build-and-publish:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v3
8+
9+
- uses: pnpm/action-setup@v2
10+
- name: Install Node.js with pnpm
11+
uses: actions/setup-node@v4
12+
with:
13+
node-version: 20
14+
cache: "pnpm"
15+
- run: pnpm install
16+
17+
- uses: mymindstorm/setup-emsdk@v11
18+
with:
19+
version: 2.0.24
20+
21+
- run: pnpm build
22+
23+
- uses: JS-DevTools/npm-publish@v1
24+
with:
25+
token: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/node_modules
22
.uuid
33
.idea
4-
pnpm-lock.yaml
5-
./*.wasm
4+
./*.wasm

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ pnpm build
3535
pnpm i
3636
pnpm build
3737
```
38+
39+
## LISENSE
40+
41+
Build script based on: [tree-sitter-wasms](https://github.com/Gregoor/tree-sitter-wasms) with MIT LICENSE.
42+
43+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

build.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// based on https://github.com/Gregoor/tree-sitter-wasms with MIT license
2+
import fs from "fs";
3+
import os from "os";
4+
import path from "path";
5+
import util from "util";
6+
7+
import { PromisePool } from "@supercharge/promise-pool";
8+
const findRoot = require("find-root");
9+
10+
import packageInfo from "./package.json";
11+
12+
const langArg = process.argv[2];
13+
14+
const exec = util.promisify(require("child_process").exec);
15+
16+
const outDir = path.join(__dirname, "out");
17+
18+
let hasErrors = false;
19+
20+
async function buildParserWASM(
21+
name: string,
22+
{ subPath, generate }: { subPath?: string; generate?: boolean } = {}
23+
) {
24+
const label = subPath ? path.join(name, subPath) : name;
25+
try {
26+
console.log(`⏳ Building ${label}`);
27+
let packagePath;
28+
try {
29+
packagePath = findRoot(require.resolve(name));
30+
} catch (_) {
31+
packagePath = path.join(__dirname, "node_modules", name);
32+
}
33+
const cwd = subPath ? path.join(packagePath, subPath) : packagePath;
34+
if (generate) {
35+
await exec(`pnpm tree-sitter generate`, { cwd });
36+
}
37+
await exec(`pnpm tree-sitter build-wasm ${cwd}`);
38+
console.log(`✅ Finished building ${label}`);
39+
} catch (e) {
40+
console.error(`🔥 Failed to build ${label}:\n`, e);
41+
hasErrors = true;
42+
}
43+
}
44+
45+
if (fs.existsSync(outDir)) {
46+
fs.rmSync(outDir, { recursive: true, force: true });
47+
}
48+
49+
fs.mkdirSync(outDir);
50+
51+
process.chdir(outDir);
52+
53+
const grammars = Object.keys(packageInfo.devDependencies)
54+
.filter((n) => n.startsWith("tree-sitter-") && n !== "tree-sitter-cli")
55+
.concat('@tree-sitter-grammars/tree-sitter-zig')
56+
.concat("@tlaplus/tree-sitter-tlaplus")
57+
.filter((s) => !langArg || s.includes(langArg));
58+
59+
PromisePool.withConcurrency(os.cpus().length)
60+
.for(grammars)
61+
.process(async (name) => {
62+
if (name == "tree-sitter-rescript") {
63+
await buildParserWASM(name, { generate: true });
64+
} else if (name == "tree-sitter-ocaml") {
65+
await buildParserWASM(name, { subPath: "ocaml" });
66+
} else if (name == "tree-sitter-php") {
67+
await buildParserWASM(name, { subPath: "php" });
68+
} else if (name == "tree-sitter-typescript") {
69+
await buildParserWASM(name, { subPath: "typescript" });
70+
await buildParserWASM(name, { subPath: "tsx" });
71+
} else {
72+
await buildParserWASM(name);
73+
}
74+
})
75+
.then(async () => {
76+
if (hasErrors) {
77+
process.exit(1);
78+
}
79+
await exec(`mv *.wasm ${outDir}`, { cwd: __dirname });
80+
});

package.json

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
{
22
"name": "@unit-mesh/treesitter-artifacts",
33
"author": "Unit Mesh authors",
4-
"type": "module",
5-
"exports": {
6-
".": {
7-
"import": "./main/treesitter.js",
8-
"require": "./main/treesitter.js"
9-
},
10-
"./wasm/*": "./wasm/*"
11-
},
12-
"main": "./main/treesitter.js",
4+
"files": [
5+
"/out"
6+
],
7+
"main": "bindings/node",
138
"version": "1.6.4",
149
"repository": {
1510
"type": "git",
@@ -25,33 +20,21 @@
2520
],
2621
"license": "MIT",
2722
"scripts": {
28-
"gen-wasm": "tree-sitter --version && run-p __gen-* && run-s __mv-wasm",
29-
"__gen-c": "tree-sitter build-wasm node_modules/tree-sitter-c/",
30-
"__gen-cpp": "tree-sitter build-wasm node_modules/tree-sitter-cpp/",
31-
"__gen-c-sharp": "tree-sitter build-wasm node_modules/tree-sitter-c-sharp/",
32-
"__gen-go": "tree-sitter build-wasm node_modules/tree-sitter-go/",
33-
"__gen-java": "tree-sitter build-wasm node_modules/tree-sitter-java/",
34-
"__gen-javascript": "tree-sitter build-wasm node_modules/tree-sitter-javascript/",
35-
"__gen-kotlin": "tree-sitter build-wasm node_modules/tree-sitter-kotlin/",
36-
"__gen-lua": "tree-sitter build-wasm node_modules/tree-sitter-lua/",
37-
"__gen-python": "tree-sitter build-wasm node_modules/tree-sitter-python/",
38-
"__gen-rust": "tree-sitter build-wasm node_modules/tree-sitter-rust/",
39-
"__gen-swift": "tree-sitter build-wasm node_modules/tree-sitter-swift/",
40-
"__gen-php": "tree-sitter build-wasm node_modules/tree-sitter-php/",
41-
"__gen-typescript": "tree-sitter build-wasm node_modules/tree-sitter-typescript/typescript/",
42-
"__gen-typescript-jsx": "tree-sitter build-wasm node_modules/tree-sitter-typescript/tsx/",
43-
"__gen-zig": "tree-sitter build-wasm node_modules/tree-sitter-zig/",
44-
"__gen-toml": "tree-sitter build-wasm node_modules/tree-sitter-toml/",
45-
"__gen-markdown": "tree-sitter build-wasm node_modules/@tree-sitter-grammars/tree-sitter-markdown/tree-sitter-markdown",
46-
"__mv-wasm": "rm -rf ./wasm && mkdir wasm && mv *.wasm wasm/",
47-
"download_treesitter": "node index.js",
48-
"build": "run-p gen-wasm download_treesitter"
23+
"build": "ts-node build.ts"
4924
},
5025
"devDependencies": {
51-
"@tree-sitter-grammars/tree-sitter-markdown": "0.2.3",
26+
"ts-node": "^10.9.2",
27+
"typescript": "5.3.3",
5228
"@types/node": "^18.16.0",
5329
"node-fetch": "^3.3.1",
5430
"npm-run-all": "^4.1.5",
31+
"@supercharge/promise-pool": "^3.1.1",
32+
"find-root": "^1.1.0"
33+
},
34+
"dependencies": {
35+
"@tree-sitter-grammars/tree-sitter-markdown": "0.2.3",
36+
"@tree-sitter-grammars/tree-sitter-zig": "1.0.0",
37+
"@tlaplus/tree-sitter-tlaplus": "^1.2.4",
5538
"tree-sitter-c": "0.20.6",
5639
"tree-sitter-c-sharp": "0.20.0",
5740
"tree-sitter-cli": "^0.20.8",
@@ -66,6 +49,8 @@
6649
"tree-sitter-swift": "^0.3.6",
6750
"tree-sitter-toml": "0.5.1",
6851
"tree-sitter-php": "^0.22.0",
52+
"tree-sitter-ocaml": "^0.20.4",
53+
"tree-sitter-rescript": "github:rescript-lang/tree-sitter-rescript#6376fa028f31aa4e26ca2c8f007e322cd2a5eb4a",
6954
"tree-sitter-bash": "^0.20.5",
7055
"tree-sitter-typescript": "git://github.com/tree-sitter/tree-sitter-typescript#v0.20.4",
7156
"tree-sitter-zig": "git://github.com/GrayJack/tree-sitter-zig#8e970cb"

0 commit comments

Comments
 (0)