#!/usr/bin/env python3 """Turn a line number in dist/index.html into the source file and line it came from. python3 tools/where.py 4211 -> src/sim/pipes.js:57 python3 tools/where.py < error.txt -> the text with every index.html:LINE rewritten Browser errors point into the built page. build.py writes dist/bundle-lines.json, which says where each source file starts in it. Rebuild first if the sources changed since the error. """ import json, pathlib, re, sys ROOT = pathlib.Path(__file__).resolve().parent.parent def load(): return json.loads((ROOT / 'dist' / 'bundle-lines.json').read_text())['files'] def where(line, files=None): files = files or load() best = None for start, f in files: if start <= line: best = (start, f) else: break return f'src/{best[1]}:{line - best[0] + 1}' if best else f'index.html:{line} (before the script)' def translate(text, files=None): files = files or load() return re.sub(r'(?:file://)?[^\s()]*index\.html:(\d+)(:\d+)?', lambda m: where(int(m.group(1)), files) + (m.group(2) or ''), text) if __name__ == '__main__': if len(sys.argv) > 1: for a in sys.argv[1:]: print(where(int(a))) else: print(translate(sys.stdin.read()), end='')