diff --git a/main-installer.sh b/main-installer.sh index aa27cf3..41c69a2 100755 --- a/main-installer.sh +++ b/main-installer.sh @@ -295,10 +295,8 @@ EOF exit 1 } - # Install npm dependencies - log "INFO" "Updating npm dependencies..." - cd "$SCRIPT_DIR" - npm install || { + # Install npm dependencies using our common function + ensure_npm_packages "$INSTALL_DIR" || { log "ERROR" "NPM installation failed" exit 1 } @@ -393,10 +391,8 @@ else exit 1 } - # Step 6: Install npm dependencies - log "INFO" "Installing npm dependencies..." - cd "$SCRIPT_DIR" - npm install || { + # Step 6: Install npm dependencies using our common function + ensure_npm_packages "$INSTALL_DIR" || { log "ERROR" "NPM installation failed" exit 1 } diff --git a/modules/utils-module.sh b/modules/utils-module.sh index 7db8ccf..e52fc0a 100644 --- a/modules/utils-module.sh +++ b/modules/utils-module.sh @@ -97,6 +97,48 @@ function create_dir_if_not_exists() { } # Function to finalize the setup (permissions, etc.) +# Function to ensure NPM packages are properly installed +function ensure_npm_packages() { + local install_dir=$1 + + # Ensure package.json exists in the installation directory + if [ ! -f "$install_dir/package.json" ]; then + log "INFO" "Copying package.json to installation directory..." + cp "$SCRIPT_DIR/package.json" "$install_dir/package.json" || { + log "ERROR" "Failed to copy package.json to installation directory" + return 1 + } + fi + + # Install NPM packages if not already installed or if it's an update + if [ ! -d "$install_dir/node_modules" ] || [ "$IS_UPDATE" = "true" ]; then + log "INFO" "Installing NPM packages in $install_dir..." + + # Save current directory + local current_dir=$(pwd) + + # Change to install directory and install packages + cd "$install_dir" || { + log "ERROR" "Failed to change to installation directory: $install_dir" + return 1 + } + + npm install || { + log "ERROR" "NPM installation failed in $install_dir" + cd "$current_dir" # Return to original directory + return 1 + } + + # Return to original directory + cd "$current_dir" + log "INFO" "NPM packages successfully installed in $install_dir" + else + log "INFO" "NPM packages appear to be already installed in $install_dir, skipping" + fi + + return 0 +} + function finalize_setup() { log "INFO" "Setting up final permissions and configurations..." @@ -139,9 +181,10 @@ function finalize_setup() { create_dir_if_not_exists "$MEDIA_DIR/magazines" "$USER:$USER" fi - # Install NPM packages - log "INFO" "Installing NPM packages..." - cd $INSTALL_DIR && npm install + # Install npm packages + ensure_npm_packages "$INSTALL_DIR" || { + log "ERROR" "Failed to install NPM packages" + } # Handle configuration file if ! update_config_file "$CONFIG_DIR/config.json" "$IS_UPDATE"; then