name: Snapshot Release
on:
  push:
    paths:
      - ".changeset/**"
    branches:
      - snapshot
  pull_request:
    paths:
      - ".changeset/**"
    branches: [master, main, release]
env:
  CI: true
jobs:
  snapshot-release:
    # Only repo owner can publish snapshot version when create pr target master
    if: ${{ github.triggering_actor == github.repository_owner }}
    runs-on: ubuntu-latest
    steps:
      - name: checkout code repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: setup node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - uses: pnpm/action-setup@v2
        name: Install pnpm
        id: pnpm-install
        with:
          run_install: false
      - name: get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_ENV
      - uses: actions/cache@v3
        name: setup pnpm cache
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: install dependencies
        run: pnpm install --frozen-lockfile=false
      - name: Creating .npmrc
        run: |
          cat << EOF > "$HOME/.npmrc"
            //registry.npmjs.org/:_authToken=$NPM_TOKEN
          EOF
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      # enable it if develop swc plugin
      # - name: install Rust
      #   uses: actions-rs/toolchain@v1
      #   with:
      #     profile: minimal
      # - name: install wasm target
      #   shell: bash
      #   run: |
      #     rustup target add wasm32-wasi
      - name: create and publish versions
        run: |
          pnpm ci:snapshot
          pnpm changeset pre enter snapshot
          pnpm ci:prerelease
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
