#!/bin/sh
# Lo más parecido a un runner que se puede tener sin GitHub: la imagen oficial
# de Node, sin las herramientas del curso. Instala make y git, copia Kallpa,
# declara las herramientas como en esperado/ y corre npm ci y make check.
# Se corre en la terminal de su computadora, donde está Docker:
#
#   sh runner-limpio.sh                      todas las herramientas declaradas
#   sh runner-limpio.sh @eslint/js           sin declarar ese paquete
#
# Dentro del contenedor del curso este error no se ve: los import encuentran
# los paquetes de la imagen aunque falten en package.json.
HERE="$(cd "$(dirname "$0")" && pwd)"
docker run --rm -v "$HERE/../..:/curso:ro" -e FALTA="${1:-}" node:26-trixie-slim bash -c '
  apt-get update -qq >/dev/null && apt-get install -y -qq --no-install-recommends make >/dev/null 2>&1
  cp -r /curso/kallpa /tmp/kallpa && cd /tmp/kallpa
  cp /curso/code/week-12/esperado/package.json /curso/code/week-12/esperado/package-lock.json .
  export PATH="$PWD/node_modules/.bin:$PATH"
  if [ -n "$FALTA" ]; then
    echo "\$ npm uninstall --no-audit --no-fund $FALTA | grep -E \"^(added|removed)\""
    npm uninstall --no-audit --no-fund "$FALTA" 2>&1 | grep -E "^(added|removed)"
  else
    echo "\$ npm ci --no-audit --no-fund | grep -E \"^added\""
    npm ci --no-audit --no-fund 2>&1 | grep -E "^added"
  fi
  echo "\$ node --version; which tsc"
  node --version; which tsc
  echo "\$ make check | grep -v -E \"^ *(✔|▶)\" | cut -c1-110"
  make check 2>&1 | grep -v -E "^ *(✔|▶)" | cut -c1-110
  echo "[código de salida: ${PIPESTATUS[0]}]"
'
