summaryrefslogtreecommitdiffhomepage
path: root/nix/scripts/patch-wasm.ts
diff options
context:
space:
mode:
authorAlbert O'Shea <[email protected]>2025-11-24 15:54:29 +1100
committerGitHub <[email protected]>2025-11-23 22:54:29 -0600
commita3a239967f170423e91382bfdaf1058755befbb8 (patch)
tree322961f56a32fe8d164fdebc43c3a326c3fa421e /nix/scripts/patch-wasm.ts
parentb4fd4bb25757250b1bcd4da9e6398011ed07e2f3 (diff)
downloadopencode-a3a239967f170423e91382bfdaf1058755befbb8.tar.gz
opencode-a3a239967f170423e91382bfdaf1058755befbb8.zip
nix: bundle js dist with bun and patch tree-sitter wasm paths (#4644)
Co-authored-by: Aiden Cline <[email protected]> Co-authored-by: Github Action <[email protected]>
Diffstat (limited to 'nix/scripts/patch-wasm.ts')
-rw-r--r--nix/scripts/patch-wasm.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/nix/scripts/patch-wasm.ts b/nix/scripts/patch-wasm.ts
new file mode 100644
index 000000000..99f8a40e9
--- /dev/null
+++ b/nix/scripts/patch-wasm.ts
@@ -0,0 +1,39 @@
+#!/usr/bin/env bun
+
+import fs from "fs"
+import path from "path"
+
+/**
+ * Rewrite tree-sitter wasm references inside a JS file to absolute paths.
+ * argv: [node, script, file, mainWasm, ...wasmPaths]
+ */
+const [, , file, mainWasm, ...wasmPaths] = process.argv
+
+if (!file || !mainWasm) {
+ console.error("usage: patch-wasm <file> <mainWasm> [wasmPaths...]")
+ process.exit(1)
+}
+
+const content = fs.readFileSync(file, "utf8")
+const byName = new Map<string, string>()
+
+for (const wasm of wasmPaths) {
+ const name = path.basename(wasm)
+ byName.set(name, wasm)
+}
+
+let next = content
+
+for (const [name, wasmPath] of byName) {
+ next = next.replaceAll(name, wasmPath)
+}
+
+next = next.replaceAll("tree-sitter.wasm", mainWasm).replaceAll("web-tree-sitter/tree-sitter.wasm", mainWasm)
+
+// Collapse any relative prefixes before absolute store paths (e.g., "../../../..//nix/store/...")
+next = next.replace(/(\.\/)+/g, "./")
+next = next.replace(/(\.\.\/)+\/?(\/nix\/store[^"']+)/g, "/$2")
+next = next.replace(/(["'])\/{2,}(\/nix\/store[^"']+)(["'])/g, "$1/$2$3")
+next = next.replace(/(["'])\/\/(nix\/store[^"']+)(["'])/g, "$1/$2$3")
+
+if (next !== content) fs.writeFileSync(file, next)