# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Release Version

on:
  push:
    branches: ["main"]
    tags: ["v*"]
jobs:
  build:
    runs-on: ubuntu-latest
    environment: release
    steps:
      - name: extract tag
        id: extract_tag
        uses: actions/github-script@v6
        with:
          script: |
            const prefix = 'refs/tags/';
            const ref = context.ref;
            return ref.startsWith(prefix) ? ref.substring(prefix.length) : '';
          result-encoding: string
      - uses: actions/checkout@v3
      - name: cache node_modules
        uses: actions/cache@v3
        env:
          cache-name: cache-node-modules
        with:
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 14.x
      - name: setup global dependencies
        run: |
          npm install yarn -g 
          npm install zx@7.2.3 -g

      - name: install repo dependencies
        run: |
          yarn install --registry="https://registry.yarnpkg.com"
      - name: npm version
        run: |
          zx scripts/workflow/set-package-version.mjs
        env:
          TAG: ${{steps.extract_tag.outputs.result}}
      - name: build
        run: |
          yarn build:all
      - name: upload build
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: |
            packages/*/dist/**
            packages/*/es/**
            package.json
  release_npm:
      permissions:
        contents: write  # support to do the release on branch
        pull-requests: write
        
      if: ${{ startsWith(github.ref, 'refs/tags/') }}
      needs: [build]
      environment: release
      runs-on: ubuntu-latest

      steps:
        - name: extract tag
          id: extract_tag
          uses: actions/github-script@v6
          with:
            script: |
              const prefix = 'refs/tags/';
              const ref = context.ref;
              return ref.startsWith(prefix) ? ref.substring(prefix.length) : '';
            result-encoding: string
        - uses: actions/checkout@v3
          with:
            ref: 'main'
        - name: use Node.js
          uses: actions/setup-node@v3
          with:
            node-version: 14.x
        - name: cache node_modules
          uses: actions/cache@v3
          env:
            cache-name: cache-node-modules
          with:
            path: node_modules
            key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
            restore-keys: |
              ${{ runner.os }}-build-${{ env.cache-name }}-
              ${{ runner.os }}-build-
              ${{ runner.os }}-
        - name: setup git
          run: |
            git config --global user.name ${{ github.actor }}
            git config --global user.email ${{ github.actor }}@users.noreply.github.com
            git fetch --tags
            git pull
        - name: setup global deps
          run: |
            npm install yarn -g 
            npm install zx@7.2.3 -g

        - name: install repo deps
          run: |
            yarn install --registry="https://registry.yarnpkg.com"
        - name: download build
          uses: actions/download-artifact@v4
          with:
            name: build
        - name: npm version
          run: |
            zx scripts/workflow/set-package-version.mjs
          env:
            TAG: ${{steps.extract_tag.outputs.result}}
        - name: setup npm
          run: |
            npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"

            echo "ls -a ${{ github.workspace }}"
            ls -a ${{ github.workspace }}
          env:
            CI: true
            NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        - name: publish
          run: |
            zx scripts/workflow/npm-publish.mjs
        - name: push to release branch
          uses: EndBug/add-and-commit@v9
          with:
            add: "."
            default_author: github_actor
            fetch: true
            new_branch: release
            push: '--set-upstream origin release --force'
            message: 'Auto Publish npm version ${{steps.extract_tag.outputs.result}}'
        - name: create PR from back to main
          uses: actions/github-script@v6
          env:
            RELEASED_VERSION: ${{steps.extract_tag.outputs.result}}
          with:
            script: |
              const owner = context.repo.owner;
              const repo = context.repo.repo;
              const base = "main";
              const head = "release";
              const title = "new version published: " + process.env.RELEASED_VERSION;
              const body = "";
              github.rest.pulls.create({
                  owner,
                  repo,
                  base,
                  head,
                  title,
                  body
              });
