♻️ refactor(config): Simplify environment variable handling and improve build script

This commit is contained in:
web@ppanel
2024-12-26 22:23:45 +07:00
parent 087c36ce48
commit cf54d0f3a2
4 changed files with 131 additions and 161 deletions
+5 -32
View File
@@ -19,29 +19,6 @@ bun install || {
exit 1
}
# Function to extract variables from .env.template
extract_env_variables() {
local TEMPLATE_PATH=$1
local DEFAULT_PORT=$2
local ENV_VARS=" NODE_ENV: 'production'," # Start with NODE_ENV
ENV_VARS="$ENV_VARS\n PORT: $DEFAULT_PORT," # Add default port
if [[ -f $TEMPLATE_PATH ]]; then
while IFS= read -r line; do
# Ignore empty lines and comments
if [[ ! -z "$line" && ! $line =~ ^# ]]; then
VAR_NAME=$(echo $line | cut -d'=' -f1)
VAR_VALUE=$(echo $line | cut -d'=' -f2-)
ENV_VARS="$ENV_VARS\n $VAR_NAME: '$VAR_VALUE'," # Add new line for each variable
fi
done < "$TEMPLATE_PATH"
fi
# Remove the trailing comma
ENV_VARS=${ENV_VARS%,}
echo -e "$ENV_VARS"
}
# Step 2: Build each project using Turbo
for ITEM in "${PROJECTS[@]}"; do
IFS=":" read -r PROJECT PROJECT_PATH DEFAULT_PORT <<< "$ITEM"
@@ -50,16 +27,12 @@ for ITEM in "${PROJECTS[@]}"; do
echo "Build failed for $PROJECT"
exit 1
}
# Extract environment variables
ENV_TEMPLATE_PATH="$PROJECT_PATH/.env.template"
ENV_VARS=$(extract_env_variables "$ENV_TEMPLATE_PATH" "$DEFAULT_PORT")
# Copy build output and static resources to the build directory
PROJECT_BUILD_DIR=$OUT_DIR/$PROJECT
cp -r $PROJECT_PATH/.next/standalone/. $PROJECT_BUILD_DIR/
cp -r $PROJECT_PATH/.next/static $PROJECT_BUILD_DIR/$PROJECT_PATH/.next/
cp -r $PROJECT_PATH/public $PROJECT_BUILD_DIR/$PROJECT_PATH/
cp -r $PROJECT_PATH/.env.template $PROJECT_BUILD_DIR/$PROJECT_PATH/.env
# Generate ecosystem.config.js for the project
ECOSYSTEM_CONFIG="$PROJECT_BUILD_DIR/ecosystem.config.js"
@@ -70,11 +43,11 @@ module.exports = {
name: "$PROJECT",
script: "$PROJECT_PATH/server.js",
interpreter: "bun",
watch: ["$PROJECT_PATH"],
watch: true,
instances: "max",
exec_mode: "cluster",
env: {
$ENV_VARS
PORT: $DEFAULT_PORT
}
}
]
@@ -84,7 +57,7 @@ EOL
# Create a tar.gz archive for each project
ARCHIVE_NAME="$OUT_DIR/$PROJECT.tar.gz"
tar -czvf $ARCHIVE_NAME -C $PROJECT_BUILD_DIR . || {
tar -czvf $ARCHIVE_NAME -C $OUT_DIR $PROJECT || {
echo "Archiving failed for $PROJECT"
exit 1
}
@@ -92,4 +65,4 @@ EOL
done
# Final output
echo "All projects have been built, archived, and individual PM2 configuration files generated in their respective directories."
echo "All projects have been built and archived successfully."