name: Populate readme.md from master and lint src/data.js

on:
  push:
    branches: master
    paths:
    - src/data.js
    - scripts/readme-template.md

env:
  CI: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-node@v1
      with:
        node-version: 16.x

    - name: Cache/Restore node modules
      uses: actions/cache@v1
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}

    - name: Install Dependencies
      run: npm install

    - name: Lint src/data.js
      run: |
        npx eslint --fix src/data.js
        git add src/data.js

    - name: Populate readme.md from master
      run: |
        node ./scripts/populate-readme.js
        mv generated-readme.md readme.md
        git add readme.md
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        # commit only if any changes
        if [ ! -z "$(git status --porcelain)" ]; then
        git commit -m "chore: generate \`readme.md\`, lint \`src/data.js\`"
        fi

    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
