Skip to content

Commit

Permalink
build.py working, removing build.js
Browse files Browse the repository at this point in the history
  • Loading branch information
syrk4web committed Jul 22, 2024
1 parent 84b1863 commit 373b0d5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 217 deletions.
4 changes: 4 additions & 0 deletions src/ui/client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# build
opt-dashboard
opt-setup
2 changes: 1 addition & 1 deletion src/ui/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For example, you need to execute `npm run dev-dashboard` to run a vite dev serve

In case you want to run the BunkerWeb UI, try to update front-end and get the modifications on your app, you need to do the following :
- go to `misc/dev` path and run `docker compose -f docker-compose.ui.yml up --build` in order to create BunkerWeb with UI looking for local static and templates folder
- update front-end in dev mode and run inside `cd/src/ui/client` : `node ./build.js` to rebuild setup and dashboard pages.
- update front-end in dev mode and run inside `cd/src/ui/client` : `python build.py` to rebuild setup and dashboard pages.

# Prod mode

Expand Down
205 changes: 0 additions & 205 deletions src/ui/client/build.js

This file was deleted.

62 changes: 51 additions & 11 deletions src/ui/client/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from subprocess import Popen, PIPE
import os
import shutil
import re

# get current directory
current_directory = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -38,16 +39,52 @@ def reset():
remove_dir(opt_dir)
remove_dir(opt_dir_dashboard)
remove_dir(opt_dir_setup)


def create_base_dirs():
os.makedirs(opt_dir, exist_ok=True)
os.makedirs(opt_dir_templates, exist_ok=True)


def move_template(folder, target_folder):
base_html = """
<body>
{% set data_server_flash = [] %}
{% with messages = get_flashed_messages(with_categories=true) %}
{% for category, message in messages %}
{% if data_server_flash.append({"type": "error" if category == "error" else "success", "title": "dashboard_error" if category == "error" else "dashboard_success", "message": message}) %}{% endif %}
{% endfor %}
{% endwith %}
<div class='hidden' data-csrf-token='{{ csrf_token() }}'></div>
<div class='hidden' data-server-global='{{data_server_global if data_server_global else {}}}'></div>
<div class='hidden' data-server-flash='{{data_server_flash|tojson}}'></div>
<div class='hidden' data-server-builder='{{data_server_builder[1:-1]}}'></div>
<div id='app'></div>
</body>
</html>"""

# I want to get all subfollder of a folder
for root, dirs, files in os.walk(folder):
for file in files:
file_path = os.path.join(root, file)
# rename index.html by the name of the folder

# get file content
content = ""
with open(file_path, "r") as f:
content = f.read()
# I want to replace all paths using regex like this : /href="\/(css|js|img|favicon|assets|js)|src="\/(assets|js)/g
regex_paths = """/href="\/(css|js|img|favicon|assets|js)|src="\/(assets|js)/g"""
content = re.sub(regex_paths, "", content)
regex_script = """/<script/g"""
content = re.sub(regex_script, '<script nonce="{{ script_nonce }}" ', content)
# get index of <body>
index = content.index("<body>")
# get the content before <body>
content = content[:index] + base_html
# write the new content
with open(file_path, "w") as f:
f.write(content)

# remove previous file if exists
if os.path.exists(f"{target_folder}/{os.path.basename(root)}.html"):
os.remove(f"{target_folder}/{os.path.basename(root)}.html")
Expand All @@ -70,21 +107,24 @@ def move_statics(folder, target_folder):
shutil.move(dir, f"{target_folder}/{os.path.basename(dir)}")


def move_opt_to_ui():
move_statics(opt_dir_dashboard)
def set_dashboard():
move_template(opt_dir_dashboard_pages, ui_dir_templates)
move_statics(opt_dir_dashboard, ui_dir_static)


def set_setup():
move_template(opt_dir_setup_page, ui_dir_templates)


def build():
reset()
create_base_dirs()
run_command(["npm", "install"], True)
run_command(["npm", "run", "build-dashboard"])
run_command(["npm", "run", "build-setup"], True)
# format dashboard files
move_template(opt_dir_dashboard_pages, ui_dir_templates)
move_statics(opt_dir_dashboard, ui_dir_static)
# format setup files
move_template(opt_dir_setup_page, ui_dir_templates)
# now move output files to the ui
run_command(["npm", "run", "build-dashboard"], True)
# run_command(["npm", "run", "build-dashboard"])
# run_command(["npm", "run", "build-setup"], True)
set_dashboard()
# set_setup()


build()

0 comments on commit 373b0d5

Please sign in to comment.