Repository navigation
Expand file tree
/
Copy pathbuild_netlify.py
More file actions
115 lines (91 loc) · 3.79 KB
/
Copy pathbuild_netlify.py
File metadata and controls
115 lines (91 loc) · 3.79 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import annotations
import os
import shutil
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SOURCE = ROOT / "frontend"
PAGES = SOURCE / "pages"
DIST = ROOT / "dist"
def reset_dist() -> None:
if DIST.exists():
shutil.rmtree(DIST)
DIST.mkdir(parents=True, exist_ok=True)
def copy_tree(name: str) -> None:
source_dir = SOURCE / name
if source_dir.exists():
shutil.copytree(source_dir, DIST / name)
def rewrite_html(content: str) -> str:
replacements = {
'href="../css/': 'href="/css/',
'src="../js/': 'src="/js/',
'src="../assets/': 'src="/assets/',
'src="../script/': 'src="/script/',
'src="biblioDrift_favicon.png"': 'src="/assets/images/biblioDrift_favicon.png"',
'href="style.css"': 'href="/css/style.css"',
'href="style-responsive.css"': 'href="/css/style-responsive.css"',
'src="config.js"': 'src="/js/config.js"',
'src="footer.js"': 'src="/js/footer.js"',
'src="app.js"': 'src="/js/app.js"',
'src="chat.js"': 'src="/js/chat.js"',
'src="library-3d.js"': 'src="/js/library-3d.js"',
'src="script/header-scroll.js"': 'src="/script/header-scroll.js"',
'src="js/header-scroll.js"': 'src="/script/header-scroll.js"',
'../assets/biblioDrift_favicon.png': '/assets/images/biblioDrift_favicon.png',
'href="../manifest.json"': 'href="/manifest.json"',
'href="/manifest.json"': 'href="/manifest.json"',
}
for pattern, replacement in replacements.items():
content = content.replace(pattern, replacement)
return content
def build_html() -> None:
for html_file in PAGES.glob("*.html"):
target_file = DIST / html_file.name
content = html_file.read_text(encoding="utf-8")
target_file.write_text(rewrite_html(content), encoding="utf-8")
def write_clean_route_redirects() -> None:
redirects = []
# if BACKEND_URL is set in Netlify's environment variables,
# proxy all /api/v1/* requests to the Flask backend.
# without this, Netlify has no rule for /api/v1/ and shows a 404.
backend_url = os.getenv("BACKEND_URL", "").strip().rstrip("/")
if backend_url:
redirects.append(f"/api/v1/* {backend_url}/api/v1/:splat 200")
redirects += [
"/app /app.html 200",
"/chat /chat.html 200",
"/auth /auth.html 200",
"/library /library.html 200",
"/vault /vault.html 200",
"/profile /profile.html 200",
"/privacy-policy /privacy-policy.html 200",
"/terms-and-conditions /terms-and-conditions.html 200",
"/request-book /request-book.html 200",
"/contributors /contributors.html 200",
"/contributing /contributing.html 200",
"/community-stories /community-stories.html 200",
"/nearby-bookstores /nearby-bookstores.html 200",
]
(DIST / "_redirects").write_text("\n".join(redirects) + "\n", encoding="utf-8")
def inject_api_base_override() -> None:
"""Optional Netlify build env MOOD_API_BASE → runtime override in dist config.js."""
api_base = os.getenv("MOOD_API_BASE", "").strip()
if not api_base:
return
config_path = DIST / "js" / "config.js"
if not config_path.exists():
return
snippet = f'window.__MOOD_API_BASE_OVERRIDE__ = {api_base!r};\n'
content = config_path.read_text(encoding="utf-8")
config_path.write_text(snippet + content, encoding="utf-8")
def main() -> None:
reset_dist()
for folder in ("css", "js", "assets", "script"):
copy_tree(folder)
manifest_src = SOURCE / "manifest.json"
if manifest_src.exists():
shutil.copy2(manifest_src, DIST / "manifest.json")
build_html()
write_clean_route_redirects()
inject_api_base_override()
if __name__ == "__main__":
main()