-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ts
159 lines (133 loc) · 4.21 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { PromisePool } from "@supercharge/promise-pool";
import findRoot from "find-root";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import util from "node:util";
import packageInfo from "./package.json";
class ParserError {
constructor(public message: string, public value: any) {}
}
const dependencies = packageInfo.devDependencies;
type ParserName = keyof typeof dependencies;
const exec = util.promisify(require("child_process").exec);
const outDir = path.join(__dirname, "out");
function getPackagePath(name: string) {
try {
return findRoot(require.resolve(name));
} catch (_) {
return path.join(__dirname, "node_modules", name);
}
}
async function gitCloneOverload(name: ParserName) {
const packagePath = getPackagePath(name);
const value = dependencies[name];
const match = value.match(/^github:(\S+)#(\S+)$/);
if (match == null) {
throw new ParserError(`❗ Failed to parse git repo for ${name}`, value);
}
try {
const repoUrl = `https://github.com/${match[1]}.git`;
const commitHash = match[2];
console.log(`🗑️ Deleting cached node dependency for ${name}`);
await exec(`rm -rf ${packagePath}`);
console.log(`⬇️ Cloning ${name} from git`);
await exec(`git clone ${repoUrl} ${packagePath}`);
process.chdir(packagePath);
await exec(`git reset --hard ${commitHash}`);
} catch (e) {
throw new ParserError(`❗Failed to clone git repo for ${name}`, e);
}
}
async function buildParserWASM(
name: ParserName,
{ subPath, generate }: { subPath?: string; generate?: boolean } = {}
) {
const label = subPath ? path.join(name, subPath) : name;
const cliPackagePath = getPackagePath("tree-sitter-cli");
const packagePath = getPackagePath(name);
const cliPath = path.join(cliPackagePath, "tree-sitter");
const generateCommand = cliPath.concat(" generate");
const buildCommand = cliPath.concat(" build --wasm");
console.log(`⏳ Building ${label}`);
const cwd = subPath ? path.join(packagePath, subPath) : packagePath;
if (!fs.existsSync(cwd)) {
throw new ParserError(`❗ Failed to find cwd ${label}`, cwd);
}
if (generate) {
try {
await exec(generateCommand, { cwd });
} catch (e) {
throw new ParserError(`❗ Failed to generate ${label}`, e);
}
}
try {
await exec(buildCommand, { cwd });
await exec(`mv *.wasm ${outDir}`, { cwd });
console.log(`✅ Finished building ${label}`);
} catch (e) {
throw new ParserError(`❗ Failed to build ${label}`, e);
}
}
async function processParser(name: ParserName) {
switch (name) {
case "tree-sitter-php":
await buildParserWASM(name, { subPath: "php" });
break;
case "tree-sitter-typescript":
await buildParserWASM(name, { subPath: "typescript" });
await buildParserWASM(name, { subPath: "tsx" });
break;
case "tree-sitter-xml":
await buildParserWASM(name, { subPath: "xml" });
await buildParserWASM(name, { subPath: "dtd" });
break;
case "tree-sitter-markdown":
await gitCloneOverload(name);
await buildParserWASM(name, {
subPath: "tree-sitter-markdown",
});
await buildParserWASM(name, {
subPath: "tree-sitter-markdown-inline",
});
break;
case "tree-sitter-elixir":
case "tree-sitter-perl":
case "tree-sitter-query":
await gitCloneOverload(name);
await buildParserWASM(name, { generate: true });
break;
default:
await buildParserWASM(name);
}
}
async function run() {
const grammars = Object.keys(dependencies).filter(
(n) =>
(n.startsWith("tree-sitter-") && n !== "tree-sitter-cli") ||
n === "@elm-tooling/tree-sitter-elm"
) as ParserName[];
let hasErrors = false;
await PromisePool.withConcurrency(os.cpus().length)
.for(grammars)
.process(async (name) => {
try {
await processParser(name);
} catch (e) {
if (e instanceof ParserError) {
console.error(e.message + ":\n", e.value);
} else {
console.error(e);
}
hasErrors = true;
}
});
if (hasErrors) {
throw new Error();
}
}
fs.mkdirSync(outDir);
process.chdir(outDir);
run().catch(() => {
process.exit(1);
});