summaryrefslogtreecommitdiffhomepage
path: root/nix/scripts/patch-wasm.ts
blob: 88a06c2bd2bde8fa070bc0165a6e421824dd9903 (plain)
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
#!/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/...")
const nixStorePrefix = process.env.NIX_STORE || "/nix/store"
next = next.replace(/(\.\/)+/g, "./")
next = next.replace(
  new RegExp(`(\\.\\.\\/)+\\/{1,2}(${nixStorePrefix.replace(/^\//, "").replace(/\//g, "\\/")}[^"']+)`, "g"),
  "/$2",
)
next = next.replace(new RegExp(`(["'])\\/{2,}(\\/${nixStorePrefix.replace(/\//g, "\\/")}[^"']+)(["'])`, "g"), "$1$2$3")
next = next.replace(new RegExp(`(["'])\\/\\/(${nixStorePrefix.replace(/\//g, "\\/")}[^"']+)(["'])`, "g"), "$1$2$3")

if (next !== content) fs.writeFileSync(file, next)