# Start with a minimal Node.js base image
FROM node:21-alpine as base

# Install pnpm globally with caching to avoid reinstalling if nothing has changed
RUN npm i -g pnpm

# Set the working directory
WORKDIR /home/app

# Copy only package-related files to leverage caching
COPY --link ./scripts/package.json ./scripts/pnpm-lock.yaml ./scripts/
COPY --link ./shared/package.json ./shared/pnpm-lock.yaml ./shared/
COPY --link ./consumer/package.json ./consumer/pnpm-lock.yaml ./consumer/

# Install dependencies for each package
WORKDIR /home/app/scripts
RUN pnpm install --frozen-lockfile

WORKDIR /home/app/shared
RUN pnpm install --frozen-lockfile

WORKDIR /home/app/consumer
RUN pnpm install --frozen-lockfile

# Now copy the rest of the source files
WORKDIR /home/app

COPY --link ../scripts ./scripts
COPY --link ../shared ./shared
COPY --link ../consumer ./consumer

# Build the consumer
WORKDIR /home/app/consumer

RUN pnpm run build_all

# Start the application
CMD ["node", "/home/app/consumer/dist/consumer/src/index.js"]