-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.ts
More file actions
186 lines (157 loc) · 5.18 KB
/
build.ts
File metadata and controls
186 lines (157 loc) · 5.18 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { exec as execSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { exit } from "node:process";
import { fileURLToPath } from "node:url";
import util from "node:util";
import { PromisePool } from "@supercharge/promise-pool";
import findRoot from "find-root";
import packageInfo from "./package.json" with { type: "json" };
class ParserError extends Error {
constructor(
public message: string,
public value: unknown,
) {
super(message);
this.name = "ParserError";
}
}
const __dirname = import.meta.dirname;
const dependencies = packageInfo.devDependencies;
type ParserName = keyof typeof dependencies;
const exec = util.promisify(execSync);
const outDir = path.join(__dirname, "out");
function getPackagePath(name: string) {
try {
return findRoot(fileURLToPath(import.meta.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 = /^github:(\S+)#(\S+)$/.exec(value);
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 (error) {
throw new ParserError(`❗Failed to clone git repo for ${name}`, error);
}
}
async function buildParserWASM(
name: ParserName,
{ subPath, generate }: { subPath?: string; generate?: boolean } = {},
) {
const label = subPath != null ? 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 != null ? 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 (error) {
throw new ParserError(`❗ Failed to generate ${label}`, error);
}
}
try {
await exec(buildCommand, { cwd });
await exec(`mv *.wasm ${outDir}`, { cwd });
console.log(`✅ Finished building ${label}`);
} catch (error) {
throw new ParserError(`❗ Failed to build ${label}`, error);
}
}
async function processParser(name: ParserName) {
// oxlint-disable-next-line typescript/switch-exhaustiveness-check
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() {
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
const grammars = Object.keys(dependencies).filter(
(n) =>
(n.startsWith("tree-sitter-") && n !== "tree-sitter-cli") ||
n === "@elm-tooling/tree-sitter-elm",
) as ParserName[];
if (grammars.length === 0) {
throw new Error("❗ No parsers found in dependencies");
}
let hasErrors = false;
const [initialGrammar, ...remainingGrammars] = grammars;
const processGrammar = async (name: ParserName) => {
try {
await processParser(name);
} catch (error) {
if (error instanceof ParserError) {
console.error(`${error.message}:\n`, error.value);
} else {
console.error(error);
}
hasErrors = true;
}
};
// [email protected] downloads/extracts wasi-sdk into a shared cache path.
// Warm that cache once before the parallel builds to avoid extraction races.
await processGrammar(initialGrammar);
await PromisePool.withConcurrency(os.cpus().length)
.for(remainingGrammars)
.process(processGrammar);
// oxlint-disable-next-line typescript/no-unnecessary-condition
if (hasErrors) {
throw new Error("❗Failed to build some parsers");
}
}
fs.mkdirSync(outDir);
process.chdir(outDir);
try {
await run();
} catch (error) {
console.error(error);
exit(1);
}