stages:
  - deploy

publish_npm:
  image: "node:20.18-alpine3.19"
  stage: deploy
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  before_script:
    - apk --no-cache add git
    - git submodule update --remote --merge
    - apk add --update make bash protobuf-dev 
    - cd ./config && npm i && cd ../

  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      changes:
        - config/package.json
  script:
    - make js-client
    - cd ./config
    - |
      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_NAME=$(node -p "require('./package.json').name")
    - NPM_PACKAGE_CURRENT_VERSION=$(npm show ${NPM_PACKAGE_NAME} version)
    - npm pub
    - NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")
    - echo "Successfully published version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} to GitLab's NPM registry"