FROM node:alpine AS build

WORKDIR /app

COPY package.json package-lock.json ./
COPY packages/shared/package.json ./packages/shared/
COPY apps/server/package.json ./apps/server/

RUN npm ci -w @calchat/server -w @calchat/shared --include-workspace-root

COPY packages/shared/ packages/shared/
COPY apps/server/ apps/server/

RUN npm run build -w @calchat/shared && npm run build -w @calchat/server

FROM node:alpine

WORKDIR /app

COPY --from=build /app/package.json /app/package-lock.json ./
COPY --from=build /app/packages/shared/package.json packages/shared/
COPY --from=build /app/apps/server/package.json apps/server/

RUN npm ci --omit=dev -w @calchat/server -w @calchat/shared

COPY --from=build /app/packages/shared/dist/ packages/shared/dist/
COPY --from=build /app/apps/server/dist/ apps/server/dist/

EXPOSE 3001

CMD ["node", "apps/server/dist/app.js"]
