image: jsii/superchain:latest
.cache:
  cache:
    untracked: true
    key:
      files:
        - package-lock.json
    paths:
      - node_modules/
      - .npm/
    policy: pull

stages:
  - prepare
  - test
  - deploy
  - publish

include:
  - project: 'affinidi/foundational/foundational-pipeline'
    file: '/nodejs.yml'

prepare:
  stage: prepare
  extends: .node-base
  script:
    - npm i --cache .npm --prefer-offline
  cache:
    untracked: true
    key:
      files:
        - package-lock.json
    paths:
      - node_modules/
      - .npm/

test:
  stage: test
  rules:
    - if: '$SKIP_TEST == "true"'
      when: never
  script:
    - mv $TEST_ENV_FILE .env
    - mv $TEST_PRIVATE_KEY_DEV privateKey_dev.pem
    - npm i
    - npm run build
    - npm run test

pages:
  stage: deploy
  only:
    - main
  extends:
    - .node-public-publisher
    - .cache
  script:
    - cp -R doc public
    - cp -R doc public/doc
  artifacts:
    paths:
      - public

# publishes to npmjs.org with beta tag
publish:
  when: manual
  allow_failure: false
  stage: publish
  only:
    - tags
  script:
    - npm i
    - npm run build && npm run package
    - npm config set //registry.npmjs.org/:_authToken=$NPM_REGISTRY_TOKEN
    - npm publish --access public --tag beta
    - twine upload  -r testpypi dist/python/*  -u $PYPI_USERNAME -p $TEST_PYPI_TOKEN
    - cd dist/dotnet/
    - for f in *.nupkg; do mv -- "$f" "${f%.nupkg}-beta.nupkg"; done
    - for f in *.snupkg; do mv -- "$f" "${f%.snupkg}-beta.snupkg"; done
    - cd ../../
    - dotnet nuget push dist/dotnet/*-beta.nupkg -k "${NUGET_TOKEN}" -s https://api.nuget.org/v3/index.json --skip-duplicate

# tag current verson to npmjs.org with latest tag
publish-production:
  when: manual
  allow_failure: false
  stage: publish
  only:
    - tags
  script:
    - npm i
    - npm run build && npm run package
    - npm config set //registry.npmjs.org/:_authToken=$NPM_REGISTRY_TOKEN
    - npm dist-tag add @affinidi/common-check-widget-backend-lib@$CI_COMMIT_TAG latest
    - twine upload dist/python/* -u $PYPI_USERNAME -p $PYPI_TOKEN
    - dotnet nuget push dist/dotnet/*.nupkg -k "${NUGET_TOKEN}" -s https://api.nuget.org/v3/index.json --skip-duplicate
