# syntax=docker/dockerfile:1
# VitePress production — static build served by nginx
FROM node:24-alpine AS builder

# Corepack enabled; pnpm version comes from package.json's packageManager
# field via `corepack install` below. No drift between Docker and host.
RUN corepack enable

WORKDIR /app
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc* ./
RUN corepack install
COPY apps/docs/ ./apps/docs/
COPY packages/ ./packages/

# Optional pnpm patches/ dir — bind-mount and conditional copy so the
# build doesn't fail when the project doesn't use pnpm patches. Must
# run BEFORE `pnpm install` for patchedDependencies resolution.
RUN --mount=type=bind,source=.,target=/ctx,readonly \
    if [ -d /ctx/patches ]; then \
      mkdir -p ./patches && cp -r /ctx/patches/. ./patches/; \
    fi

RUN pnpm install --frozen-lockfile

# APP_NAME = pnpm workspace package name (e.g., "@numero/docs"), used as
# the --filter selector. APP_DIR = on-disk directory name under apps/
# (e.g., "docs"), used in COPY paths. Two values because the package
# name and directory name aren't always the same in pnpm workspaces.
# ce auto-passes both (APP_NAME from package.json#name, APP_DIR from
# path.basename(contract.location)).
#
# Fail-loud guard: pnpm --filter exits 0 on a zero-match filter, which
# produces a confusing "dist not found" error two stages later instead
# of a "this arg wasn't set" error here.
ARG APP_NAME
ARG APP_DIR
RUN test -n "${APP_NAME}" \
  || (echo "ERROR: APP_NAME build-arg required for vitepressprod template" && exit 1)
RUN test -n "${APP_DIR}" \
  || (echo "ERROR: APP_DIR build-arg required for vitepressprod template" && exit 1)
RUN pnpm --filter "${APP_NAME}" build

FROM nginx:alpine
# Build args are stage-scoped — must redeclare to use them in COPY.
ARG APP_DIR
COPY --from=builder /app/apps/${APP_DIR}/.vitepress/dist /usr/share/nginx/html
EXPOSE 80
