FROM node:20-alpine AS build

WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . .
RUN npm run build
RUN npm prune --omit=dev

FROM node:20-alpine AS runner

ENV NODE_ENV=production
ENV PORT=5173
ENV NODEDC_LAUNCHER_STORAGE_DIR=/app/server/storage
ENV NODEDC_LAUNCHER_UPLOADS_DIR=/app/server/storage/uploads

WORKDIR /app

COPY --from=build /app/package.json /app/package-lock.json ./
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/server ./server
COPY --from=build /app/dist ./dist
COPY --from=build /app/public ./public

RUN mkdir -p /app/server/storage/uploads /app/dist/storage/uploads /app/public/storage/uploads \
  && chown -R node:node /app/server/storage /app/dist/storage /app/public/storage

USER node

EXPOSE 5173

CMD ["node", "server/dev-server.mjs"]
