name: Publish to npm

on:
  release:
    types: [published]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Setup Node (${{ matrix.node-version }})
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org/
      - name: Set up git user
        run: git config --global user.name ${GITHUB_ACTOR}
      - name: Set up git email
        run: git config --global user.name ${GITHUB_ACTOR}@users.noreply.github.com
      - name: Install dependencies
        run: npm ci
      - name: Build dist folder
        run: npm run build
      - name: Commit dist folder if needed #it fails if nothing has changed so we allow an error
        run: git commit -am 'Rebuild dist'
        continue-on-error: true
      - name: Read tag name
        run: echo "RELEASE_VERSION=${GITHUB_REF:10}" >> $GITHUB_ENV
      - name: Update changelog
        uses: thomaseizinger/keep-a-changelog-new-release@v1
        with:
          version: ${{ env.RELEASE_VERSION }}
      - name: Commit changelog
        run: git commit -am 'Update changelog'
      - name: Delete tag #for some reason npm version fails if the tag exists
        run: git tag -d $RELEASE_VERSION
      - name: Update version in package.json
        run: npm version $RELEASE_VERSION
      - name: Push changed files to master
        run: git push origin HEAD:master
      - name: Recreate tag
        run: git tag -f $RELEASE_VERSION
      - name: Update remote tag
        run: git push --tags --force
      - name: Publish to npm
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}
