FROM node:20 AS install

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY .yarn .yarn
COPY .yarnrc.yml package.json yarn.lock ./
COPY frontend/package.json frontend/package.json
COPY shared/package.json shared/package.json
COPY graphql/package.json graphql/package.json

RUN corepack enable && yarn install --immutable --inline-builds

FROM install AS build

# Bundle app source
COPY . .

RUN yarn workspace @inject/frontend prebuild

RUN yarn workspace @inject/frontend build

FROM nginx:1-alpine

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /usr/src/app/dist /usr/app
COPY ./frontend/substituteEnv.sh /docker-entrypoint.d/01-substitute-env.sh

ENV NGINX_DEFAULT_REQUEST=index.html \
    NGINX_ROOT=dist

EXPOSE 80