Skip to content

Commit

Permalink
Implemented custom Prettier hook and updated GitHub Actions workflow.
Browse files Browse the repository at this point in the history
**Added:**

- Created a new custom pre-commit hook script `prettier.sh` to format JSON, YAML,
  and YML files using Prettier.

**Changed:**

- Updated GitHub Actions pre-commit workflow to use `mage runPreCommit` command
  instead of `pre-commit run --all-files`, enhancing the CI process.
- Modified `.pre-commit-config.yaml` to use the custom Prettier hook script
  for formatting instead of the standard Prettier mirror repository hook.

**Removed:**

- Removed the Prettier mirror repository hook from `.pre-commit-config.yaml`,
  replacing it with the custom implementation for better control over formatting.

These changes enhance code formatting consistency and improve the automation
in CI/CD pipeline through custom scripting and workflow adjustments.
  • Loading branch information
l50 committed Dec 16, 2023
1 parent 23216c6 commit 8882354
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ jobs:
run: mage installDeps

- name: Run pre-commit
run: pre-commit run --all-files
run: mage runPreCommit
38 changes: 38 additions & 0 deletions .hooks/prettier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -exo pipefail

# Check if npm is installed
if ! [ -x "$(command -v npm)" ]; then
echo 'Error: npm is not installed.' >&2
exit 1
else
# Check if Prettier is installed
if ! [ -x "$(command -v prettier)" ]; then
echo 'Error: Prettier is not installed.' >&2
echo 'Installing Prettier...'
npm install -g prettier
fi
fi

# Check if Prettier is installed
if ! [ -x "$(command -v prettier)" ]; then
echo 'Error: Prettier is not installed.' >&2
exit 1
fi

# Run Prettier on staged .json, .yaml, and .yml files
echo "Running Prettier on staged files..."

# List all staged files, filter for the desired extensions, and run Prettier
git diff --cached --name-only --diff-filter=d \
| grep -E '\.(json|ya?ml)$' \
| xargs -I {} prettier --write {}

# Add the files back to staging area as Prettier may have modified them
git diff --name-only --diff-filter=d \
| grep -E '\.(json|ya?ml)$' \
| xargs git add

echo "Prettier formatting completed."

exit 0
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ repos:
- id: yamllint
entry: yamllint --strict -c .hooks/linters/yamllint.yaml

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.6
hooks:
- id: prettier
files: \.(json|yaml|yml)$

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
Expand All @@ -43,7 +37,7 @@ repos:
- id: go-critic
- id: go-build
- id: go-mod-tidy

- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 3.0.0
hooks:
Expand Down Expand Up @@ -86,3 +80,9 @@ repos:
language: script
entry: .hooks/generate-docs.sh
require_serial: true

- id: prettier
name: Run prettier
entry: .hooks/prettier-hook.sh
language: script
types: [json, yaml]

0 comments on commit 8882354

Please sign in to comment.