name: Release

on:
  push:
    tags:
      - "*alpha*"
      - "*pre*"
      - "*beta*"
      - "1[0-9]+.[0-9]+.[0-9]+"

jobs:
  prerelease:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout highlight.js
        uses: actions/checkout@v2

      - name: Tag is ${{ github.ref }}.
        # we have to repeat ourselves here since the environment is not actually updated
        # until the next step executes, so we can't access $TAG_NAME yet
        run: |
          echo "TAG_NAME=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
          echo "MAJOR_VERSION=$(echo ${GITHUB_REF/refs\/tags\//} | cut -d'.' -f1)" >> $GITHUB_ENV
      - name: Make sure we are pushing a tag...
        if: ${{ !contains(github.ref,'refs/tags/') }}
        run: false
        # run: echo "TAG_NAME=0.0.0-test0" >> $GITHUB_ENV

      - if: contains(github.ref, 'beta') || contains(github.ref, 'pre') || contains(github.ref, 'alpha')
        run: |
          echo "NPM_TAG=beta" >> $GITHUB_ENV
          echo "RELEASING=beta" >> $GITHUB_ENV

      - if: ${{ !(contains(github.ref, 'beta') || contains(github.ref, 'pre') || contains(github.ref, 'alpha')) }}
        run: |
          echo "NPM_TAG=latest" >> $GITHUB_ENV
          echo "RELEASING=stable" >> $GITHUB_ENV

      - name: match-tag-to-package-version
        uses: geritol/match-tag-to-package-version@0.0.2
        env:
          TAG_PREFIX: refs/tags/ # Optional, default prefix refs/tags/

      - name: Use Node.js 15.x
        uses: actions/setup-node@v1
        with:
          node-version: 15.x
      - name: Build Node.js package
        run: |
          npm install
          node ./tools/build.js -t node
          npm test

      - name: Publish highlight.js to NPM
        id: publish
        uses: JS-DevTools/npm-publish@v1
        with:
          check-version: true
          token: ${{ secrets.NPM_TOKEN }}
          package: ./build/package.json
          tag: ${{ env.NPM_TAG }}

      - if: steps.publish.outputs.type != 'none'
        run: |
          echo "Version changed: ${{ steps.publish.outputs.old-version }} => ${{ steps.publish.outputs.version }}"

        # if stable release
      - name: Stable Release
        if: env.RELEASING == 'stable'
        run: echo "BRANCH_NAME=${MAJOR_VERSION}-stable" >> $GITHUB_ENV
        # else (beta)
      - name: Beta Release
        if: env.RELEASING == 'beta'
        run: echo "BRANCH_NAME=main" >> $GITHUB_ENV
      - name: Confirm release is either stable or beta
        if: ${{ !(env.RELEASING == 'stable' || env.RELEASING == 'beta') }}
        run: |
          echo We seem to be releasing `${RELEASING}`.
          false

      - name: Checkout cdn-release
        uses: actions/checkout@v2
        with:
          repository: 'highlightjs/cdn-release'
          path: 'cdn-release'
          token: ${{ secrets.CDN_REPO_TOKEN }}
          ref: ${{ env.BRANCH_NAME }}

      - name: Build CDN package
        run: node ./tools/build.js -t cdn :common

      - name: Commmit & Push cdn-release ${{ env.TAG_NAME }}
        working-directory: ./cdn-release
        run: |
          rm -r ./build
          mv ../build/ ./build/
          mv ./build/DIGESTS.md .
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add ./build/
          git add ./DIGESTS.md
          git commit -m'Update to version ${{ env.TAG_NAME }}'
          git tag ${TAG_NAME}
          git push -f --atomic origin ${BRANCH_NAME} ${TAG_NAME}

      - name: Publish cdn-assets to NPM
        id: publish_cdn
        uses: JS-DevTools/npm-publish@v1
        with:
          check-version: true
          token: ${{ secrets.NPM_TOKEN }}
          package: ./cdn-release/build/package.json
          tag: ${{ env.NPM_TAG }}

        # log.info('Updating CDN repo at %s' % settings.HLJS_CDN_SOURCE)
        # run(['nodejs', 'tools/build.js', '--target', 'cdn', ':common'])
        # os.chdir(settings.HLJS_CDN_SOURCE)
        # run(['git', 'pull', '-f'])
        # lines = run(['git', '--git-dir', os.path.join(settings.HLJS_CDN_SOURCE, '.git'), 'tag'])
        # build_dir = os.path.join(settings.HLJS_CDN_SOURCE, 'build')
        # if version in lines:
        #     log.info('Tag %s already exists in the local CDN repo' % version)
        # else:
        #     if os.path.exists(build_dir):
        #         shutil.rmtree(build_dir)
        #     shutil.move(os.path.join(settings.HLJS_SOURCE, 'build'), build_dir)
        #     run(['git', 'add', '.'])
        #     run(['git', 'commit', '-m', 'Update to version %s' % version])
        #     run(['git', 'tag', version])
        # run(['git', 'push'])
        # run(['git', 'push', '--tags'])
        # npm_publish(build_dir)
        # os.chdir(settings.HLJS_SOURCE)