#!/usr/bin/env bash
# nerf-nx-run -- Run an Nx target on a project in project:target format (e.g. myapp:build)
# Generated from nx manifest. Do not edit directly.
# nerf:threat:read=workspace
# nerf:threat:write=workspace

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-nx-run <target>

Arguments:
  <target> (required)
      Target in project:target format (e.g. myapp:build, myapp:test, myapp:lint)
      Must match: ^[a-zA-Z0-9_-]+:[a-zA-Z0-9_-]+$

Maps to: npx nx run <target>

Run an Nx target on a project in project:target format (e.g. myapp:build).
EOF
  exit 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

TARGET="${1:-}"
shift 2>/dev/null || true

if [[ -n "${TARGET}" ]] && [[ "${TARGET}" == -* ]]; then
  echo "error: nerf-nx-run: <target> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${TARGET}" ]]; then
  echo "error: nerf-nx-run: missing required argument <target>" >&2
  echo "  hint: provide a value for <target>" >&2
  usage
fi

_NERF_PATTERN='^[a-zA-Z0-9_-]+:[a-zA-Z0-9_-]+$'
if [[ -n "${TARGET}" ]] && ! [[ "${TARGET}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-nx-run: argument <target> does not match required pattern" >&2
  echo "  value:   \"${TARGET}\"" >&2
  echo "  pattern: ^[a-zA-Z0-9_-]+:[a-zA-Z0-9_-]+$" >&2
  echo "  hint: value must match ^[a-zA-Z0-9_-]+:[a-zA-Z0-9_-]+$" >&2
  exit 1
fi

test -f nx.json > /dev/null 2>&1 || { echo 'error: nerf-nx-run: Not in an Nx workspace root (nx.json not found). Run from the repo root.' >&2; exit 1; }

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: npx nx run "${TARGET}""
  exit 0
fi

exec npx nx run "${TARGET}"
