summaryrefslogtreecommitdiffhomepage
path: root/nix/scripts
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
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')
-rw-r--r--nix/scripts/canonicalize-node-modules.ts31
-rw-r--r--nix/scripts/patch-wasm.ts39
2 files changed, 63 insertions, 7 deletions
diff --git a/nix/scripts/canonicalize-node-modules.ts b/nix/scripts/canonicalize-node-modules.ts
index bb004f3c5..828a18fbc 100644
--- a/nix/scripts/canonicalize-node-modules.ts
+++ b/nix/scripts/canonicalize-node-modules.ts
@@ -24,15 +24,13 @@ for (const entry of directories) {
if (!info.isDirectory()) {
continue
}
- const marker = entry.lastIndexOf("@")
- if (marker <= 0) {
+ const parsed = parseEntry(entry)
+ if (!parsed) {
continue
}
- const slug = entry.slice(0, marker).replace(/\+/g, "/")
- const version = entry.slice(marker + 1)
- const list = versions.get(slug) ?? []
- list.push({ dir: full, version, label: entry })
- versions.set(slug, list)
+ const list = versions.get(parsed.name) ?? []
+ list.push({ dir: full, version: parsed.version, label: entry })
+ versions.set(parsed.name, list)
}
const semverModule = (await import(join(bunRoot, "node_modules/semver"))) as
@@ -79,6 +77,12 @@ for (const [slug, entry] of Array.from(selections.entries()).sort((a, b) => a[0]
await mkdir(parent, { recursive: true })
const linkPath = join(parent, leaf)
const desired = join(entry.dir, "node_modules", slug)
+ const exists = await lstat(desired)
+ .then(info => info.isDirectory())
+ .catch(() => false)
+ if (!exists) {
+ continue
+ }
const relativeTarget = relative(parent, desired)
const resolved = relativeTarget.length === 0 ? "." : relativeTarget
await rm(linkPath, { recursive: true, force: true })
@@ -94,3 +98,16 @@ for (const line of rewrites.slice(0, 20)) {
if (rewrites.length > 20) {
console.log(" ...")
}
+
+function parseEntry(label: string) {
+ const marker = label.startsWith("@") ? label.indexOf("@", 1) : label.indexOf("@")
+ if (marker <= 0) {
+ return null
+ }
+ const name = label.slice(0, marker).replace(/\+/g, "/")
+ const version = label.slice(marker + 1)
+ if (!name || !version) {
+ return null
+ }
+ return { name, version }
+}
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)