name: Release
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
    outputs:
      version: ${{ steps.current_version.outputs.current-version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Cache target
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - name: Cache
        uses: actions/cache@v1
        id: yarn-cache
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ runner.os }}-yarn-
      - name: Install dependencies
        run: yarn install
      - name: Check Typescript
        run: yarn check:typescript
      - name: Linting
        run: yarn lint
      - name: Build and see if executable works
        run: |
          yarn build
          node dist/index.js --help
      - name: Configure registry
        run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
      - name: Get current package.json version
        id: current_version
        uses: martinbeentjes/npm-get-version-action@master
      - name: Persist artifact
        uses: actions/upload-artifact@v2
        with:
          name: artifacts
          path: |
            .npmrc
            dist

  beta:
    runs-on: ubuntu-latest
    needs: build
    if: contains(needs.build.outputs.version, 'beta') && (startsWith(github.event.head_commit.message, ':bookmark:') || startsWith(github.event.head_commit.message, ':green_heart:'))
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Grab persisted artifact
        uses: actions/download-artifact@v2
        with:
          name: artifacts
      - run: yarn publish:beta

  release:
    runs-on: ubuntu-latest
    needs: build
    if: "!contains(needs.build.outputs.version, 'beta') && (startsWith(github.event.head_commit.message, ':bookmark:') || startsWith(github.event.head_commit.message, ':green_heart:'))"
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Grab persisted artifact
        uses: actions/download-artifact@v2
        with:
          name: artifacts
      - run: yarn publish:master
