# 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
# It will also deploy the build to two different repos with https://github.com/peaceiris/actions-gh-pages

name: Build ImJoy Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  test-and-build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [13.14.0]
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Cache dependencies
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - run: npm ci
      - run: npm run check-format
      - run: npm run build
      - run: npm run test
      - name: download elFinder
        run: |
          wget https://github.com/imjoy-team/elFinder/archive/refs/heads/gh-pages.zip
          unzip gh-pages.zip 
          rm gh-pages.zip 
          rm -rf dist/elFinder
          mv elFinder-gh-pages/ dist/elFinder
          cp dist/elFinder/service-worker.js dist # make sure we have service worker for all domains
      - name: Save build output
        uses: actions/upload-artifact@v1
        with:
          name: built-output
          path: ./dist
    env:
      NODE_OPTIONS: '--max-old-space-size=4096'

  deploy-imjoy-core:
    runs-on: ubuntu-latest
    needs: test-and-build
    if: github.ref == 'refs/heads/master'
    steps:
      - name: Load saved build output
        uses: actions/download-artifact@v1
        with:
          name: built-output
          path: ./dist
      - name: Deploy imjoy-core to https://lib.imjoy.io
        uses: peaceiris/actions-gh-pages@v3.5.0
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
        with:
          deploy_key: ${{ secrets.ACTION_DEPLOY_KEY_FOR_LIB }}
          external_repository: imjoy-team/lib.imjoy.io
          publish_branch: master
          publish_dir: ./dist
          commit_message: Deployed from
          cname: 'lib.imjoy.io'

  publish-to-npm:
    runs-on: ubuntu-latest
    needs: test-and-build
    if: github.ref == 'refs/heads/master'
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 13.14.0
          registry-url: 'https://registry.npmjs.org'
      - run: yarn
      - name: Load saved build output
        uses: actions/download-artifact@v1
        with:
          name: built-output
          path: ./dist
      - name: Publish if version has been updated
        uses: oeway/npm-publish-action@1.1.1
        with:
          tag_name: "v%s"
          tag_message: "v%s"
          commit_pattern: "^Release (\\S+)"
          commit_user: oeway
          publish_with: yarn
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
      
