Преглед изворни кода

Empty pages directory on recompile (#269)

andy-verstraeten пре 2 година
родитељ
комит
b93c7a8cbf
3 измењених фајлова са 22 додато и 0 уклоњено
  1. 3 0
      pynecone/app.py
  2. 6 0
      pynecone/compiler/compiler.py
  3. 13 0
      pynecone/compiler/utils.py

+ 3 - 0
pynecone/app.py

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

+ 6 - 0
pynecone/compiler/compiler.py

@@ -208,3 +208,9 @@ def compile_components(components: Set[CustomComponent]):
     # Compile the components.
     # Compile the components.
     code = _compile_components(components)
     code = _compile_components(components)
     return output_path, code
     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)

+ 13 - 0
pynecone/compiler/utils.py

@@ -304,3 +304,16 @@ def write_page(path: str, code: str):
     utils.mkdir(os.path.dirname(path))
     utils.mkdir(os.path.dirname(path))
     with open(path, "w") as f:
     with open(path, "w") as f:
         f.write(code)
         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))