# Multi-stage Dockerfile to build a monorepo project (CLI and Electron-based desktop apps) from source
# Stage 1: Build
FROM node:lts-bookworm AS builder
# Default: do not build the renderer to avoid known PostCSS/Tailwind export issue
ARG BUILD_RENDERER=false
WORKDIR /workspace
# Install build tooling
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl git && rm -rf /var/lib/apt/lists/*
RUN npm i -g pnpm
# Copy the entire repository
COPY . .
# Install workspace dependencies at the root (pnpm will hoist as needed)
RUN pnpm i
# Build CLI
RUN bash -lc "cd apps/cli && pnpm i && pnpm run build"
# Build Electron main (desktop)
RUN bash -lc "cd apps/desktop/layer/main && pnpm i && pnpm run build"
# Prepare renderer dist directory; it may be empty if not building renderer
RUN mkdir -p /workspace/apps/desktop/layer/renderer/dist
# Conditionally build the renderer (disabled by default to avoid known issues in some environments)
RUN if [ "$BUILD_RENDERER" != "false" ]; then \
bash -lc "cd apps/desktop/layer/renderer && pnpm i && pnpm run build:web"; \
fi
# Stage 2: Runtime image
FROM node:lts-bookworm
# Create a non-root user for running the app
RUN groupadd -r app && useradd -r -g app app
WORKDIR /app
# Copy built artifacts
COPY --from=builder /workspace/apps/cli/dist /app/cli/dist
COPY --from=builder /workspace/apps/cli/node_modules /app/cli/node_modules
COPY --from=builder /workspace/apps/desktop/layer/main/dist /app/desktop/main/dist
COPY --from=builder /workspace/apps/desktop/layer/renderer/dist /app/desktop/renderer/dist
# Ensure correct permissions for the non-root user
RUN chown -R app:app /app
USER app
# Default command: run the CLI
CMD ["node", "/app/cli/dist/index.js"]