Skip to content

Commit

Permalink
fix: add script to install node_modules in mounted mfes
Browse files Browse the repository at this point in the history
  • Loading branch information
hinakhadim committed Oct 31, 2024
1 parent a178535 commit 7b1f7e9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tutormfe/patches/dev-docker-compose-jobs-services
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{%- for app_name, app in iter_mfes() %}
{%- set mounts = iter_mounts(MOUNTS, app_name)|list %}
{%- if mounts %}
{{ app_name }}-job:
image: "{{ MFE_DOCKER_IMAGE_DEV_PREFIX }}-{{ app_name }}-dev:{{ MFE_VERSION }}"
volumes:
{%- for mount in mounts %}
- {{ mount }}
{%- endfor %}
{%- endif %}
{%- endfor %}
25 changes: 25 additions & 0 deletions tutormfe/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import functools
import os
import re
import typing as t
from glob import glob

Expand Down Expand Up @@ -304,3 +305,27 @@ def _check_mfe_host(config: Config) -> None:
f'Warning: MFE_HOST="{mfe_host}" is not a subdomain of LMS_HOST="{lms_host}". '
"This configuration is not typically recommended and may lead to unexpected behavior."
)


@tutor_hooks.Actions.CONFIG_LOADED.add()
def _run_jobs_in_mounted_mfes(config: Config) -> None:
mounts = get_typed(config, "MOUNTS", list, [])

pattern = re.compile(r'frontend-app-(\w+)')
mounted_mfes = [match.group(1) for mount in mounts if (match := pattern.search(mount))]

mfe_task_file = os.path.join(
str(
importlib_resources.files("tutormfe")
/ "templates"
/ "mfe"
/ "tasks"
/ "mfe"
/ "npm-install.sh"
)
)

for mounted_mfe in mounted_mfes:
with tutor_hooks.Contexts.app("mfe").enter():
with open(mfe_task_file, encoding='utf-8') as fi:
tutor_hooks.Filters.CLI_DO_INIT_TASKS.add_item((mounted_mfe , fi.read()))
36 changes: 36 additions & 0 deletions tutormfe/templates/mfe/tasks/mfe/npm-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Paths for the fingerprint and package-lock.json
FINGERPRINT_FILE="node_modules/.fingerprint"
PACKAGE_LOCK="package-lock.json"

# Function to generate the SHA1 hash of the package-lock.json file
generate_fingerprint() {
sha1sum "$PACKAGE_LOCK" | awk '{print $1}'
}

# Check if node_modules exists and fingerprint is valid
if [ ! -d "node_modules" ]; then
echo "⚠️ node_modules not found! Installing dependencies..."
npm clean-install

elif [ ! -f "$FINGERPRINT_FILE" ]; then
echo "⚠️ Fingerprint file not found! Re-installing dependencies..."
npm clean-install

else
CURRENT_FINGERPRINT=$(generate_fingerprint)
STORED_FINGERPRINT=$(cat "$FINGERPRINT_FILE")

if [ "$CURRENT_FINGERPRINT" != "$STORED_FINGERPRINT" ]; then
echo "⚠️ package-lock.json changed! Re-installing dependencies..."
npm clean-install
else
echo "✅ Dependencies are up to date."
exit 0
fi
fi

# Store the new fingerprint after installation
generate_fingerprint > "$FINGERPRINT_FILE"
echo "✅ Dependencies installed and fingerprint updated."

0 comments on commit 7b1f7e9

Please sign in to comment.