#!/bin/bash

# Array of known potentially hanging packages
hanging_packages=("xmlhttprequest-ssl@latest" "engine.io-parser@latest")

# Timeout in seconds for each package installation attempt


install_with_timeout() {
    package=$1
    echo "Attempting to install $package with a timeout of $timeout_duration seconds."
    # Start npm install in the background
    npm install $package --verbose &> $package.log &

    # Get PID of the npm process
    pid=$!

    # Wait for the npm process to finish or timeout
    (sleep $timeout_duration && kill -0 $pid 2>/dev/null && kill -9 $pid && echo "Timeout reached for $package, process killed." && echo $package >> exclude.log) &
    waiter_pid=$!

    # Wait for the npm process to complete
    wait $pid

    # Kill the waiter process in case npm finished before the timeout
    kill -0 $waiter_pid 2>/dev/null && kill -9 $waiter_pid
}

# Install potentially hanging packages first with a timeout
for package in "${hanging_packages[@]}"; do
    install_with_timeout $package
done
