Reputation: 20761
I'm currently trying to enforce that me or my colleague cannot commit files that contains console.log in my angular application.
I currently already have husky
on pre-commit, that execute a ng lint --fix
.
Is there a way to either add something to my linting to prevent console log, or to add something in the husky script?
People should still be able to use console.log, just not commit it.
Thanks
Upvotes: 5
Views: 4273
Reputation: 351
With husky you can add the following to your pre-commit
file:
# Get the current branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "Committing to $current_branch branch... running pre-commit check..."
# Check for console.log statements in the staged files
if git diff --cached --name-only | xargs grep -n 'console.log'; then
echo "console.log found. Aborting commit."
exit 1
else
echo "pre-commit check passed. Committing to $current_branch branch."
fi
That will check for any "console.log" strings in your staged changes and block the commit if it finds any.
Upvotes: 1
Reputation: 4493
You can also create a .gitignore
file contaninig this:
console.log
With this your log file will be generated, but not commited.
EDIT
You can't ignore file by content with gitignore.
Instead, you can exlude files with shell script, like this:
for file in $(git grep -l --cached 'console.log') ; do
git rm --cached $file
done
Upvotes: -1
Reputation: 2234
You can go to your project's tslint.json
file and make sure this option is present in your file:
{
"rules": {
"no-console": true
}
}
If you prefer something that won't "block" you but just warn you, you also can set this option as follows:
{
"rules": {
"no-console": {
"severity": "warning",
}
}
}
And finally, if you want to target some console
functions more precisely, you can specify them like this:
{
"rules": {
"no-console": {
"severity": "warning",
"options": [
"log",
"error",
"debug",
"info",
"time",
"timeEnd",
"trace"
]
}
}
}
Upvotes: 4