Files
operating-room-monitor-server/scripts/dev_static_livereload.py
2026-04-28 10:41:48 +08:00

71 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""以 livereload 提供静态资源目录:保存 HTML/JS/CSS 时浏览器自动刷新。
需 dev 依赖:``uv sync --group dev````livereload``)。
用法:
uv run --group dev python scripts/dev_static_livereload.py --root web/voice-confirmation
uv run --group dev python scripts/dev_static_livereload.py --root web/voice-confirmation -p 8080 --host 127.0.0.1
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
def main() -> None:
try:
from livereload import Server
except ImportError as exc: # pragma: no cover
print(
"需要安装 dev 依赖: uv sync --group dev (需含 livereload",
file=sys.stderr,
)
raise SystemExit(1) from exc
p = argparse.ArgumentParser(description="Static site + browser live reload (livereload)")
p.add_argument(
"--root",
type=Path,
required=True,
help="要托管的目录(如 web/voice-confirmation",
)
p.add_argument("--host", default="127.0.0.1")
p.add_argument("-p", "--port", type=int, default=8080)
p.add_argument(
"--extra-watch",
type=Path,
action="append",
default=[],
help="额外监视路径(可重复),变更时触发自动刷新",
)
args = p.parse_args()
root: Path = args.root.resolve()
if not root.is_dir():
print(f"不是目录: {root}", file=sys.stderr)
raise SystemExit(2)
server = Server()
server.watch(str(root))
for w in args.extra_watch:
wp = w.resolve()
if wp.exists():
server.watch(str(wp if wp.is_file() else wp))
url = f"http://{args.host}:{args.port}/"
print(f"Livereload 静态根目录: {root}")
print(f"访问: {url} (编辑目录内文件后应自动刷新浏览器;首次请手动打开)")
print("按 Ctrl+C 停止。")
server.serve(
host=args.host,
port=args.port,
root=str(root),
open_url=False,
debug=False,
)
if __name__ == "__main__":
main()