name: Build and publish Deno package

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v1

      - name: Get version
        id: version
        shell: bash
        run: echo ::set-output name=VERSION::"$([[ "$GITHUB_REF" == refs/tags/v* ]] && echo ${GITHUB_REF#refs/tags/v} || echo '0.0.0')"

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          default: true

      - name: Run prebuild steps
        shell: bash
        run: bash ./prebuild.sh

      - name: Install wasm-pack
        working-directory: ./wasm
        shell: bash
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Build native module
        working-directory: ./wasm
        shell: bash
        run: TARGET=web ./build

      - name: Install B2 CLI
        run: |
          wget -O b2 https://github.com/Backblaze/B2_Command_Line_Tool/releases/latest/download/b2-linux
          chmod +x b2

      - name: Upload to B2
        run: |
          ./b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }}
          for f in wasm/pkg/*; do
            if [[ "$f" == *.js ]]; then
              content_type='text/javascript'
            elif [[ "$f" == *.wasm ]]; then
              content_type='application/wasm'
            else
              content_type='application/octet-stream'
            fi
            ./b2 upload-file --contentType "$content_type" ${{ secrets.CICD_CLI_B2_BUCKET_NAME }} "$f" "minify-html/deno/${{ steps.version.outputs.VERSION }}/$(basename "$f")"
          done
