stages:
  - publish-npm

publish_npm:
  image: "node:current-alpine"
  stage: publish-npm
  before_script:
    - apk add --update bash
    - apk add --update make
    - apk add --update yarn
    - apk add --update jq
    - apk add --update rsync
    - apk add --update protobuf-dev
    - apk add --update protobuf-dev
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - config/package.json
  script:
    - npm install -g typescript@4.7.4
    - npm install -g ts-proto@1.115.5
    - mkdir clients
    - make proto
    - ls
    - cd ./config
    - rsync -a --prune-empty-dirs ../clients ./
    - |
      if [[ ! -f .npmrc ]]; then
        echo 'No .npmrc found! Creating one now. Please review the following link for more information: https://docs.gitlab.com/ee/user/packages/npm_registry/index.html#project-level-npm-endpoint-1'
        {
          echo "@perxis-js:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
          echo "${CI_API_V4_URL#http*:}/packages/npm/:_authToken=\${CI_JOB_TOKEN}"
          echo "${CI_API_V4_URL#http*:}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=\${CI_JOB_TOKEN}"
        } >> .npmrc
      fi
    - NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")
    - yarn pub
    - echo "Successfully published version ${NPM_PACKAGE_VERSION} to GitLab's NPM registry"
    - rm -rf ./clients
    - cd ..
    - rm -rf ./clients