---
image: node:18-alpine

definitions:
  caches:
    npm: $HOME/.npm
  steps:
    - step: &lint
        name: Check code syntax
        caches:
          - npm
          - node
        script:
          - npm ci
          - npm run lint
    - step: &test
        name: Run tests
        caches:
          - npm
          - node
        script:
          - npm ci
          - npm run test -- --coverage --coverageReporters=json-summary
        artifacts:
          - coverage/**
    - step: &publish-npm
        name: Publish to NPM
        script:
          # add "--tag $BITBUCKET_TAG" if not a semver
          - |
            if [[ $BITBUCKET_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
              npm publish
            else
              npm publish --tag $BITBUCKET_TAG
            fi
    - step: &testify
        name: Testify to the Witness
        script:
          - pipe: docker://public.ecr.aws/cbcrnd/pipe-witness-testify

pipelines:
  default:
    - parallel:
        - step: *lint
        - step: *test
    - step: *testify

  tags:
    '*':
      - parallel:
          fail-fast: true
          steps:
            - step: *lint
            - step: *test
      - step: *publish-npm
