interplane.Get Omega
Read in your language

tree-sitter-omega

aien-dev/tree-sitter-omega

Brought by drakestapleton

branch main Branches Tags History

tree-sitter-omega / bindings / node / index.js

56 lines · 2173 bytes Raw Blame History
1import { readFileSync } from "node:fs";
2import { fileURLToPath } from "node:url";
3
4const root = fileURLToPath(new URL("../..", import.meta.url));
5
6// A `bun build --compile` executable has no package directory for node-gyp-build to search, so
7// require the prebuild directly. Its path has to stay static for Bun to embed the addon.
8const binding = typeof process.versions.bun === "string" && Bun.isStandaloneExecutable
9 ? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-omega.node`)
10 : (await import("node-gyp-build")).default(root);
11
12try {
13 // Bun only embeds static paths, and `import()` rejects absolute Windows paths like `root`.
14 const nodeTypes = await import("../../src/node-types.json", { with: { type: "json" } });
15 binding.nodeTypeInfo = nodeTypes.default;
16} catch { }
17
18if (typeof process.versions.bun === "string" && Bun.isStandaloneExecutable) {
19 // Static paths so Bun embeds the queries. Grammars often lack some of them, and inside a `try` a
20 // missing file is skipped instead of failing the build.
21 try {
22 binding.HIGHLIGHTS_QUERY = (await import("../../queries/highlights.scm", { with: { type: "text" } })).default;
23 } catch { }
24 try {
25 binding.INJECTIONS_QUERY = (await import("../../queries/injections.scm", { with: { type: "text" } })).default;
26 } catch { }
27 try {
28 binding.LOCALS_QUERY = (await import("../../queries/locals.scm", { with: { type: "text" } })).default;
29 } catch { }
30 try {
31 binding.TAGS_QUERY = (await import("../../queries/tags.scm", { with: { type: "text" } })).default;
32 } catch { }
33} else {
34 const queries = [
35 ["HIGHLIGHTS_QUERY", `${root}/queries/highlights.scm`],
36 ["INJECTIONS_QUERY", `${root}/queries/injections.scm`],
37 ["LOCALS_QUERY", `${root}/queries/locals.scm`],
38 ["TAGS_QUERY", `${root}/queries/tags.scm`],
39 ];
40
41 for (const [prop, path] of queries) {
42 Object.defineProperty(binding, prop, {
43 configurable: true,
44 enumerable: true,
45 get() {
46 delete binding[prop];
47 try {
48 binding[prop] = readFileSync(path, "utf8");
49 } catch { }
50 return binding[prop];
51 }
52 });
53 }
54}
55
56export default binding;