Empty pages directory on recompile (#269)

This commit is contained in:
andy-verstraeten 2023-01-18 21:11:31 +01:00 committed by GitHub
parent 40801186ad
commit b93c7a8cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -284,6 +284,9 @@ class App(Base):
# Create the database models.
Model.create_all()
# Empty the .web pages directory
compiler.purge_web_pages_dir()
# Compile the root document with base styles and fonts.
compiler.compile_document_root(self.stylesheets)

View File

@ -208,3 +208,9 @@ def compile_components(components: Set[CustomComponent]):
# Compile the components.
code = _compile_components(components)
return output_path, code
def purge_web_pages_dir():
"""Empty out .web directory."""
template_files = ["_app.js", "404.js"]
utils.empty_dir(constants.WEB_PAGES_DIR, keep_files=template_files)

View File

@ -304,3 +304,16 @@ def write_page(path: str, code: str):
utils.mkdir(os.path.dirname(path))
with open(path, "w") as f:
f.write(code)
def empty_dir(path, keep_files=[]):
"""Remove all files and folders in a directory except for the kept file- or foldernames.
Args:
path (str): The path to the directory that will be emptied
keep_files (list, optional): List of filenames or foldernames that will not be deleted. Defaults to [].
"""
directory_contents = os.listdir(path)
for element in directory_contents:
if element not in keep_files:
utils.rm(os.path.join(path, element))