in server/app/lib/assets.py [0:0]
def generate_assets(static_dir, htdocs_dir):
"""Generates the HTML scaffolding from EZT and compiles JS"""
from .. import plugins
if not os.path.isdir(htdocs_dir):
print(f"Creating {htdocs_dir}")
os.makedirs(htdocs_dir, exist_ok=True)
# Generate front page HTML
origin_filepath = os.path.join(static_dir, "templates", "index.ezt")
target_filepath = os.path.join(htdocs_dir, "index.html")
# print(f"Writing front page file {target_filepath}")
datadict = {
"plugins": plugins.root.plugins,
"uuid": str(uuid.uuid4()), # cache rejection on site regen
}
ezt_to_html(template_file=origin_filepath, data=datadict, target_filename=target_filepath)
# Compile JS assets
js_assets = ""
plugin_js_dir = os.path.join(static_dir, "plugins")
for filename in sorted(os.listdir(plugin_js_dir)):
if filename.endswith(".js"):
filepath = os.path.join(plugin_js_dir, filename)
try:
filedata = open(filepath).read()
js_assets += f"// {filename}:\n\n{filedata}\n"
except FileNotFoundError:
# In dev/test, this might be (say) an emacs recovery file,
# which does not really exist. Skip it.
# In production, we *just* did a listdir(), so we should
# never see a problem opening the file.
pass
# Copy all assets to htdocs
assets_origin = os.path.join(static_dir, "assets")
assets_target = os.path.join(htdocs_dir, "_assets")
shutil.copytree(assets_origin, assets_target, dirs_exist_ok=True)
with open(os.path.join(assets_target, "plugins.js"), "w") as f:
f.write(js_assets)