diff --git a/.github/actions/base/prettier/action.yml b/.github/actions/base/prettier/action.yml index 847c4c9..67efce8 100644 --- a/.github/actions/base/prettier/action.yml +++ b/.github/actions/base/prettier/action.yml @@ -43,11 +43,33 @@ runs: echo "files=$files" >> "$GITHUB_OUTPUT" fi - - name: Install Prettier + - name: Install dependencies + if: steps.resolved-files.outputs.any-changed == 'true' + shell: bash + working-directory: ${{ env.HOLDEX_WORKING_DIR || '.' }} + run: | + if [ -f package.json ]; then + if [ "${{ inputs.package-manager }}" = "pnpm" ]; then + pnpm install --frozen-lockfile + elif [ "${{ inputs.package-manager }}" = "npm" ]; then + npm ci + else + bun i --frozen-lockfile + fi + else + echo "No package.json found, skipping dependency installation." + fi + + - name: Install Prettier (fallback) if: steps.resolved-files.outputs.any-changed == 'true' shell: bash working-directory: ${{ env.HOLDEX_WORKING_DIR || '.' }} run: | + if [ -x "./node_modules/.bin/prettier" ]; then + echo "Using project-local Prettier." + exit 0 + fi + if [ "${{ inputs.package-manager }}" = "pnpm" ]; then pnpm add -g prettier elif [ "${{ inputs.package-manager }}" = "npm" ]; then @@ -62,10 +84,21 @@ runs: shell: bash working-directory: ${{ env.HOLDEX_WORKING_DIR || '.' }} run: | + run_prettier() { + if [ -x "./node_modules/.bin/prettier" ]; then + ./node_modules/.bin/prettier "$@" + elif command -v prettier >/dev/null 2>&1; then + prettier "$@" + else + echo "::error::Prettier not found locally or globally." >&2 + exit 1 + fi + } + probe_file=".prettier-config-probe.tmp" touch "$probe_file" - if prettier --find-config-path "$probe_file" >/dev/null 2>&1; then + if run_prettier --find-config-path "$probe_file" >/dev/null 2>&1; then echo "has-config=true" >> "$GITHUB_OUTPUT" else echo "has-config=false" >> "$GITHUB_OUTPUT" @@ -79,23 +112,6 @@ runs: rm -f "$probe_file" - - name: Install dependencies - if: steps.resolved-files.outputs.any-changed == 'true' - shell: bash - working-directory: ${{ env.HOLDEX_WORKING_DIR || '.' }} - run: | - if [ -f package.json ]; then - if [ "${{ inputs.package-manager }}" = "pnpm" ]; then - pnpm install --frozen-lockfile - elif [ "${{ inputs.package-manager }}" = "npm" ]; then - npm ci - else - bun i --frozen-lockfile - fi - else - echo "No package.json found, skipping dependency installation." - fi - - name: Skip when no files changed if: steps.resolved-files.outputs.any-changed != 'true' shell: bash @@ -106,4 +122,16 @@ runs: if: steps.resolved-files.outputs.any-changed == 'true' shell: bash working-directory: ${{ env.HOLDEX_WORKING_DIR || '.' }} - run: prettier --check --ignore-unknown ${{ steps.resolved-files.outputs.files }} + run: | + run_prettier() { + if [ -x "./node_modules/.bin/prettier" ]; then + ./node_modules/.bin/prettier "$@" + elif command -v prettier >/dev/null 2>&1; then + prettier "$@" + else + echo "::error::Prettier not found locally or globally." >&2 + exit 1 + fi + } + + run_prettier --check --ignore-unknown ${{ steps.resolved-files.outputs.files }} diff --git a/ACTIONS.md b/ACTIONS.md index 04c378e..0eeb53e 100644 --- a/ACTIONS.md +++ b/ACTIONS.md @@ -58,10 +58,11 @@ Path: `/.github/actions/base/prettier` - Discovers changed files itself if `changed-files` is not provided. - Skips execution if no changed files exist. -- Installs Prettier globally using selected package manager. -- Verifies a resolvable Prettier config exists. - Runs dependency install in repo context when `package.json` exists: - `bun i --frozen-lockfile`, `pnpm install --frozen-lockfile`, or `npm ci`. +- Uses project-local Prettier (`./node_modules/.bin/prettier`) when available after dependency install. +- Installs Prettier globally as a fallback only when no local binary is found. +- Verifies a resolvable Prettier config exists. - Runs `prettier --check --ignore-unknown` on changed files. - Respects `HOLDEX_WORKING_DIR` env var: all steps run in that directory when set.