nomadro/scripts/fix-community-login-next.py
eric 28998243c3 Add community root-path middleware and fix login next= targets.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 19:07:54 -05:00

45 lines
1.2 KiB
Python

#!/usr/bin/env python3
from __future__ import annotations
import pathlib
import re
ROOT = pathlib.Path(__file__).resolve().parents[1] / "apps/nomadweb/frontend/src"
def fix(text: str) -> str:
t = text
t = re.sub(r"(?<!/community)/login\?", "/community/login?", t)
t = re.sub(r'(?<!/community)/login"', '/community/login"', t)
t = re.sub(r"(?<!/community)/login'", "/community/login'", t)
def repl_next(m: re.Match[str]) -> str:
path = m.group(1)
if path.startswith(("/community", "/book", "/nav", "http")):
return m.group(0)
if not path.startswith("/"):
path = "/" + path
return f"next=/community{path}"
t = re.sub(r"next=(/[^\s\"'?]+)", repl_next, t)
t = t.replace("/community/community/", "/community/")
return t
def main() -> None:
n = 0
for p in ROOT.rglob("*"):
if p.suffix not in {".ts", ".tsx"}:
continue
raw = p.read_text(encoding="utf-8")
out = fix(raw)
if out != raw:
p.write_text(out, encoding="utf-8")
n += 1
print("fixed", p.relative_to(ROOT))
print("files", n)
if __name__ == "__main__":
main()