"""Build the Chrome package: dist/flagtldr--chrome.zip (+ the same files unpacked in dist/flagtldr--chrome/ for "Load unpacked"). Only runtime files – no tests, fixtures, node_modules or dev tooling. The Chrome manifest drops Firefox-only keys (background.scripts, browser_specific_settings) so chrome://extensions shows no warnings. Usage: python3 scripts/package-chrome.py [--store] --store: store build (Edge Add-ons / Chrome Web Store) – no 127.0.0.1/localhost host permissions (local classifier / progrok stay unusable there); output dist/flagtldr--store.zip.""" import json, shutil, sys, zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] EXT = ROOT / "extension" INCLUDE = ["icons", "src", "vendor"] man = json.loads((EXT / "manifest.json").read_text()) ver = man["version"] man["background"] = {k: v for k, v in man["background"].items() if k != "scripts"} man.pop("browser_specific_settings", None) STORE = "--store" in sys.argv if STORE: man["host_permissions"] = [h for h in man["host_permissions"] if "127.0.0.1" not in h and "localhost" not in h] dist = ROOT / "dist" kind = "store" if STORE else "chrome" out_dir, out_zip = dist / f"flagtldr-{ver}-{kind}", dist / f"flagtldr-{ver}-{kind}.zip" shutil.rmtree(out_dir, ignore_errors=True) out_dir.mkdir(parents=True) files = sorted(p for d in INCLUDE for p in (EXT / d).rglob("*") if p.is_file()) for p in files: dst = out_dir / p.relative_to(EXT) dst.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(p, dst) (out_dir / "manifest.json").write_text(json.dumps(man, ensure_ascii=False, indent=2) + "\n") out_zip.unlink(missing_ok=True) with zipfile.ZipFile(out_zip, "w", zipfile.ZIP_DEFLATED) as z: for p in sorted(out_dir.rglob("*")): if p.is_file(): zi = zipfile.ZipInfo(str(p.relative_to(out_dir)), date_time=(2026, 1, 1, 0, 0, 0)) zi.compress_type = zipfile.ZIP_DEFLATED z.writestr(zi, p.read_bytes()) print(f"{out_zip} ({out_zip.stat().st_size} bytes, {len(files) + 1} files)") print(f"{out_dir}/ (unpacked, for chrome://extensions → Load unpacked)")