151 lines
6.3 KiB
Python
151 lines
6.3 KiB
Python
#!/usr/bin/env python3
|
|
"""ตรวจความสอดคล้องของ SPA/PWA แบบ static (ไม่ต้องเปิดเบราว์เซอร์)
|
|
|
|
ตรวจ:
|
|
1. ลิงก์ภายใน partial ต้องเป็น "#/..." เท่านั้น (ห้าม "#section" หรือ "./x.html")
|
|
2. ทุก route ใน router.js ต้องมีไฟล์ partial อยู่จริง และไฟล์ partial ที่มีต้องถูกอ้างใน router
|
|
3. ทุกไฟล์ใน PRECACHE ของ sw.js ต้องมีอยู่จริง
|
|
4. VERSION ของ sw.js / app/version.js / app/version.json / index.html ?v= ต้องตรงกัน
|
|
5. shell (index.html) ต้องมี <link rel="manifest">, theme-color, และสคริปต์ register
|
|
6. ไม่มีอีโมจิในไฟล์ที่ผู้ใช้เห็น (partial, css, js)
|
|
|
|
ใช้: python tools/check_app.py (exit 1 ถ้าเจอปัญหา)
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
import urllib.parse
|
|
|
|
PROBLEMS = []
|
|
NOTES = []
|
|
|
|
EMOJI = re.compile(
|
|
"[\U0001F300-\U0001FAFF\U00002600-\U000027BF\U0001F000-\U0001F0FF\U00002190-\U000021FF\U00002B00-\U00002BFF]"
|
|
)
|
|
|
|
# สัญลักษณ์ที่ยอมให้ใช้ (ลูกศร/ขีดคั่น/เครื่องหมายคำพูด) — ไม่นับเป็นอีโมจิ
|
|
ALLOWED_SYMBOLS = set("→←↑↓↔⇒⇐✓✔✗·–—«»…‰°±×÷")
|
|
|
|
|
|
def problem(message):
|
|
PROBLEMS.append(message)
|
|
|
|
|
|
def read(path):
|
|
with open(path, encoding="utf-8") as handle:
|
|
return handle.read()
|
|
|
|
|
|
def check_partial_links():
|
|
partial_dir = "partials"
|
|
for name in sorted(os.listdir(partial_dir)):
|
|
if not name.endswith(".html"):
|
|
continue
|
|
path = os.path.join(partial_dir, name)
|
|
body = read(path)
|
|
for match in re.finditer(r'href="([^"]+)"', body):
|
|
href = match.group(1)
|
|
if href.startswith(("#/", "http://", "https://", "mailto:", "tel:")):
|
|
continue
|
|
if href.startswith("#"):
|
|
problem("%s: ลิงก์ '%s' เป็น hash ธรรมดา (จะทำให้ router เปลี่ยนหน้า) — ใช้ '#/...'" % (path, href))
|
|
continue
|
|
if href.endswith(".html"):
|
|
problem("%s: ลิงก์ '%s' จะโหลดหน้าใหม่ — ใช้ '#/...'" % (path, href))
|
|
|
|
|
|
def check_routes():
|
|
router = read("app/router.js")
|
|
routes = re.findall(r"^\s*'?([a-z-]+)'?:\s*\{\s*partial:\s*'([^']+)'", router, re.MULTILINE)
|
|
declared = set()
|
|
for _name, partial in routes:
|
|
declared.add(partial)
|
|
if not os.path.exists(partial):
|
|
problem("router.js ชี้ %s ที่ไม่มีอยู่จริง" % partial)
|
|
|
|
for name in sorted(os.listdir("partials")):
|
|
if name.endswith(".html") and "partials/" + name not in declared:
|
|
NOTES.append("partials/%s ไม่มี route (เข้าถึงไม่ได้แต่ไม่พัง)" % name)
|
|
|
|
|
|
def check_precache():
|
|
sw = read("sw.js")
|
|
block = sw.split("const PRECACHE = [")[1].split("];")[0]
|
|
urls = re.findall(r'"([^"]+)"', block)
|
|
for url in urls:
|
|
path = urllib.parse.unquote(url.replace("./", "", 1))
|
|
if not os.path.exists(path):
|
|
problem("PRECACHE ชี้ไฟล์ที่ไม่มี: %s" % url)
|
|
NOTES.append("PRECACHE: %d ไฟล์" % len(urls))
|
|
|
|
|
|
def check_versions():
|
|
sw = re.search(r'const VERSION = "([^"]+)"', read("sw.js")).group(1)
|
|
version_js = re.search(r'window\.FARM_VERSION = "([^"]+)"', read("app/version.js")).group(1)
|
|
version_json = json.loads(read("app/version.json"))["version"]
|
|
shell = set(re.findall(r"\?v=([0-9][^\"&]*)", read("index.html")))
|
|
package = json.loads(read("package.json"))["version"]
|
|
|
|
versions = {"sw.js": sw, "app/version.js": version_js, "app/version.json": version_json,
|
|
"package.json": package}
|
|
for key, value in versions.items():
|
|
if value != sw:
|
|
problem("เวอร์ชันไม่ตรง: %s = %s แต่ sw.js = %s" % (key, value, sw))
|
|
if shell != {sw}:
|
|
problem("index.html ?v= = %s แต่ sw.js = %s" % (sorted(shell), sw))
|
|
NOTES.append("version: %s" % sw)
|
|
|
|
|
|
def check_shell():
|
|
shell = read("index.html")
|
|
for needle, label in [
|
|
('rel="manifest"', "ไม่มี <link rel=manifest>"),
|
|
('name="theme-color"', "ไม่มี meta theme-color"),
|
|
('app/pwa.js', "ไม่มีสคริปต์ app/pwa.js"),
|
|
('app/router.js', "ไม่มีสคริปต์ app/router.js"),
|
|
('id="view"', "ไม่มี <main id=view>"),
|
|
]:
|
|
if needle not in shell:
|
|
problem("index.html: %s" % label)
|
|
|
|
manifest = json.loads(read("manifest.json"))
|
|
for icon in manifest["icons"]:
|
|
if not os.path.exists(icon["src"]):
|
|
problem("manifest ชี้ไอคอนที่ไม่มี: %s" % icon["src"])
|
|
NOTES.append("manifest icons: %d" % len(manifest["icons"]))
|
|
|
|
|
|
def check_emoji():
|
|
targets = ["index.html", "sw.js", "manifest.json"]
|
|
for folder in ("partials", "app", "core", "assets/css"):
|
|
for name in os.listdir(folder):
|
|
if name.endswith((".html", ".js", ".css")):
|
|
targets.append(os.path.join(folder, name))
|
|
for path in targets:
|
|
for line_number, line in enumerate(read(path).splitlines(), 1):
|
|
hit = EMOJI.search(line)
|
|
if hit and hit.group(0) not in ALLOWED_SYMBOLS:
|
|
problem("%s:%d มีอีโมจิ: %s" % (path, line_number, line.strip()[:60]))
|
|
|
|
|
|
def main():
|
|
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
for check in (check_partial_links, check_routes, check_precache, check_versions, check_shell, check_emoji):
|
|
check()
|
|
|
|
for note in NOTES:
|
|
print(" · " + note)
|
|
if PROBLEMS:
|
|
print("\nพบปัญหา %d จุด:" % len(PROBLEMS))
|
|
for item in PROBLEMS:
|
|
print(" ✗ " + item)
|
|
return 1
|
|
print("\nOK — SPA/PWA ผ่านการตรวจ static ทั้งหมด")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|