# Plugins, installed into an image built from the app image, which migrate, server, and worker
# run. List them in .env as PLUGINS, npm packages at exact versions separated by spaces
# (PLUGINS=@newses/plugin-webhook@1.0.0), and this file beside compose.yaml in COMPOSE_FILE
# (COMPOSE_FILE=compose.yaml:compose.plugins.yaml), so every docker compose command in this folder
# includes it; then build and start:
#   docker compose up -d --build
# Changing PLUGINS and running that again upgrades or removes them; nothing is installed while the
# containers run. Plugins run inside the server with access to its data: install only those you
# trust. Needs Docker Compose 2.24.4 or later. In a checkout, build the app image first
# (docker compose -f compose.yaml build), which the plugins' image is built from.
# To try a plugin before it is published, pack it (npm pack) into a plugins folder beside this
# file and list its path in PLUGINS, such as ./plugins/newses-plugin-webhook-1.0.0.tgz; packing it
# again and running the same command installs the new build.
x-plugins: &plugins
  image: newses-plugins:local
  pull_policy: build
  build: !override
    context: .
    dockerfile_inline: |
      ARG NEWSES_IMAGE
      # Packed plugins, when there is a plugins folder; mounted to install, never in the image.
      FROM scratch AS packed
      COPY plugin[s] /plugins/
      FROM $${NEWSES_IMAGE}
      ARG PLUGINS
      USER root
      RUN --mount=type=bind,from=packed,target=/compose cd /compose \
        && node /app/server/src/plugins/install.ts --dir /app/server/.plugins --link $${PLUGINS} \
        && node /app/shared/src/third-party-licenses.ts --node-modules /app/node_modules \
          --node-modules /app/server/.plugins/node_modules \
          --out /app/server/src/third-party-licenses.json
      USER node
    args:
      NEWSES_IMAGE: ${NEWSES_IMAGE:-public.ecr.aws/d5a4a9x3/newses:0.3.14@sha256:c836b22db8052446e694fdfb9c5a32bda00a82ae3a6948a2b612d8330a167816}
      PLUGINS: ${PLUGINS:?list the plugins in PLUGINS in .env}

services:
  migrate:
    <<: *plugins
  server:
    <<: *plugins
  worker:
    <<: *plugins
