# Common node machine
# [Workavia] Pinned to Node 22 (was `node:lts-alpine`). Node 24 broke
# the `config` package (Utils.isRegExp removed) — until tdrive bumps
# their `config` dep, we stay on 22 LTS.
FROM node:22-alpine AS node-base

### Install dependencies
RUN apk add --update-cache \
      ghostscript \
      graphicsmagick \
      curl \
    && rm -rf /var/cache/apk/*


### Install TDrive
WORKDIR /usr/src/app
COPY backend/node/package*.json ./

# Test Stage
FROM node-base AS test

RUN npm install --legacy-peer-deps
COPY backend/node/ .

# Add frontend Stage
FROM node-base AS installed-libs

COPY backend/node/ .
#Install dev dependancies for build
ENV NODE_ENV=development
RUN npm ci --legacy-peer-deps

#Build in production mode
ENV NODE_ENV=production
RUN npm run build
RUN rm -rf node_modules
#Install prod dependancies after build
RUN npm ci --legacy-peer-deps

# Development Stage
FROM installed-libs AS development

ENV NODE_ENV=development
RUN npm install -g pino-pretty && \
    npm install -g tsc-watch && \
    npm ci --legacy-peer-deps
CMD ["npm", "run", "dev:debug"]

# Production Stage
FROM installed-libs AS production

EXPOSE 4000
CMD ["npm", "run", "serve"]