name: Transpile TS

on:
  pull_request:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token.
          fetch-depth: 0 # otherwise, you will failed to push refs to dest repo.
          ref: ${{ github.event.pull_request.head.sha }} # checks out the branch being merged into `master`

      - name: Install dependencies
        run: | 
          npm i
      
      - name: Node setup
        uses: actions/setup-node@v2
        with:
          node-version: '16'
          cache: 'npm'

      - name: Build JavaScript files
        run: | # Change line to your build script command.
          npm run start 

      - name: Force add JS files to override .gitignore
        run: git add --force ./lib # <-- Change this to your build path.

      - name: Commit files
        run: | # Change last line to your preferred commit message (I like `chore: build js files`).
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git commit -m "Updating JS files" -a || echo "No commit needed. JS files are already up-to-date"

      - name: Push changes
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.event.pull_request.head.ref }} # pushes the commit to the branch being merged into `master`