default:
  image: amio/node-chrome

stages:
  - fetch
  - deps
  - check
  - build
  - test
  - deploy
  - benchmark

submodule_fetch:
  stage: fetch
  script:
    - chmod 600 .drawing-app-deploy.rsa
    - git submodule sync --recursive
    - GIT_SSH_COMMAND='ssh -i '`pwd`'/.drawing-app-deploy.rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' git submodule update --init --recursive
  artifacts:
    paths:
      - src/liowebrtc
      - src/rtcpeerconnection
      - src/signalbuddy
      - src/yjs
      - src/drawing-crdt/pkg

npm_install_prod:
  stage: deps
  dependencies:
    - submodule_fetch
  script:
    - npm ci --only=prod
  artifacts:
    paths:
      - node_modules
      - src/liowebrtc
      - src/rtcpeerconnection
      - src/signalbuddy
      - src/yjs
      - src/drawing-crdt/pkg

npm_install:
  stage: deps
  dependencies:
    - submodule_fetch
  script:
    - npm ci
  artifacts:
    paths:
      - node_modules
      - src/liowebrtc
      - src/rtcpeerconnection
      - src/signalbuddy
      - src/yjs
      - src/drawing-crdt/pkg

format_check:
  stage: check
  dependencies:
    - npm_install
  script:
    - npm run format-check

lint:
  stage: check
  dependencies:
    - npm_install
  script:
    - npm run lint

build:
  stage: build
  dependencies:
    - npm_install
  script:
    - npm run build
    - gcc -E -P -traditional-cpp -o /dev/stdout -DFILES_TO_CACHE_LIST=`find public/ -type f "!" -iname service-worker.js -and "!" -name '.*' | cut -c6- | sed 's_._"_' | sed 's/$/",/' | sort | tr -d '\n'` - < public/service-worker.js | npx prettier --parser babel > public/service-worker.js
  artifacts:
    paths:
      - public/

test:
  stage: test
  dependencies:
    - npm_install
  script:
    - npm run test

chrome_test:
  stage: test
  dependencies:
    - npm_install
    - build
  script:
    - npm run start-bg
    - npm run test-e2e

deploy:
  stage: deploy
  dependencies:
    - npm_install_prod
    - build
  only:
    - master
  script:
    - apt-get update -qq && apt-get install -qq zip
    - zip -r application.zip *
    - curl -X POST -u "$DEPLOYMENT_USERNAME:$DEPLOYMENT_PASSWORD" $DEPLOYMENT_ENDPOINT -T application.zip

benchmark:
  stage: benchmark
  dependencies:
    - npm_install
  only:
    - master
    - rust-wasm-crdt
  script:
    - apt-get -y install gnuplot
    - npm run build:bench
    - cp __benchmarks__/benchmarks.html public/benchmarks.html
    - npm run start-bg
    - npm run benchmarks
    - npm run plot
  artifacts:
    paths:
      - plots/
