start build.py to replace build.js

Start creating build.py script in order to build client in order to drop build.js because there are better perfs and less code in python ( = more maintenability and readability)
This commit is contained in:
Jordan Blasenhauer 2024-07-22 12:13:01 +02:00
parent 03fbec034e
commit 84b1863bab
2 changed files with 108 additions and 15 deletions

View file

@ -68,38 +68,43 @@ async function buildVite() {
// Change dir structure for flask app
async function updateClientDir() {
const srcDir = resolve(`./${clientBuildDir}/dashboard/pages`);
const dirToRem = resolve(`./${clientBuildDir}/dashboard`);
const temporaryTemp = resolve(`./templatestemp`);
const baseTemp = resolve(`./${clientBuildDir}/dashboard/`);
const staticTemp = resolve(`./${clientBuildDir}/templates`);
try {
await createDirIfNotExists(staticTemp);
await copyDir(srcDir, staticTemp);
await delElRecursive(dirToRem);
await changeOutputTemplates();
createDirIfNotExists(staticTemp);
copyDir(srcDir, temporaryTemp);
changeOutputTemplates();
// Delete templatestemp dir
delElRecursive(baseTemp);
} catch (err) {
console.log(err);
}
}
async function changeOutputTemplates() {
const templateDir = resolve(`./${clientBuildDir}/templates`);
fs.readdir(templateDir, (err, subdirs) => {
const templateTempDir = resolve(`./templatestemp`);
fs.readdir(templateTempDir, (err, subdirs) => {
subdirs.forEach((subdir) => {
// Get absolute path of current subdir
const currPath = resolve(`./${clientBuildDir}/templates/${subdir}`);
const currPath = resolve(`./templatestemp/${subdir}`);
// Rename index.html by subdir name
moveFile(`${currPath}/index.html`, `./templates/${subdir}.html`);
moveFile(
`${currPath}/index.html`,
`./${clientBuildDir}/templates/${subdir}.html`
);
});
});
}
async function setBuildTempToUI() {
// Run all files in /templates and get data
fs.readdir(resolve("./templates"), (err, files) => {
fs.readdir(resolve(`./${clientBuildDir}/templates`), (err, files) => {
// Read content
files.forEach((file) => {
fs.readFile(
resolve(`./templates/${file}`),
resolve(`./${clientBuildDir}/templates/${file}`),
{
encoding: "utf8",
flag: "r",
@ -155,14 +160,12 @@ async function setBuildTempToUI() {
}
);
});
// Delete templates to avoid to add it to static
delElRecursive("./dashboard/templates");
});
}
async function moveBuildStaticToUI() {
// move build static subdir to app ui static dir
const srcDir = resolve(`./dashboard`);
const srcDir = resolve(`./${clientBuildDir}`);
const destDir = resolve(appStaticDir);
fs.readdir(srcDir, (err, dirs) => {
dirs.forEach((dir) => {
@ -194,7 +197,7 @@ async function build() {
await delPrevDirs();
await buildVite();
await updateClientDir();
// await setBuildTempToUI();
await setBuildTempToUI();
// await moveBuildStaticToUI();
// await buildSetup();
}

90
src/ui/client/build.py Normal file
View file

@ -0,0 +1,90 @@
# I want to run process
from subprocess import Popen, PIPE
import os
import shutil
# get current directory
current_directory = os.path.dirname(os.path.realpath(__file__))
# needed dirs
opt_dir = f"{current_directory}/output"
opt_dir_templates = f"{current_directory}/output/templates"
opt_dir_dashboard = f"{current_directory}/opt-dashboard"
opt_dir_dashboard_pages = f"{current_directory}/opt-dashboard/dashboard/pages"
opt_dir_setup = f"{current_directory}/opt-setup"
opt_dir_setup_page = f"{current_directory}/opt-setup/setup"
ui_dir_static = f"{current_directory}/../static"
ui_dir_templates = f"{current_directory}/../templates"
statics = ("assets", "css", "flags", "img", "js")
def run_command(command, need_wait=False):
process = Popen(command, stdout=PIPE, stderr=PIPE, cwd=current_directory, shell=True)
if need_wait:
process.wait()
out, err = process.communicate()
if err:
print("Error: ", err)
print(out)
def remove_dir(directory):
if os.path.exists(directory):
shutil.rmtree(directory)
def reset():
remove_dir(opt_dir)
remove_dir(opt_dir_dashboard)
remove_dir(opt_dir_setup)
os.makedirs(opt_dir, exist_ok=True)
os.makedirs(opt_dir_templates, exist_ok=True)
def move_template(folder, target_folder):
# 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
# 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")
shutil.copy(file_path, f"{target_folder}/{os.path.basename(root)}.html")
def move_statics(folder, target_folder):
# I want to get all subfollder of a folder
for root, dirs, files in os.walk(folder):
for dir in dirs:
if dir not in statics:
continue
dir = os.path.join(root, dir)
# remove previous folder if exists
if os.path.exists(f"{target_folder}/{os.path.basename(dir)}"):
shutil.rmtree(f"{target_folder}/{os.path.basename(dir)}")
# rename index.html by the name of the folder
shutil.move(dir, f"{target_folder}/{os.path.basename(dir)}")
def move_opt_to_ui():
move_statics(opt_dir_dashboard)
def build():
reset()
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
build()