Skip to content

Commit

Permalink
Add precommit hook for env files
Browse files Browse the repository at this point in the history
  • Loading branch information
motechFR committed Sep 6, 2023
1 parent d6624f2 commit a81a613
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

tsc --noEmit && npx lint-staged
tsc --noEmit

npx lint-staged

# List of files to check. These should already be in gitignore, but this provides an additional layer of protection.
# List of banned files to check against. Any staging (ex. .env.staging, .env.stg) or prod file will be rejected.
# To test this, try adding a file with the name .env.blocked to your project and add '.env.b' to the banned_files array
banned_files=(".env.p" ".env.s")

# Get list of staged files from git
staged_files=$(git diff --cached --name-only)

# Loop through each staged file
for staged_file in $staged_files; do
# Loop through each banned file
for banned_file in "${banned_files[@]}"; do
# Check if the staged file contains the banned file pattern
if grep -q "$banned_file" <<< "$staged_file"; then
printf "\n"
printf "\e[31mError: Banned file \e[1m'$staged_file'\e[0m\e[31m found in staged files\e[0m \n"
printf "Please remove it and try again.\n"
exit 1
fi
done
done

0 comments on commit a81a613

Please sign in to comment.