<h1>YANZ UPLOAD ANJG</h1>#!/bin/bash
set -euo pipefail

export NVM_DIR="$HOME/.nvm"

LOCK_FILE="$HOME/.nvm-install.lock"
COMPLETE_MARKER="$NVM_DIR/.install_complete"

# Concurrent runs in the same container (two git deploys of the same
# website, the apps API install racing the appinit startup install, ...)
# used to pass the "nvm.sh exists" guard together and then race on the
# same $NVM_DIR: parallel git clones aborted/corrupted the tree, parallel
# global yarn installs corrupted the npm prefix, and a late runner could
# report "NVM already installed" while another run was still installing
# node (a false success for its caller). Serialise them with a lock.
exec 9>"$LOCK_FILE"
if ! flock -w 3600 9; then
  echo "⏳ Timed out waiting for another nvm install to finish" >&2
  exit 1
fi

nvm_loads() {
   [ -s "$NVM_DIR/nvm.sh" ] || return 1
  # shellcheck disable=SC1091
  \. "$NVM_DIR/nvm.sh" >/dev/null 2>&1 || return 1
  nvm --version >/dev/null 2>&1
}

# Re-check under the lock: a concurrent run may have finished while we
# waited for it. Only trust a finished run if it marked itself complete.
if [ -f "$COMPLETE_MARKER" ] && nvm_loads; then
  echo "NVM already installed"
  exit 0
fi

# Without a completion marker, an existing $NVM_DIR whose nvm does not
# load is debris from a killed run: the git clone below would refuse the
# non-empty directory and the install could never recover, so clear it
# and start clean.
if [ -d "$NVM_DIR" ] && ! nvm_loads; then
  echo "🧹 Clearing incomplete nvm installation..."
  rm -rf "$NVM_DIR"
fi

echo "🔍 Fetching latest nvm release version..."
NVM_VERSION=$(curl -s https://api.github.com/repos/nvm-sh/nvm/releases/latest \
  | grep '"tag_name":' \
  | sed -E 's/.*"v([^"]+)".*/\1/')

echo "➡️ Latest nvm version is v$NVM_VERSION"

echo "📥 Downloading and installing nvm v$NVM_VERSION..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION}/install.sh | bash

echo "🔄 Loading nvm into current shell..."

# shellcheck disable=SC1091
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

echo "📦 Installing stable Node.js..."
nvm install 26

echo "🧶 Installing Yarn..."
if command -v yarn >/dev/null 2>&1; then
  echo "Yarn already installed"
else
  npm install -g yarn
fi

echo "✅ Installation complete!"
node -v
npm -v
yarn --version

# Mark the install complete only after every check above passed, so the
# "already installed" fast path can never describe a half-installed tree.
