name: Publish formio-gds-template to npm

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: 'https://registry.npmjs.org'
      - name: Install dependencies
        run: |
          rm -rf node_modules
          npm ci
      - name: Build package
        run: npm run prepublish
      - name: Publish package to npm
        # npm refuses to publish a prerelease (e.g. 3.0.1-alpha) without an explicit tag.
        run: |
          VERSION=$(node -p "require('./package.json').version")
          case "$VERSION" in
            *-*) TAG=next ;;
            *) TAG=latest ;;
          esac
          echo "Publishing $VERSION with tag $TAG"
          npm publish --access public --tag "$TAG"
        env:
          NODE_AUTH_TOKEN: ${{ secrets.FORMIO_GDS_TEMPLATE_TOKEN }}
