name: Ontime CLI build

on:
  release:
    types: [published]
  workflow_dispatch:

jobs:
  build_cli:
    permissions:
      id-token: write
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v3
        with:
          version: 9

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build project packages
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
        run: pnpm build

      - name: Copy server
        run: mkdir -p apps/cli/server && cp apps/server/dist/index.cjs apps/cli/server/index.cjs
      
      - name: Copy client
        run: cp -R apps/client/build apps/cli/client

      - name: Copy external
        run: cp -R apps/server/src/external apps/cli/external
      
      - name: Publish to NPM
        run: pnpm publish --access public --no-git-checks
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_CONFIG_PROVENANCE: true
        working-directory: ./apps/cli
