From 04006bb736eaa163aba2f700f40569abbc804f75 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 4 Jun 2023 09:45:05 +0200 Subject: [PATCH] automatically handle static files --- README.md | 1 + config.json | 6 ++++++ lib/config.py | 1 + lib/main.py | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d9eb82..15b64c2 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re "contact.html", "reservation.html" ], + "staticFiles": [], "outDir": "dist" } ``` diff --git a/config.json b/config.json index c2898df..155fdc4 100644 --- a/config.json +++ b/config.json @@ -7,5 +7,11 @@ "contact.html", "reservation.html" ], + "staticFiles": [ + "images", + "js", + "css", + "webfonts" + ], "outDir": "dist" } \ No newline at end of file diff --git a/lib/config.py b/lib/config.py index 908f0b1..717c650 100644 --- a/lib/config.py +++ b/lib/config.py @@ -5,6 +5,7 @@ from typing import TypedDict class Config(TypedDict): name: str templates: list[str] + staticFiles: list[str] outDir: str diff --git a/lib/main.py b/lib/main.py index 2bf37f0..f5842c7 100644 --- a/lib/main.py +++ b/lib/main.py @@ -1,3 +1,5 @@ +import os +import shutil import sys from os import path @@ -15,11 +17,22 @@ def main(): sys.exit() data = {} + destination_path = config["outDir"] + logger.info(f"🏁 Start building {config['name']}") + + if os.path.exists(destination_path) and os.path.isdir(destination_path): + shutil.rmtree(destination_path) + os.mkdir(destination_path) + for template in config["templates"]: logger.info(f"📃Render '{template}'") - with open(path.join(config["outDir"], template), "w") as f: + with open(path.join(destination_path, template), "w") as f: f.write(render(template, data.get(template))) + + logger.info("⏩ Start copying staticfiles to build") + for folder in config["staticFiles"]: + shutil.copytree(folder, f"{config['outDir']}/{folder}") logger.info("🎉 Done…")