# --------------> The frontend compiler image
FROM node:18 AS frontend_compile
WORKDIR /usr/src/app/apps/ui
COPY ./tsconfig.base.json /usr/src/app
COPY ./apps/ui ./
RUN npm ci
RUN npm run build

# --------------> The backend compiler image
FROM node:18 AS backend_compile
WORKDIR /usr/src/app/apps/platform
COPY ./tsconfig.base.json /usr/src/app
COPY ./apps/platform ./
RUN npm ci
RUN npm run build

# --------------> The build image
FROM node:18 AS build
WORKDIR /usr/src/app
COPY --from=backend_compile /usr/src/app/apps/platform/package*.json ./
COPY --from=backend_compile /usr/src/app/apps/platform/build ./build
COPY --from=backend_compile /usr/src/app/apps/platform/db ./db
COPY --from=backend_compile /usr/src/app/apps/platform/public ./public 
COPY --from=frontend_compile /usr/src/app/apps/ui/build ./public
RUN npm ci --only=production

# --------------> The production image
FROM node:18-alpine
RUN apk add dumb-init
ENV NODE_ENV="production"
USER node
WORKDIR /usr/src/app
COPY --chown=node:node --from=build /usr/src/app ./
EXPOSE 80
CMD ["dumb-init", "node", "build/boot.js"]

