name: Prod

on:
  push:
    branches:
      - main
      - next
      - v*
      - patch-release
      - "!renovate/**"
  pull_request:

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    uses: ./.github/workflows/_build.yml

  lint:
    name: Lint
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          cache: "yarn"

      - name: Install Dependencies
        run: yarn install --immutable

      - name: Download Artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: dist
          path: dist

      - name: Lint Code
        run: yarn test:production-lint

  test:
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        os:
          - "ubuntu-latest"
          - "macos-latest"
          - "windows-latest"
        node:
          # Latest version
          - "26"
          # Minimal version for production
          - "14"
        include:
          - os: "ubuntu-latest"
            # Pick a version that is fast (normally highest LTS version)
            node: "26"
            FULL_TEST: true
          # Versions not tested on linux
          - os: "ubuntu-latest"
            node: "24"
          - os: "ubuntu-latest"
            node: "22"
          - os: "ubuntu-latest"
            node: "20"
          - os: "ubuntu-latest"
            node: "18"
          - os: "ubuntu-latest"
            node: "16"
          # Tests on Intel Mac x Node.js 14 ( https://github.com/prettier/prettier/issues/16248 )
          - os: "macos-15-intel"
            node: "14"
        # setup-node does not support Node.js 14 x M1 Mac
        exclude:
          - os: "macos-latest"
            node: "14"
    env:
      FULL_TEST: ${{ matrix.FULL_TEST }}
    name: ${{ matrix.FULL_TEST && '[Full Test] ' || '' }} Node.js ${{ matrix.node }} on ${{ startsWith(matrix.os, 'macos') && 'MacOS' || startsWith(matrix.os, 'windows') && 'Windows' || 'Linux' }}
    runs-on: ${{ matrix.os }}
    needs: [build]
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Install yarn@3
        if: ${{ matrix.node == '14' || matrix.node == '16' }}
        run: |
          yarn config unset approvedGitRepositories
          yarn config unset npmMinimalAgeGate
          yarn set version 3
          yarn config set httpRetry 10
          cat .yarnrc.yml

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: ${{ matrix.node }}
          cache: "yarn"

      - name: Downgrade Jest
        if: ${{ matrix.node == '14' || matrix.node == '16' }}
        run: node ./scripts/prepare-test-for-legacy-nodejs.js --node-version ${{ matrix.node }}

      - name: Install Dependencies(yarn@3,mutable)
        if: ${{ matrix.node == '14' || matrix.node == '16' }}
        env:
          YARN_ENABLE_IMMUTABLE_INSTALLS: false
        run: yarn install

      - name: Install Dependencies
        run: yarn install --immutable

      - name: Download Artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: dist
          path: dist

      - name: Run Tests
        run: yarn test:production

      - name: Run Format Tests (standalone)
        run: yarn test:production-standalone

      - name: "`--debug-benchmark` flag"
        if: ${{ matrix.node != '14' && matrix.os != 'windows-latest' }}
        working-directory: dist/prettier
        run: node bin/prettier.cjs --debug-benchmark package.json 2>&1 >/dev/null | grep "Latency avg" || exit 1

  browser:
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        os:
          - "ubuntu-latest"
          # - "macos-latest"
          - "windows-latest"
        browser:
          - "chrome"
          - "firefox"
        include:
          # It's slow on firefox
          - os: "ubuntu-latest"
            browser: "chrome"
            FULL_TEST: true
        exclude:
          # Hangs https://github.com/prettier/prettier/pull/18716
          - os: "windows-latest"
            browser: "chrome"
    env:
      FULL_TEST: ${{ matrix.FULL_TEST }}
      TEST_RUNTIME_BROWSER: ${{ matrix.browser }}
    name: ${{ matrix.FULL_TEST && '[Full Test] ' || '' }} Browser(${{ matrix.browser }}) on ${{ startsWith(matrix.os, 'macos') && 'MacOS' || startsWith(matrix.os, 'windows') && 'Windows' || 'Linux' }}
    runs-on: ${{ matrix.os }}
    needs: [build]
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          cache: "yarn"

      - name: Puppeteer cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: .tmp/puppeteer/
          key: v1

      # https://chromium.googlesource.com/chromium/src/+/main/docs/security/apparmor-userns-restrictions.md
      - run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
        if: matrix.os == 'ubuntu-latest'

      - name: Install Dependencies
        run: yarn install --immutable

      - name: Download Artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: dist
          path: dist

      - name: Run Tests (ESM)
        env:
          TEST_BUNDLE_FORMAT: esm
        run: yarn test:production:browser

      - name: Run Tests (UMD)
        env:
          TEST_BUNDLE_FORMAT: umd
        run: yarn test:production:browser

  cli:
    name: CLI
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Download Artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: dist
          path: dist

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          package-manager-cache: false

      - name: "`--debug-benchmark` flag"
        working-directory: dist/prettier
        run: node bin/prettier.cjs --debug-benchmark package.json 2>&1 >/dev/null | grep "'--debug-benchmark' requires the 'tinybench' package to be installed." || exit 1

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: "0.10.48"
          package-manager-cache: false

      - name: Validate Node.js Version
        run: node -v | grep "v0.10.48" || exit 1

      - name: Run CLI on Node.js v0.10.48
        working-directory: dist/prettier
        run: node bin/prettier.cjs --version 2>&1 >/dev/null | grep "prettier requires at least version 14 of Node, please upgrade" || exit 1

  preview:
    if: github.repository == 'prettier/prettier'
    name: Preview release
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          # `pkg-pr-new` requires Node.js v20
          node-version: latest

      - name: Download Artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: dist
          path: dist

      - name: Release
        working-directory: dist
        run: |
          yarn dlx pkg-pr-new publish "./*" --compact --packageManager=yarn
