name: Publish to npm

# Tag push (vX.Y.Z) → sync optionalDependencies to the LATEST per-platform
# cicy-code + cicy-mihomo, then publish cicy-desktop to npm. This is what
# `npx cicy-desktop` / `npm i -g cicy-desktop` users get — the app-installer
# workflows only build dmg/exe/AppImage, they do NOT publish npm.
on:
  push:
    tags:
      - 'v*'
  workflow_dispatch: {}

concurrency:
  group: cicy-desktop-release-npm
  cancel-in-progress: true

jobs:
  # 单元测试(node:test,零依赖)。发布前必过 —— 这些用例守的是 WSL/Docker sidecar 那条
  # 只在 Windows 真机上才跑得到的路径,本地很难复现,只能靠断言兜住。
  # Node 22:`node --test "<glob>"` 的 glob 展开是 node ≥21 的能力(20 只认目录参数)。
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Install dependencies
        run: npm ci --ignore-scripts

      - name: Run tests
        run: npm test

  publish:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Sync runtime deps to latest (cicy-code + cicy-mihomo; drop msys2)
        run: node scripts/sync-runtime-deps.cjs

      # NOTE: the homepage SPA is rebuilt from source automatically by the
      # prepublishOnly npm hook (scripts/build-homepage.cjs) during `npm publish`
      # below, so the package can never ship a stale homepage (this is how the
      # Docker card lingered, and how the version badge went missing). No explicit
      # step needed — one mechanism (npm lifecycle) covers npm + all installers.
      - name: Publish cicy-desktop to npm
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
