---
image: node:22-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
        artifacts:
          - coverage/**
    - step: &build
        name: Build Distribution
        caches:
          - node
        script:
          - npm ci
          - npm run build
        artifacts:
          - dist/**
    - step: &publish-npm
        name: Publish to NPM
        script:
          - npm ci
          # 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

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

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