Skip to content

Instantly share code, notes, and snippets.

@fangbinwei
Created October 1, 2019 13:08
Show Gist options
  • Save fangbinwei/76b3fb9309e8e3f1f4aa137bc2936e5a to your computer and use it in GitHub Desktop.
Save fangbinwei/76b3fb9309e8e3f1f4aa137bc2936e5a to your computer and use it in GitHub Desktop.
git pre-commit hooks for detecting 'console.log'
const shell = require('shelljs')
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
shell.exec('git diff --cached',{silent: true}, async (code, stdout, stderr) => {
if (code !== 0) return
if (!stdout.match(/console\.log/)) return
console.log('There are some occurrences of console.log at your modification.\nYou can use git-cz --no-verify to commit')
shell.exit(1)
})
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
consoleregexp='console.log'
# CHECK
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
exit 0; #THE USER WANTS TO CONTINUE
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment