componentize-qjs-cli 0.2.2

CLI for converting JavaScript to WebAssembly components using QuickJS
Documentation
name: Publish npm packages

on:
  workflow_dispatch:
    inputs:
      release_tag:
        description: Release tag to build from
        required: false
        type: string
  workflow_call:
    inputs:
      release_tag:
        description: Release tag to build from
        required: true
        type: string

permissions:
  contents: read

concurrency:
  group: npm-publish-${{ inputs.release_tag || github.ref_name }}
  cancel-in-progress: false

env:
  CARGO_TERM_COLOR: always
  RELEASE_TAG: ${{ inputs.release_tag || github.ref_name }}

jobs:
  build:
    name: Build - ${{ matrix.settings.target }}
    runs-on: ${{ matrix.settings.os }}
    strategy:
      fail-fast: false
      matrix:
        settings:
          - target: x86_64-apple-darwin
            os: macos-15-intel
          - target: aarch64-apple-darwin
            os: macos-14
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          - target: aarch64-pc-windows-msvc
            os: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ env.RELEASE_TAG }}

      - name: Install Rust
        uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
        with:
          targets: wasm32-wasip2,${{ matrix.settings.target }}

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24

      - name: Cache cargo
        uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1

      - name: Install cargo-binstall
        uses: cargo-bins/cargo-binstall@aaa84a43aec4955a42c5ffc65d258961e39f276e # v1.19.1

      - name: Install auditable build tool
        run: cargo binstall cargo-auditable --force

      - name: Install cross-compilation tools
        if: matrix.settings.cross
        uses: taiki-e/setup-cross-toolchain-action@129361238c06ff2cc1c4ca5c5d2217af441ffdf6 # v1.40.1
        with:
          target: ${{ matrix.settings.target }}

      - name: Install musl tools
        if: contains(matrix.settings.target, 'musl')
        run: sudo apt-get install -y musl-tools

      - name: Install npm dependencies
        working-directory: npm
        run: npm install

      - name: Sync version from Cargo.toml
        shell: bash
        run: |
          VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          cd npm
          npm version "$VERSION" --no-git-tag-version --allow-same-version

      - name: Build napi binding
        working-directory: npm
        run: npx napi build --manifest-path ../napi/Cargo.toml --platform --target ${{ matrix.settings.target }} --js index.js --dts index.d.ts -o . --release
        env:
          COMPONENTIZE_QJS_RUNTIME_AUDITABLE: 1

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: bindings-${{ matrix.settings.target }}
          path: npm/*.node
          if-no-files-found: error

      - name: Upload generated JS bindings
        if: matrix.settings.target == 'x86_64-unknown-linux-gnu'
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: js-bindings
          path: |
            npm/index.js
            npm/index.d.ts
          if-no-files-found: error

  publish:
    name: Publish to npm
    runs-on: ubuntu-latest
    needs: build
    environment: npm
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ env.RELEASE_TAG }}

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org

      - name: Ensure npm CLI supports trusted publishing
        run: npm install -g npm@^11.5.1

      - name: Install npm dependencies
        working-directory: npm
        run: npm install

      - name: Sync version from Cargo.toml
        run: |
          VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          cd npm
          npm version "$VERSION" --no-git-tag-version --allow-same-version

      - name: Download all artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: npm/artifacts
          merge-multiple: true

      - name: Restore generated JS bindings
        working-directory: npm
        run: cp artifacts/index.js artifacts/index.d.ts .

      - name: List artifacts
        working-directory: npm
        run: ls -la artifacts/*.node

      - name: Prepare npm packages
        working-directory: npm
        run: |
          npx napi create-npm-dirs --npm-dir npm
          npx napi artifacts --output-dir artifacts --npm-dir npm
          npx napi prepublish --skip-optional-publish --no-gh-release

      - name: Stage README and LICENSE in npm packages
        run: ${{ github.workspace }}/.github/scripts/stage-npm-assets.sh

      - name: Check npm package contents
        working-directory: npm
        run: ${{ github.workspace }}/.github/scripts/check-npm-packages.sh

      - name: Publish platform packages
        if: github.event_name != 'workflow_dispatch'
        working-directory: npm
        shell: bash
        run: |
          set -euo pipefail
          for package_json in npm/*/package.json; do
            package_dir="$(dirname "$package_json")"
            (cd "$package_dir" && npm publish --access public)
          done

      - name: Publish main package
        if: github.event_name != 'workflow_dispatch'
        working-directory: npm
        run: npm publish --access public