FROM node:lts-alpine AS builder

WORKDIR /app
COPY package.json package.json
COPY packages/ ./packages/
RUN npm install

COPY js/utils/preload.js ./js/utils/preload.js
COPY js/utils/translator.js ./js/utils/translator.js

ARG PRELOAD_SRC_LANG=""
ARG PRELOAD_TARGET_LANG=""

RUN if [ -n "$PRELOAD_SRC_LANG" ] ; then \
    node js/utils/preload.js $PRELOAD_SRC_LANG en; \
    fi

RUN if [ -n "$PRELOAD_TARGET_LANG" ]; then \
    node js/utils/preload.js en $PRELOAD_TARGET_LANG; \
    fi

FROM alpine:latest

RUN apk update && \
    apk add --no-cache nodejs curl

WORKDIR /app

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /root/.cache/mtran /root/.cache/mtran

COPY js ./js
COPY package.json ./package.json
COPY start.sh ./start.sh
RUN chmod +x ./start.sh

EXPOSE 8989

ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=8989


CMD ["./start.sh"]
