name: On Push
on: [push]

jobs:
  lint_test_generate:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Read .nvmrc
        run: echo ::set-output name=NVMRC::$(cat .nvmrc)
        id: nvm

      - name: Setup node ${{ steps.nvm.outputs.NVMRC }}
        uses: actions/setup-node@v2.5.0
        with:
          node-version: ${{ steps.nvm.outputs.NVMRC }}

      - name: Install
        run: yarn install --network-timeout 1000000

      - name: Lint
        run: yarn lint

      - name: Test
        run: yarn unittest

      - name: Generate
        run: yarn generate

    outputs:
      NVMRC: ${{ steps.nvm.outputs.NVMRC }}

  call_android_workflow:
    needs: lint_test_generate
    uses: ./.github/workflows/android-actions.yml
    with:
      NVMRC: ${{ needs.lint_test_generate.outputs.NVMRC }}
    secrets:
      MAPBOX_ACCESS_TOKEN: ${{ secrets.MAPBOX_ACCESS_TOKEN }}
      MAPBOX_DOWNLOAD_TOKEN: ${{ secrets.MAPBOX_DOWNLOAD_TOKEN }}

  call_ios_workflow:
    needs: lint_test_generate
    uses: ./.github/workflows/ios-actions.yml
    with:
      NVMRC: ${{ needs.lint_test_generate.outputs.NVMRC }}
    secrets:
      MAPBOX_ACCESS_TOKEN: ${{ secrets.MAPBOX_ACCESS_TOKEN }}

  publish:
    if: startsWith(github.ref, 'refs/tags/')
    needs: [lint_test_generate, call_android_workflow, call_ios_workflow]
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup node ${{ steps.nvm.outputs.NVMRC }}
        uses: actions/setup-node@v2.5.0
        with:
          node-version: ${{ needs.lint_test_generate.outputs.NVMRC }}
          registry-url: https://registry.npmjs.org/

      - name: Install and Publish
        run: npm install --force && npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
