#!/bin/bash -i
# Resolve all frontend dependencies that the application requires to develop.

# Stop on errors
set -e

cd "$(dirname "$0")/.."

# Install/upgrade node when inside devcontainer
if [[ -n "$DEVCONTAINER" ]]; then
    nodeCurrent=$(nvm version default || :)
    nodeLatest=$(nvm version-remote "$(cat .nvmrc)")
    if [[ -z "$nodeCurrent" || "$nodeCurrent" == "N/A" ]]; then
        nvm install
    elif [[ "$nodeCurrent" != "$nodeLatest" ]]; then
        nvm install --reinstall-packages-from="$nodeCurrent" --default
        nvm uninstall "$nodeCurrent"
    fi

    # install pnpm if not already available
    if ! command -v pnpm &> /dev/null; then
        npm install -g corepack
        corepack enable pnpm
    fi
fi

if ! command -v pnpm &> /dev/null; then
    echo "Error: pnpm not found. Please install it following the official instructions: https://pnpm.io/installation" >&2
    exit 1
fi
# Install node modules
pnpm install
