aboutsummaryrefslogtreecommitdiff
path: root/bindings/node/index.js
diff options
context:
space:
mode:
authorRuben Beltran del Rio <jj@r.bdr.sh>2025-12-14 19:59:18 +0100
committerRuben Beltran del Rio <jj@r.bdr.sh>2025-12-14 19:59:33 +0100
commit9b7bda28db0200b628e5f6eb7021a5a717db2e73 (patch)
tree2f477b0fe308da3554fbd0ab807c4c4725715311 /bindings/node/index.js
Initial grammar
Diffstat (limited to 'bindings/node/index.js')
-rw-r--r--bindings/node/index.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/bindings/node/index.js b/bindings/node/index.js
new file mode 100644
index 0000000..efa6bea
--- /dev/null
+++ b/bindings/node/index.js
@@ -0,0 +1,37 @@
+import { readFileSync } from "node:fs";
+import { fileURLToPath } from "node:url";
+
+const root = fileURLToPath(new URL("../..", import.meta.url));
+
+const binding = typeof process.versions.bun === "string"
+ // Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
+ ? await import(`${root}/prebuilds/${process.platform}-${process.arch}/tree-sitter-wmap.node`)
+ : (await import("node-gyp-build")).default(root);
+
+try {
+ const nodeTypes = await import(`${root}/src/node-types.json`, { with: { type: "json" } });
+ binding.nodeTypeInfo = nodeTypes.default;
+} catch { }
+
+const queries = [
+ ["HIGHLIGHTS_QUERY", `${root}/queries/highlights.scm`],
+ ["INJECTIONS_QUERY", `${root}/queries/injections.scm`],
+ ["LOCALS_QUERY", `${root}/queries/locals.scm`],
+ ["TAGS_QUERY", `${root}/queries/tags.scm`],
+];
+
+for (const [prop, path] of queries) {
+ Object.defineProperty(binding, prop, {
+ configurable: true,
+ enumerable: true,
+ get() {
+ delete binding[prop];
+ try {
+ binding[prop] = readFileSync(path, "utf8");
+ } catch { }
+ return binding[prop];
+ }
+ });
+}
+
+export default binding;