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