# GENERATED BY ./npm_publish.ts -- DO NOT DIRECTLY EDIT

name: npm_publish
on:
  workflow_dispatch:
    inputs:
      version:
        description: Version
        type: string
      dry_run:
        description: 'Dry run (build and test, but skip publishing to npmjs.org)'
        type: boolean
        default: true
  release:
    types:
      - published
jobs:
  build:
    name: npm build
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Configure git
        run: |-
          git config --global core.symlinks true
          git config --global fetch.parallel 32
      - name: Clone repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
        with:
          submodules: recursive
      - name: Install Deno
        uses: denoland/setup-deno@667a34cdef165d8d2b2e98dde39547c9daac7282
        with:
          deno-version: v2.7.1
      - name: Build npm packages
        run: './tools/release/npm/build.ts ${{ inputs.version || '''' }}'
      - name: Tar npm dist (preserves permissions)
        run: tar cf tools/release/npm/dist.tar -C tools/release/npm --exclude='node_modules' dist
      - name: Upload npm dist artifact
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f
        with:
          name: npm-dist
          path: tools/release/npm/dist.tar
          retention-days: 1
  test:
    name: 'npm test (${{ matrix.runner }})'
    needs:
      - build
    runs-on: '${{ matrix.runner }}'
    timeout-minutes: 15
    defaults:
      run:
        shell: bash
    strategy:
      matrix:
        runner:
          - ubuntu-latest
          - macos-latest
          - windows-latest
      fail-fast: false
    steps:
      - name: Clone repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
        with:
          submodules: false
      - name: Install Node
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f
        with:
          node-version: 24.x
      - name: Download npm dist artifact
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53
        with:
          name: npm-dist
          path: tools/release/npm
      - name: Extract npm dist
        run: tar xf tools/release/npm/dist.tar -C tools/release/npm
      - name: Write Verdaccio config
        run: |-
          mkdir -p "${{ runner.temp }}/verdaccio/storage"
          cat > "${{ runner.temp }}/verdaccio/config.yaml" << 'HEREDOC'
          storage: ./storage
          uplinks: {}
          packages:
            '@deno/*':
              access: $all
              publish: $all
            'deno':
              access: $all
              publish: $all
            '**':
              access: $all
              publish: $all
          max_body_size: 200mb
          log: { type: stdout, format: pretty, level: warn }
          HEREDOC
      - name: Start Verdaccio
        run: |-
          npx verdaccio@6 \
            --config "${{ runner.temp }}/verdaccio/config.yaml" --listen 4873 &
          for i in $(seq 1 30); do
            if curl -s http://localhost:4873/-/ping > /dev/null 2>&1; then
              echo "Verdaccio is ready"
              break
            fi
            sleep 1
          done
      - name: Install pnpm
        run: |-
          npm install -g pnpm
          PNPM_HOME="${{ runner.temp }}/pnpm-global"
          mkdir -p "$PNPM_HOME"
          echo "PNPM_HOME=$PNPM_HOME" >> "$GITHUB_ENV"
          echo "$PNPM_HOME/bin" >> "$GITHUB_PATH"
      - name: Configure npm to use Verdaccio
        run: |-
          npm config set registry http://localhost:4873/
          npm config set //localhost:4873/:_authToken dummy-token
      - name: Publish packages to Verdaccio
        id: publish-verdaccio
        run: |-
          DIST_DIR="tools/release/npm/dist"
          for pkg_dir in "$DIST_DIR"/@deno/*/; do
            echo "Publishing $(basename "$pkg_dir") to Verdaccio..."
            (cd "$pkg_dir" && npm publish --registry http://localhost:4873/)
          done
          echo "Publishing deno to Verdaccio..."
          (cd "$DIST_DIR/deno" && npm publish --registry http://localhost:4873/)
          VERSION=$(node -p "require('./$DIST_DIR/deno/package.json').version")
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
      - name: Test npm install deno
        run: |-
          TEST_DIR="${{ runner.temp }}/npm-test"
          mkdir -p "$TEST_DIR"
          cd "$TEST_DIR"
          npm init -y
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          npm install deno@${{ steps.publish-verdaccio.outputs.version }}
          ACTUAL="$(npx deno -v)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
      - name: Test deno via bin.cjs fallback
        run: |-
          cd "${{ runner.temp }}/npm-test"
          node node_modules/deno/bin.cjs eval "console.log('npm package works')"
      - name: Test deno via simulated readonly file system
        run: |-
          TEST_DIR="${{ runner.temp }}/readonly-test"
          mkdir -p "$TEST_DIR"
          cd "$TEST_DIR"
          npm init -y
          npm install deno@${{ steps.publish-verdaccio.outputs.version }}
          rm -f node_modules/deno/deno*
          DENO_SIMULATED_READONLY_FILE_SYSTEM=1 node node_modules/deno/bin.cjs -v
      - name: Test npm global install deno
        run: |-
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          npm install -g deno@${{ steps.publish-verdaccio.outputs.version }}
          ACTUAL="$(deno -v)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
          deno eval "console.log('global npm install works')"
          # Verify the global bin entry points directly at the native binary, not bin.cjs
          if [ "$RUNNER_OS" = "Windows" ]; then
            DENO_CMD="$(npm prefix -g)/deno.cmd"
            grep -q "bin.cjs" "$DENO_CMD" && { echo "ERROR: deno.cmd still points to bin.cjs"; exit 1; }
            echo "deno.cmd correctly points to native binary"
          else
            DENO_LINK="$(which deno)"
            LINK_TARGET="$(readlink "$DENO_LINK")"
            echo "deno symlink target: $LINK_TARGET"
            echo "$LINK_TARGET" | grep -q "bin.cjs" && { echo "ERROR: deno symlink still points to bin.cjs"; exit 1; }
            echo "deno symlink correctly points to native binary"
          fi
          npm uninstall -g deno
      - name: Test npm global install deno (--ignore-scripts)
        run: |-
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          npm install -g --ignore-scripts deno@${{ steps.publish-verdaccio.outputs.version }}
          ACTUAL="$(deno -v)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
          deno eval "console.log('global npm install works')"
          npm uninstall -g deno
      - name: Test pnpm local install deno (without postinstall)
        run: |-
          TEST_DIR="${{ runner.temp }}/pnpm-test-no-scripts"
          mkdir -p "$TEST_DIR"
          cd "$TEST_DIR"
          npm init -y
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          pnpm install --ignore-scripts deno@${{ steps.publish-verdaccio.outputs.version }} --registry http://localhost:4873/
          ACTUAL="$(node node_modules/deno/bin.cjs -v)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
      - name: Test pnpm global install deno (without postinstall)
        run: |-
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          pnpm install -g --ignore-scripts deno@${{ steps.publish-verdaccio.outputs.version }} --registry http://localhost:4873/
          ACTUAL="$(deno -v)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
          pnpm uninstall -g deno
      - name: Test pnpm local install deno (with postinstall)
        run: |-
          TEST_DIR="${{ runner.temp }}/pnpm-test"
          mkdir -p "$TEST_DIR"
          cd "$TEST_DIR"
          npm init -y
          printf 'allowBuilds:\n  deno: true\n' > pnpm-workspace.yaml
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          pnpm install deno@${{ steps.publish-verdaccio.outputs.version }} --registry http://localhost:4873/
          # `pnpm exec` may print lockfile verification messages to stdout before
          # the command output, so only compare against the last line
          ACTUAL="$(pnpm exec deno -v | tail -n 1)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
      - name: Test pnpm global install deno (with postinstall)
        run: |-
          GLOBAL_DIR="$(pnpm root -g)"
          mkdir -p "$GLOBAL_DIR"
          printf 'allowBuilds:\n  deno: true\n' > "$GLOBAL_DIR/pnpm-workspace.yaml"
          EXPECTED_VERSION="deno ${{ steps.publish-verdaccio.outputs.version }}"
          pnpm install -g deno@${{ steps.publish-verdaccio.outputs.version }} --registry http://localhost:4873/
          ACTUAL="$(deno -v)"
          echo "$ACTUAL"
          [ "$ACTUAL" = "$EXPECTED_VERSION" ] || { echo "Version mismatch: expected '$EXPECTED_VERSION', got '$ACTUAL'"; exit 1; }
          deno eval "console.log('global pnpm install works')"
          pnpm uninstall -g deno
      - name: Test npm local install deno (PowerShell)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |-
          cd "${{ runner.temp }}/npm-test"
          npx deno -v
          npx deno eval "console.log('PowerShell npm local install works')"
      - name: Test npm global install deno (PowerShell)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |-
          npm install -g deno@${{ steps.publish-verdaccio.outputs.version }}
          deno -v
          deno eval "console.log('PowerShell npm global install works')"
          npm uninstall -g deno
      - name: Test npm local install deno (cmd)
        if: runner.os == 'Windows'
        shell: cmd
        run: |-
          cd "${{ runner.temp }}/npm-test"
          npx deno -v
          npx deno eval "console.log('cmd npm local install works')"
      - name: Test npm global install deno (cmd)
        if: runner.os == 'Windows'
        shell: cmd
        run: |-
          npm install -g deno@${{ steps.publish-verdaccio.outputs.version }}
          deno -v
          deno eval "console.log('cmd npm global install works')"
          npm uninstall -g deno
  publish:
    name: npm publish
    needs:
      - build
      - test
    if: '!(inputs.dry_run || false)'
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    timeout-minutes: 15
    steps:
      - name: Configure git
        run: |-
          git config --global core.symlinks true
          git config --global fetch.parallel 32
      - name: Clone repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
        with:
          submodules: recursive
      - name: Install Deno
        uses: denoland/setup-deno@667a34cdef165d8d2b2e98dde39547c9daac7282
        with:
          deno-version: v2.7.1
      - name: Install Node
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f
        with:
          node-version: 24.x
          registry-url: 'https://registry.npmjs.org'
      - name: Download npm dist artifact
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53
        with:
          name: npm-dist
          path: tools/release/npm
      - name: Extract npm dist
        run: tar xf tools/release/npm/dist.tar -C tools/release/npm
      - name: Publish to npm
        run: './tools/release/npm/build.ts ${{ inputs.version || '''' }} --publish-only'

# gagen:pin actions/checkout@v6 = de0fac2e4500dabe0009e67214ff5f5447ce83dd
# gagen:pin actions/download-artifact@v6 = 018cc2cf5baa6db3ef3c5f8a56943fffe632ef53
# gagen:pin actions/setup-node@v6 = 53b83947a5a98c8d113130e565377fae1a50d02f
# gagen:pin actions/upload-artifact@v6 = b7c566a772e6b6bfb58ed0dc250532a479d7789f
# gagen:pin denoland/setup-deno@v2 = 667a34cdef165d8d2b2e98dde39547c9daac7282
