name: Publish to npmjs

on:
  push:
    tags:
      - 'v*'

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write  # OIDC: npm 用它换 trusted publisher 发布凭证

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          # Node 24 自带 npm 11.x，是支持 OIDC Trusted Publisher 免 token 发布的最低版本。
          # Node 20 / npm 10.x 不支持，会退化成无效认证导致 PUT 404。
          node-version: '24'
          registry-url: 'https://registry.npmjs.org/'

      - name: Install dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Run tests
        run: npm test

      - name: Publish to npm
        # 用 npm Trusted Publisher（OIDC）发布，不再需要 NPM_TOKEN secret。
        # 前提：在 npmjs.com 把本仓库（weijia/universal-sync-v2）配置为
        # universal-sync-v2 包的 trusted publisher，并指定本 workflow 文件。
        # id-token: write 权限让 npm CLI 自动向 GitHub 请求 OIDC token，
        # npm registry 验证后允许发布，无需任何长生命周期的 npm access token。
        # --provenance 生成 SLSA 供应链证明（Trusted Publisher 的配套特性）。
        run: npm publish --provenance --no-git-checks --access public
