FROM        086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:node24 AS prod-deps
USER        root
WORKDIR     /var/app
RUN         npm install -g pnpm@10.33.0
COPY        package.json pnpm-lock.yaml ./

##### dev-deps #####
# Install dev dependencies, and copy in source.
####################
FROM        prod-deps AS dev-deps
WORKDIR     /var/app
COPY        src ./src
COPY        package.json pnpm-lock.yaml .npmrc ./
RUN         --mount=type=secret,id=GITHUB_NPM_TOKEN,required=true,uid=1000 GITHUB_NPM_TOKEN=$(cat /run/secrets/GITHUB_NPM_TOKEN) pnpm install


##### dev #######
# Run as a hot-reloading service.
#################
FROM        dev-deps AS dev
WORKDIR     /var/app
COPY        nodemon.json ./
ENV         PORT 8080
EXPOSE      8080
ENTRYPOINT  ["pnpm", "run", "dev"]


##### unit-lint #####
# Install unit testing and linting configs
# and configure to run tests
#########################
FROM        dev-deps AS unit-lint
WORKDIR     /var/app
COPY        eslint.config.js jest.config.js ./
COPY        tests ./tests
RUN         node --version

ENTRYPOINT  ["pnpm", "run", "test"]


##### integration-tests #####
# Install unit testing and linting configs
# and configure to run tests
#############################
FROM        unit-lint AS integration-tests
COPY        integration_tests ./integration_tests
COPY        jest.config.js ./
ENTRYPOINT ["pnpm", "run", "test:integration"]


##### deploy #####
# Grab production node dependencies and
# production build artifacts
# and run as a service.
##################
FROM        prod-deps AS deploy
WORKDIR     /var/app
# Copy app and install production dependencies
COPY        src ./src
COPY        package.json pnpm-lock.yaml .npmrc ./
RUN         --mount=type=secret,id=GITHUB_NPM_TOKEN,required=true,uid=1000 GITHUB_NPM_TOKEN=$(cat /run/secrets/GITHUB_NPM_TOKEN) pnpm install

ENV         PORT 8080
EXPOSE      8080
CMD         ["pnpm", "run", "start"]
