# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches:
      - main
      - master
    tags:
      - '*'

  pull_request:
    branches:
      - main
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x]

    steps:
      - name: Checking out for ${{ github.ref }}
        uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Run npx version-from-git --no-git-tag-version
        if: ${{ startsWith(github.ref, 'refs/heads/') }}
        run: npx version-from-git --no-git-tag-version

      - run: npm ci

      - run: npm run bootstrap

      - name: Propagate versions
        run: |
          node_modules/.bin/lerna version --force-publish --no-git-tag-version --no-push --yes `cat package.json | jq -r .version`

      - run: npm run build --if-present

      - run: npm test
        env:
          CUSTOM_VOICE_NAME: ${{ secrets.CUSTOM_VOICE_NAME }}
          REGION: ${{ secrets.REGION }}
          SPEECH_RECOGNITION_ENDPOINT_ID: ${{ secrets.SPEECH_RECOGNITION_ENDPOINT_ID }}
          SPEECH_SYNTHESIS_DEPLOYMENT_ID: ${{ secrets.SPEECH_SYNTHESIS_DEPLOYMENT_ID }}
          SUBSCRIPTION_KEY: ${{ secrets.SUBSCRIPTION_KEY }}

      - name: Copy UMD
        run: |
          mkdir -p packages/component/umd/
          cp packages/bundle/dist/*.js packages/component/umd/

      - name: Copy documents
        run: |
          cp CHANGELOG.md packages/component
          cp LICENSE packages/component
          cp README.md packages/component

      - name: Run npm pack
        run: |
          cd packages/component
          npm pack

      - name: Upload npm-tarball
        uses: actions/upload-artifact@v2
        if: ${{ matrix.node-version == '14.x' }}
        with:
          name: npm-tarball
          path: 'packages/component/*.tgz'

      - name: Upload gh-pages
        uses: actions/upload-artifact@v2
        if: ${{ matrix.node-version == '14.x' }}
        with:
          name: gh-pages
          path: 'packages/playground/build/**/*'

  publish:
    needs: build
    runs-on: ubuntu-latest
    if: ${{ startsWith(github.ref, 'refs/heads/') || startsWith(github.ref, 'refs/tags/') }}

    steps:
      - name: Download npm-tarball
        uses: actions/download-artifact@v2
        with:
          name: npm-tarball

      - name: Download gh-pages
        uses: actions/download-artifact@v2
        with:
          name: gh-pages
          path: gh-pages/

      - name: Read package.json
        id: read-package-json
        run: |
          echo "::set-output name=name::$(tar xOf *.tgz package/package.json | jq -r '.name')"
          echo "::set-output name=version::$(tar xOf *.tgz package/package.json | jq -r '.version')"
          echo "::set-output name=tarball::$(ls *.tgz)"
          echo "::set-output name=date::$(date +%Y-%m-%d)"

      - name: Run npm publish ${{ steps.read-package-json.outputs.name }}@${{ steps.read-package-json.outputs.version }}
        run: |
          npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}
          npm publish *.tgz --tag master

      - name: Run npm dist-tag add ${{ steps.read-package-json.outputs.name }}@${{ steps.read-package-json.outputs.version }} latest
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        run: |
          npm dist-tag add ${{ steps.read-package-json.outputs.name }}@${{ steps.read-package-json.outputs.version }} latest

      - name: Draft a new release
        uses: actions/create-release@v1
        id: create-release
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: '[${{ steps.read-package-json.outputs.version }}] - ${{ steps.read-package-json.outputs.date }}'
          draft: true

      - name: Upload tarball to release
        uses: actions/upload-release-asset@v1
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: ./${{ steps.read-package-json.outputs.tarball }}
          asset_name: ${{ steps.read-package-json.outputs.tarball }}
          asset_content_type: application/octet-stream

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./gh-pages/
