name: Publish
on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      version:
        description: The version to publish (e.g. 1.0.0)
        required: true
env:
  BOT_EMAIL: "dev.bot@parcellab.com"
  BOT_NAME: "parcellab-dev-bot"
  SCOPE: "@parcellab"
jobs:
  version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.load_version.outputs.version }}
    steps:
    - name: Load version
      id: load_version
      run: |
        if [ "$GITHUB_EVENT_NAME" = 'workflow_dispatch' ]
        then
          VERSION="${{ github.event.inputs.version }}"
        else
          if [ "$GITHUB_EVENT_NAME" = 'release' ]
          then
            TAG_NAME="${{ github.event.release.tag_name }}"
          else
            TAG_NAME="${{ github.ref }}"
          fi
          CLEAN_TAG=${TAG_NAME##*/}
          VERSION=${CLEAN_TAG//v}
        fi
        echo "::set-output name=version::$VERSION"
  publish-npm:
    needs: version
    runs-on: ubuntu-latest
    steps:
      - name: Checkout current git repository
        uses: actions/checkout@v3
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of REPO_ACCESS_TOKEN
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository
      - name: Install npm dependencies
        run: npm i
      - name: Run build
        run: npm run build
      - name: Update package.json version to ${{ needs.version.outputs.version }}
        uses: mikefarah/yq@v4.27.2
        with:
          cmd: |
            yq e '.version = "${{ needs.version.outputs.version }}"' -i package.json -j
      - name: Commit new package.json version
        continue-on-error: true
        run: |
          git config --local user.email "${{ env.BOT_EMAIL }}"
          git config --local user.name "${{ env.BOT_NAME }}"
          git commit -m "chore: set version ${{ needs.version.outputs.version }} [skip ci]" -a
      - name: Push changes to current git repository
        continue-on-error: true
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.REPO_ACCESS_TOKEN_OPEN_SOURCE }}
          branch: main
      - name: Use Node.js with NPMjs as registry url
        uses: actions/setup-node@v3
        with:
          registry-url: https://registry.npmjs.org
          scope: ${{ env.SCOPE }}
      - name: Publish NPM package via NPMjs
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  publish-github:
    needs: version
    runs-on: ubuntu-latest
    steps:
      - name: Checkout current git repository
        uses: actions/checkout@v3
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of REPO_ACCESS_TOKEN
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository
      - name: Install npm dependencies
        run: npm i
      - name: Run build
        run: npm run build
      - name: Update package.json version to ${{ needs.version.outputs.version }}
        uses: mikefarah/yq@v4.27.2
        with:
          cmd: |
            yq e '.version = "${{ needs.version.outputs.version }}"' -i package.json -j
      - name: Use Node.js with Github Packages as registry url
        uses: actions/setup-node@v3
        with:
          registry-url: https://npm.pkg.github.com
          scope: ${{ env.SCOPE }}
      - name: Publish NPM package via Github
        run: npm publish --access restricted
        env:
          NODE_AUTH_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN_OPEN_SOURCE }}
