diff options
| author | Paweł Redman <pawel.redman@gmail.com> | 2026-06-14 21:00:05 +0200 |
|---|---|---|
| committer | Paweł Redman <pawel.redman@gmail.com> | 2026-06-14 21:00:05 +0200 |
| commit | 6f945ce1a971099b53ed6ce94fff62abd07955df (patch) | |
| tree | 0d148da98d7e95caa89b43c93776242d7341c701 /build.py | |
| parent | e98fac942f149b9870790c112e082733b40535a9 (diff) | |
Add watchfiles
Diffstat (limited to 'build.py')
| -rw-r--r-- | build.py | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -3,12 +3,15 @@ import json import os import shutil +from argparse import ArgumentParser from dataclasses import dataclass from datetime import datetime from pathlib import Path import jinja2 +from loguru import logger from typst import Compiler +from watchfiles import watch @dataclass @@ -28,7 +31,7 @@ class Builder: def _compile_typst_page(self, path: Path) -> CompiledPage: relative_path = path.relative_to(self.content_dir) - print(f'Typst: {relative_path}') + logger.info(f'Typst: {relative_path}') output_path = self.public_dir / relative_path.with_suffix('.html') output_path.parent.mkdir(parents=True, exist_ok=True) @@ -62,20 +65,24 @@ class Builder: (self.public_dir / 'index.html').write_text(template.render(pages=pages)) def build(self) -> None: - print(f'Removing {str(self.public_dir)!r}...') + logger.info(f'Removing {str(self.public_dir)!r}...') shutil.rmtree(self.public_dir, ignore_errors=True) self.public_dir.mkdir(parents=True, exist_ok=True) - print('Linking static directory...') + logger.info('Linking static directory...') (self.public_dir / 'static').symlink_to(self.static_dir) pages = self._compile_typst_pages() - print('Building index page...') + logger.info('Building index page...') self._compile_index_page(pages) def main() -> None: + parser = ArgumentParser() + parser.add_argument('-w', '--watch', action='store_true', help='watch for changes and rebuild') + args = parser.parse_args() + repo_dir = Path(__file__).parent site_dir = repo_dir / 'site' public_dir = repo_dir / 'public' @@ -86,6 +93,13 @@ def main() -> None: builder = Builder(site_dir=site_dir, static_dir=static_dir, public_dir=public_dir) builder.build() + if args.watch: + logger.info('Watching for changes...') + + for changes in watch(site_dir): + print(changes) + builder.build() + if __name__ == '__main__': main() |
