Files
site/tools/split-post.py
T

27 lines
2.3 KiB
Python

# tools/split-post.py: the hand edits after split.js wrote a step. Idempotent.
import re, pathlib, sys
ROOT = pathlib.Path(__file__).resolve().parent.parent
files = [p for p in list(ROOT.glob("*.js")) + list(ROOT.glob("ui/*.js")) if p.name not in ("brain.js", "sw.js")] + [ROOT / "index.html"]
for p in files:
s = t = p.read_text()
t = t.replace("export let seq = 0;", "export const ids = { seq: 0 }; // job ids; brain.js reaches it as window.seq")
t = t.replace("let seq = 0;", "const ids = { seq: 0 }; // job ids; brain.js reaches it as window.seq")
t = re.sub(r"\+\+seq\b", "++ids.seq", t)
t = re.sub(r"^(import \{[^}]*?)\bseq\b", r"\1ids", t, flags=re.M)
if "async function venice(path" in t:
t = t.replace("export async function venice(path, body, { raw = false } = {}) {", "async function veniceFetch(path, body, { raw = false } = {}) {", 1)
t = t.replace("async function venice(path, body, { raw = false } = {}) {", "async function veniceFetch(path, body, { raw = false } = {}) {", 1)
j = t.index("\n}\n", t.index("async function veniceFetch")) + 3
t = t[:j] + "/* the fetcher is injectable: the harness fragments stub it (render.setFetcher); every call goes through venice() */\nlet fetcher = veniceFetch;\nexport const setFetcher = (fn) => { fetcher = fn || veniceFetch; };\nexport const venice = (...a) => fetcher(...a);\n" + t[j:]
t = t.replace("\nasync function makeTone(", "\nexport async function makeTone(").replace("\nasync function makeNoise(", "\nexport async function makeNoise(")
t = re.sub(r'import \{ crackle \} from "\./(player\.js|index\.html)";\n', "", t) # a regex literal in noiseSpec mentions crackle; not a use
if t != s: p.write_text(t)
h = ROOT / "index.html"; s = h.read_text()
s = s.replace('<script src="brain.js"></script>', '<script src="brain.js" defer></script>') # after the module, which is deferred too
h.write_text(s)
sw = ROOT / "sw.js"; s = sw.read_text()
mods = sorted(str(p.relative_to(ROOT)) for p in list(ROOT.glob("*.js")) + list(ROOT.glob("ui/*.js")) if p.name not in ("brain.js", "sw.js"))
s = re.sub(r'const FILES = \["/", "/index.html",( "/[a-z/]+\.js",)*', 'const FILES = ["/", "/index.html",' + "".join(f' "/{m}",' for m in mods), s)
sw.write_text(s)
print("post ok:", mods)