image: ${CI_REGISTRY}/devops/images/usgs/node:20

# Do not run for merge requests
workflow:
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH

stages:
  - init
  - build
  - test
  - publish

default:
  tags:
    - build

####
# Stage: init
####

Init:
  artifacts:
    paths:
      - node_modules
      - example/node_modules
  script:
    - npm ci
    - cd example
    - npm ci
  stage: init

####
# Stage: Build
####

Audit:
  allow_failure: true
  needs: []
  script:
    - npm audit
  stage: build

Audit Angular:
  allow_failure: true
  needs: []
  script:
    - cd example
    - npm audit
  stage: build

Build Angular:
  needs:
    - Init
  script:
    - cd example
    - npm run build
  stage: build

Lint Angular:
  needs:
    - Init
  script:
    - cd example
    - npm run lint
  stage: build

Lint Project:
  needs:
    - Init
  script:
    - npm run lint
  stage: build

####
# Stage: Test
####

Angular Unit Tests:
  artifacts:
    paths:
      - example/coverage/example
    reports:
      junit: example/junit.xml
  needs:
    - Init
  script:
    - cd example
    - npm run test
  stage: test

####
# Stage: Publish
####

Publish npm:
  only:
    - tags
  needs:
    - Angular Unit Tests
    - Audit
    - Audit Angular
    - Init
    - Build Angular
    - Lint Project
  script:
    - |
      cat <<-EO_CONFIG > .npmrc
      @${CI_PROJECT_ROOT_NAMESPACE}:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/
      //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
      git-tag-version=false
      EO_CONFIG
    - npm --no-git-tag-version version "${CI_COMMIT_TAG}";
    - npm publish --access public
  stage: publish