diff --git a/pynecone/app.py b/pynecone/app.py index d6a7c4f74..3979d246f 100644 --- a/pynecone/app.py +++ b/pynecone/app.py @@ -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) diff --git a/pynecone/compiler/compiler.py b/pynecone/compiler/compiler.py index 4b96e4dfb..7f3a4d9e1 100644 --- a/pynecone/compiler/compiler.py +++ b/pynecone/compiler/compiler.py @@ -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) diff --git a/pynecone/compiler/utils.py b/pynecone/compiler/utils.py index 3a2e84f02..17f2846c4 100644 --- a/pynecone/compiler/utils.py +++ b/pynecone/compiler/utils.py @@ -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))