Update for frontend update

This commit is contained in:
timqiu 2025-03-22 00:44:53 +08:00
parent e982d81d69
commit 0ede803fb3

View File

@ -49,6 +49,7 @@ exit_with_message() {
exit $code
}
detect_os() {
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "darwin"
@ -634,17 +635,6 @@ get_port() {
done
}
# Build the local image
build_local_image() {
local dockerfile_path="$1"
log_info "[BUILD] Building local image..."
docker buildx build \
--platform linux/amd64 \
--build-arg BUILDARCH="x86-64-v3" \
-t $devbox_full_image \
-f "$dockerfile_path" .
}
###############################################
# Initialize the development environment
@ -794,17 +784,36 @@ if true ; then
# 3⃣ Clean up old dependencies
if [ -f "pnpm-lock.yaml" ]; then
mv pnpm-lock.yaml /tmp/pnpm-lock.yaml.bak
cp pnpm-lock.yaml /tmp/pnpm-lock.yaml.bak
fi
rm -rf node_modules pnpm-lock.yaml
rm -rf node_modules
# 4⃣ Install dependencies (ensuring lockfile updates)
pnpm install --no-frozen-lockfile
pnpm install --no-frozen-lockfile \\
--shamefully-hoist \\
--link-workspace-packages false \\
--store-dir /home/devbox/.pnpm-store
# 4⃣ Build the frontend
pnpm run build
# 5⃣ Check git status
echo
echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Checking git status..."
echo
git config --global --add safe.directory /home/devbox/freeleaps
git status -s
echo
echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Git status check completed."
echo
echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Backend and frontend environment initialization completed."
fi
@ -1155,6 +1164,12 @@ docker exec -i "$DEVBOX_NAME" bash <<EOF
baseline_frontend=\$(mktemp)
git config --global --add safe.directory /home/devbox/freeleaps
git status -s > "\$baseline_frontend"
# save baseline to /home/devbox/tmp/baseline_frontend
mkdir -p /home/devbox/tmp
cp "\$baseline_frontend" /home/devbox/tmp/baseline_frontend.bak
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Recorded baseline before compilation: \$baseline_frontend"
# Check if the frontend service is already running according to the package.json and pnpm-lock.yaml files timestamps
@ -1163,12 +1178,18 @@ docker exec -i "$DEVBOX_NAME" bash <<EOF
lock_time=\$(stat -c "%Y" pnpm-lock.yaml)
modules_time=\$(stat -c "%Y" node_modules)
# Calculate the time difference between the lock file and the modules file
# Calculate the absolute value of the time difference between the lock file and the modules file
time_diff=\$(( lock_time - modules_time ))
if [ \$time_diff -lt 0 ]; then
time_diff=\$(( -time_diff ))
fi
# Set the threshold for the time difference
threshold=150
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Checking frontend dependencies..."
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Time difference: \$time_diff seconds, lock_time: \$lock_time, modules_time: \$modules_time"
if [[ ! -d "node_modules" || "package.json" -nt "node_modules" || \$time_diff -gt \$threshold ]]; then
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Installing/Updating frontend dependencies..."
@ -1181,7 +1202,10 @@ docker exec -i "$DEVBOX_NAME" bash <<EOF
fi
# Install dependencies
pnpm install --no-frozen-lockfile || {
pnpm install --no-frozen-lockfile \\
--shamefully-hoist \\
--link-workspace-packages false \\
--store-dir /home/devbox/.pnpm-store || {
echo
echo "============================================================================================"
echo
@ -1224,6 +1248,15 @@ docker exec -i "$DEVBOX_NAME" bash <<EOF
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Start frontend service..."
nohup pnpm run dev > /home/devbox/logs/frontend.logs 2>&1 &
# Check git status
echo
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Checking git status..."
echo
git status -s
echo
echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Git status check completed."
echo
# Check the health of the frontend service: poll to detect HTTP status
MAX_ATTEMPTS=30
ATTEMPT=0
@ -1289,6 +1322,21 @@ docker exec -i "$DEVBOX_NAME" bash <<EOF
EOF
}
reset_freeleaps_repo() {
echo "[INIT] $(date '+%Y-%m-%d %H:%M:%S') Resetting FreeLeaps repository..."
devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance"
DEVBOX_NAME=$(cat "$devbox_container_id_file_path")
docker exec -i "$DEVBOX_NAME" bash <<EOF
echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Resetting FreeLeaps repository..."
pushd /home/devbox/freeleaps > /dev/null
git config --global --add safe.directory /home/devbox/freeleaps
git reset --hard HEAD
popd > /dev/null
echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') FreeLeaps repository reset completed."
EOF
}
# :command.command_functions
# :command.function
devbox_init_command() {
@ -1942,28 +1990,23 @@ IS_START_FRONTEND=true
compile_backend_service
compile_frontend_service
docker exec -i "$DEVBOX_NAME" bash <<EOF
echo "[INFO] \$(date '+%Y-%m-%d %H:%M:%S') Reset git changes caused by compilation"
pushd /home/devbox/freeleaps
git config --global --add safe.directory /home/devbox/freeleaps
git reset --hard
EOF
reset_freeleaps_repo
# -------------------------------------------------------------------
# 10. Final notification
# -------------------------------------------------------------------
echo
echo "==========================================================="
log_info "DevBox init completed successfully!"
log_info "DevBox Environment Details:"
log_info "1. Code repository is located at: ${WORKING_HOME}/freeleaps"
log_info "2. Open up the frontend by visiting: http://localhost:${DEVBOX_FRONTEND_PORT}"
log_info "3. Log files can be viewed at:"
log_info " - Backend logs: ${WORKING_HOME}/logs/backend.logs"
log_info " - Frontend logs: ${WORKING_HOME}/logs/frontend.logs"
log_info "DevBox container ID: $WORKING_HOME/.devbox-instance"
log_info "Backend PID: $WORKING_HOME/.backend.pid"
log_info "Frontend PID: $WORKING_HOME/.frontend.pid"
echo "DevBox init completed successfully!"
echo "DevBox Environment Details:"
echo "1. Code repository is located at: ${WORKING_HOME}/freeleaps"
echo "2. Open up the frontend by visiting: http://localhost:${DEVBOX_FRONTEND_PORT}"
echo "3. Log files can be viewed at:"
echo " - Backend logs: ${WORKING_HOME}/logs/backend.logs"
echo " - Frontend logs: ${WORKING_HOME}/logs/frontend.logs"
echo "DevBox container ID: $WORKING_HOME/.devbox-instance"
echo "Backend PID: $WORKING_HOME/.backend.pid"
echo "Frontend PID: $WORKING_HOME/.frontend.pid"
echo "==========================================================="
echo
}
@ -2167,9 +2210,10 @@ devbox_deinit_command() {
fi
# Sleep 5 seconds to allow the services to stop, for each second echo 5 seconds increase from 1 to 5 in each second by -
for i in {1..5}; do
echo -n "="
sleep 1
echo -n "[INFO] $(date '+%Y-%m-%d %H:%M:%S') Stopping services"
for i in {1..10}; do
echo -n "."
sleep 0.5
done
echo
@ -2177,7 +2221,7 @@ devbox_deinit_command() {
# Remove the use-local-component file
rm -f "$WORKING_HOME/.use-local-component"
log_info "DevBox deinitialization completed."
exit_with_message "DevBox deinitialization completed." 0
}
# :command.function