name: Publish (node)

on:
  create:
    tags:
      - v*

env:
  CARGO_INCREMENTAL: 0
  CI: "1"
  DEBUG: "napi:*"

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-18.04, macos-latest, windows-latest]

    name: Build - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          override: true

      - name: Install node dependencies
        run: npm i

      - name: Build
        shell: bash
        run: npm run build
        env:
          MACOSX_DEPLOYMENT_TARGET: "10.13"

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node

      - name: Test bindings
        run: npm test

      - name: Test in lts docker
        if: matrix.os == 'ubuntu-18.04'
        run: docker run --rm -v $(pwd):/swc -w /swc node:10-slim sh -c "yarn test"

  build-windows-i686:
    name: stable - windows-i686 - node@14
    runs-on: windows-latest
    env:
      CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 32
      CARGO_PROFILE_RELEASE_LTO: 'false'
    steps:
      - uses: actions/checkout@v2

      - name: Install node x86
        run: |
          choco install nodejs-lts --x86 -y --force
          refreshenv

      - name: Set 32bit NodeJS path
        run: |
          echo "C:\\Program Files (x86)\\nodejs" >> $GITHUB_PATH
        shell: bash

      - name: NodeJS arch
        run: node -e "console.log(process.arch)"

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          override: true
          target: i686-pc-windows-msvc

      - name: Install node dependencies
        run: npm i

      - name: Build
        shell: bash
        run: yarn build --target i686-pc-windows-msvc

      - name: Test bindings
        run: npm test

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node

  build-linux-musl:
    name: stable - linux-musl - node@10
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Login to registry
        run: |
          docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
        env:
          DOCKER_REGISTRY_URL: docker.pkg.github.com
          DOCKER_USERNAME: ${{ github.actor }}
          DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}

      - name: Cache
        uses: actions/cache@v2
        with:
          path: |
            target/
          key: linux-musl-publish-integration

      - name: Pull docker image
        run: |
          docker pull docker.pkg.github.com/napi-rs/napi-rs/nodejs-rust:10-alpine
          docker tag docker.pkg.github.com/napi-rs/napi-rs/nodejs-rust:10-alpine builder

      - name: "Install dependencies"
        run: npm i

      - name: "Build"
        run: |
          docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/swc -w /swc builder sh -c "./node_modules/.bin/napi build --platform --cargo-name node --release -c ./package.json --cargo-flags='-p node'"

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node

  build-apple-silicon:
    name: stable - aarch64-apple-darwin - node@14
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          override: true
          target: aarch64-apple-darwin

      - name: Cache
        uses: actions/cache@v2
        with:
          path: |
            target/
          key: aarch64-apple-darwin-publish-integration

      - name: Install dependencies
        run: npm i

      - name: Cross build aarch64
        run: yarn build --target aarch64-apple-darwin

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node

  build-linux-aarch64:
    name: stable - aarch64-unknown-linux-gnu - node@14
    runs-on: ubuntu-latest

    steps:
      - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset

      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          override: true
          target: aarch64-unknown-linux-gnu

      - name: Cache
        uses: actions/cache@v2
        with:
          path: |
            target/
          key: aarch64-linux-gnu-publish-integration

      - name: Install cross compile toolchain
        run: |
          sudo apt-get update
          sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu -y

      - name: Install dependencies
        run: npm i

      - name: Cross build aarch64
        run: yarn build --target aarch64-unknown-linux-gnu

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node
  
  build-linux-arm7:
    name: stable - arm7-unknown-linux-gnu - node@14
    runs-on: ubuntu-latest

    steps:
      - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset

      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          override: true
          target: armv7-unknown-linux-gnueabihf

      - name: Cache
        uses: actions/cache@v2
        with:
          path: |
            target/
          key: arm7-linux-gnu-publish-integration

      - name: Install cross compile toolchain
        run: |
          sudo apt-get update
          sudo apt-get install gcc-arm-linux-gnueabihf -y

      - name: Install dependencies
        run: npm i

      - name: Cross build aarch64
        run: yarn build --target armv7-unknown-linux-gnueabihf

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node

  build-android-aarch64:
    name: Build - Android - aarch64
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          override: true
          target: aarch64-linux-android

      - name: Install node dependencies
        run: npm i

      - name: Build
        shell: bash
        run: |
          export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android24-clang"
          yarn build --target aarch64-linux-android

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: bindings
          path: swc.*.node

  publish:
    name: npm
    runs-on: ubuntu-latest
    needs:
      - build
      - build-windows-i686
      - build-linux-musl
      - build-linux-arm7
      - build-linux-aarch64
      - build-apple-silicon
      - build-android-aarch64
    steps:
      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14

      - name: Set release name
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: Install dependencies
        run: npm i

      - name: Download all artifacts
        uses: actions/download-artifact@v2
        with:
          path: artifacts

      - name: List binaries
        run: ls -R artifacts
        shell: bash

      - name: Move binaries
        shell: bash
        run: npm run artifacts

      - name: List npm
        run: ls -R ./scripts/npm
        shell: bash

      - name: Publish
        run: |
          echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
          npm publish --access public
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
