Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛✨ [Frontend] Automatically pull latest frontend version (nocache) #7054

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
65 changes: 53 additions & 12 deletions services/static-webserver/client/scripts/post-compile.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,82 @@
import os
import json

import os
import random

output_folders = [
"source-output", # dev output
"source-output", # dev output
"build-output", # default production output
"build-client" # I believe we create the production outputs here
"build-client", # I believe we create the production outputs here
]


def read_json_file(filename):
def _read_json_file(filename):
dirname = os.path.dirname(__file__)
meta_filename = os.path.join(dirname, filename)
with open(meta_filename, "r") as file:
with open(meta_filename) as file:
metadata = json.load(file)
return metadata["applications"]


def update_apps_metadata():
dirname = os.path.dirname(__file__)
applications = read_json_file("apps_metadata.json")
applications = _read_json_file("apps_metadata.json")
for i in applications:
application = i.get("application")
replacements = i.get("replacements")
for output_folder in output_folders:
filename = os.path.join(dirname, '..', output_folder, application, "index.html")
filename = os.path.join(
dirname, "..", output_folder, application, "index.html"
)
if not os.path.isfile(filename):
continue
with open(filename, "r") as file:
with open(filename) as file:
data = file.read()
replacements = i.get("replacements")
for key in replacements:
replace_text = replacements[key]
data = data.replace("${"+key+"}", replace_text)
with open(filename, "w") as file:
data = data.replace("${" + key + "}", replace_text)
with open(filename, "w") as file:
print(f"Updating app metadata: {filename}")
file.write(data)


def _get_output_file_paths(filename):
index_file_paths = []
dirname = os.path.dirname(__file__)
applications = _read_json_file("apps_metadata.json")
for i in applications:
application = i.get("application")
for output_folder in output_folders:
index_file_paths.append(
os.path.join(dirname, "..", output_folder, application, filename)
)
return index_file_paths


def add_no_cache_param(vcs_ref_client):
index_file_paths = _get_output_file_paths("index.html")
for index_file_path in index_file_paths:
if not os.path.isfile(index_file_path):
continue
with open(index_file_path) as index_file:
data = index_file.read()
data = data.replace("${boot_params}", "nocache=" + vcs_ref_client)
with open(index_file_path, "w") as file:
print(f"Updating vcs_ref_client: {index_file_path}")
file.write(data)

boot_file_paths = _get_output_file_paths("boot.js")
for boot_file_path in boot_file_paths:
Copy link
Member

@pcrespov pcrespov Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIP: if you would instead use pathlib.Path, your code would become more compact

from pathlib import Path

boot_file_paths: list[Path] = [] # assume we have this


for boot_file_path in boot_file_paths:
    if boot_file_path.is_file():
        print(f"Updating URL_PARAMETERS: {boot_file_path}")

        boot_file_path.write_text(
            boot_file_path.read_text().replace(
                "addNoCacheParam : false",
                "addNoCacheParam : true",
            )
        )

and the same with all the operations to compose paths.

if not os.path.isfile(boot_file_path):
continue
with open(boot_file_path) as boot_file:
data = boot_file.read()
data = data.replace("addNoCacheParam : false", "addNoCacheParam : true")
with open(boot_file_path, "w") as file:
print(f"Updating URL_PARAMETERS: {boot_file_path}")
file.write(data)


if __name__ == "__main__":
update_apps_metadata()
vcs_ref_client = os.getenv("VCS_REF_CLIENT", str(random.random()))
add_no_cache_param(vcs_ref_client)
2 changes: 1 addition & 1 deletion services/static-webserver/client/source/boot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</head>
<body>
${preBootJs}
<script type="text/javascript" src="${appPath}boot.js"></script>
<script type="text/javascript" src="${appPath}boot.js?${boot_params}"></script>
</body>
<script>
window.markerConfig = {
Expand Down
Loading