name: Build and Deploy to NPM

on:
  push:
    branches: [ master ]

jobs:
  build:
    name: Build and Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      
      - uses: actions/setup-node@v1
        with:
          node-version: '12'

      - name: Get Semantic Release Info
        id: semantic_release_info
        uses: jossef/action-semantic-release-info@v1
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - name: Update package.json version
        if: ${{ steps.semantic_release_info.outputs.version }}
        uses: jossef/action-set-json-field@v1
        with:
          file: package.json
          field: version
          value: ${{ steps.semantic_release_info.outputs.version }}

      - name: Update bower.json version
        if: ${{ steps.semantic_release_info.outputs.version }}
        uses: jossef/action-set-json-field@v1
        with:
          file: bower.json
          field: version
          value: ${{ steps.semantic_release_info.outputs.version }}

      - name: Build font and docs
        if: ${{ steps.semantic_release_info.outputs.version }}
        run: |
          npm ci
          npm run build
          npm run docs-build

      - name: Commit files
        if: ${{ steps.semantic_release_info.outputs.version }}
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add -A
          git commit -m "docs(): bumping release ${{ steps.semantic_release_info.outputs.version }}"
          git tag ${{ steps.semantic_release_info.outputs.git_tag }}

      - name: Push changes
        if: ${{ steps.semantic_release_info.outputs.version }}
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ github.token }}
          tags: true

      - name: Create GitHub Release
        if: ${{ steps.semantic_release_info.outputs.version }}
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          tag_name: ${{ steps.semantic_release_info.outputs.git_tag }}
          release_name: ${{ steps.semantic_release_info.outputs.git_tag }}
          body: ${{ steps.semantic_release_info.outputs.notes }}
          draft: false
          prerelease: false
  
      - name: Publish to npm
        if: ${{ steps.semantic_release_info.outputs.version }}
        uses: JS-DevTools/npm-publish@v1
        with:
          token: ${{ secrets.NPM_TOKEN }}
