name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  install:
    name: Install
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - name: Cache node_modules
      id: cacheModules
      uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}

    - name: Install dependencies
      if: steps.cacheModules.outputs.cache-hit != 'true'
      run: npm install

  test:
    name: Test specs
    needs: [install]
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node-version: [10.x, 12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
    - name: Cache node_modules
      uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    - run: make test

  lint:
    name: Lint code
    needs: [install]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    - run: make lint

  commitlint:
    name: Lint commits
    needs: [install]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    # The commitlint command needs the main branch available to work.
    - run: git fetch origin main
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    - run: make commitlint

  typecheck:
    name: Check types
    needs: [install]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    - run: make typecheck

  # The security job can't run on pull requests opened from forks because
  # Github doesn't pass down the SNYK_TOKEN environment variable.
  security:
    name: Check security
    needs: [install]
    runs-on: ubuntu-latest
    if: |
      github.repository == 'lquixada/cross-fetch' &&
      github.ref == 'refs/heads/main'
    steps:
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    - run: make secure
      env:
        SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}

  npm:
    name: Publish to NPM registry
    if: |
      github.repository == 'lquixada/cross-fetch' &&
      github.ref == 'refs/heads/main' &&
      contains(github.event.head_commit.message, 'chore(release)')
    needs: [test, lint, commitlint, typecheck, security]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    # Setup .npmrc file to publish to npm
    - uses: actions/setup-node@v2
      with:
        node-version: '10.x'
        registry-url: 'https://registry.npmjs.org'
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    - run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  github:
    name: Publish to Github registry
    if: |
      github.repository == 'lquixada/cross-fetch' &&
      github.ref == 'refs/heads/main' &&
      contains(github.event.head_commit.message, 'chore(release)')
    needs: [test, lint, commitlint, typecheck, security]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    # Setup .npmrc file to publish to GitHub Packages
    - uses: actions/setup-node@v2
      with:
        registry-url: 'https://npm.pkg.github.com'
    - uses: actions/cache@v2
      with:
        path: ~/.npm # this is cache where npm installs from before going out to the network
        key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
    - run: npm install --prefer-offline
    # The package name needs to be scoped in order to publish on Github Registry
    - run: sed -i 's/"cross-fetch"/"@lquixada\/cross-fetch"/g' package.json
    - run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
