FROM node:14 AS install

ARG NPM_TOKEN

# Install dependencies
ADD package.json /tmp/package.json
ADD .npmrc /tmp/.npmrc
ADD yarn.lock /tmp/yarn.lock
RUN cd /tmp && yarn

# Set working directory
WORKDIR /app

# Copy sources
COPY . .

#Copy dependencies
RUN cp -a /tmp/node_modules .

# Set working directory
WORKDIR /app

# Build app
RUN yarn build

FROM nginx:latest AS final

ARG NPM_TOKEN

# Copy published application from build image to final image
COPY --from=install /app/nginx/conf.d /etc/nginx/conf.d
COPY --from=install /app/nginx/nginx.conf /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf
COPY --from=install /app/dist /usr/share/nginx/html

# Set application working directory and specify ports and entrypoint 
WORKDIR /usr/share/nginx/html
EXPOSE 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]