# stage: dev
FROM node:15-alpine as dev
WORKDIR /app

COPY package*.json ./
RUN npm ci
COPY . .

# stage: build
FROM dev as build
RUN npm prune --production

# stage: production
FROM node:15-alpine as production
WORKDIR /app

RUN printf "%b" '#!'"/bin/sh\n\
set -e\n\
if [ ! -z \"\$RUN_MIGRATIONS\" ]; then\n\
    echo \"Running migrations.\"\n\
    npm run knex:migrate:latest\n\
fi\n\
exec \"\$@\"\n" > docker-entrypoint.sh && chmod +x docker-entrypoint.sh

COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/src ./src
COPY --from=build /app/*.json ./
COPY --from=build /app/*.ts ./

ENTRYPOINT [ "./docker-entrypoint.sh" ]
CMD [ "npm", "start" ]