forked from EvgeniyPeshkov/syntax-highlighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
50 lines (45 loc) · 1.62 KB
/
build.js
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
#!/usr/bin/env node
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
// Languages
let langs = [];
fs.readdirSync(__dirname + "/../grammars/").forEach(name => {
langs.push(path.basename(name, ".json"));
});
// Language-package map
const langMap = {
typescript: ["typescript", "typescript"],
typescriptreact: ["typescript", "tsx"],
ocaml: ["ocaml", "ocaml"],
shellscript: ["bash"]
}
// Build wasm parsers for supported languages
const parsersDir = __dirname + "/../parsers";
if (!fs.existsSync(parsersDir)) {
fs.mkdirSync(parsersDir);
}
for (li of langs) {
const lang = li;
let module = "node_modules/tree-sitter-" + lang;
let output = "tree-sitter-" + lang + ".wasm";
if (langMap[lang]) {
module = path.join("node_modules/tree-sitter-" +
langMap[lang][0], ...langMap[lang].slice(1))
output = "tree-sitter-" + langMap[lang][langMap[lang].length - 1] + ".wasm";
}
console.log("Compiling " + lang + " parser");
exec("node_modules/.bin/tree-sitter build-wasm " + module,
(err) => {
if (err)
console.log("Failed to build wasm for " + lang + ": " + err.message);
else
fs.rename(output, "parsers/" + lang + ".wasm",
(err) => {
if (err)
console.log("Failed to copy built parser: " + err.message);
else
console.log("Successfully compiled " + lang + " parser");
});
});
}