Created
December 26, 2015 00:14
-
-
Save hazeledmands/d4b83629eb77de8b1e28 to your computer and use it in GitHub Desktop.
Pre-commit hooks to prevent .only and console.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# this lives in ./scripts/install_hooks.sh | |
SCRIPT_DIR=$(cd $(dirname "${0}") && pwd -L) | |
if [ "${NODE_ENV:="development"}" != "development" ]; then | |
echo 'Not in dev' | |
exit 0 | |
fi | |
# account for us potentially being in a submodule | |
GIT_DIR=`git rev-parse --git-dir` && test $GIT_DIR && test -d $GIT_DIR || { | |
echo 'No .git folder found' | |
exit 0 | |
} | |
echo "Located .git dir in $GIT_DIR." | |
echo 'Installing git hooks...' | |
rm -f "${GIT_DIR}/hooks"/* 2> /dev/null | |
for hook in `ls ${SCRIPT_DIR}/hooks` | |
do | |
echo " ${hook}" | |
ln -s "${SCRIPT_DIR}/hooks/${hook}" "${GIT_DIR}/hooks/" | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env coffee | |
# this lives in ./scripts/lint.coffee | |
fibrous = require 'fibrous' | |
glob = require 'glob' | |
path = require 'path' | |
fs = require 'fs' | |
lstream = require 'lstream' | |
optimist = require('optimist') | |
argv = optimist | |
.boolean('s').alias('s', 'stdin').describe('s', 'read from stdin') | |
.boolean('h').alias('h', 'help').describe('h', 'see help') | |
.argv | |
if argv.help | |
console.log optimist.help() | |
process.exit() | |
lintFiles = fibrous -> | |
cwd = path.join process.cwd(), (process.argv[2] or 'src') | |
files = glob.sync '**/*.coffee', {cwd} | |
files.forEach (file) -> | |
file = path.join cwd, file | |
stream = fs.createReadStream(file) | |
lintStream stream, file | |
lintStream = (stream, file) -> | |
stream = stream.pipe(new lstream()) | |
lineNum = 0 | |
stream.on 'data', (line) -> | |
lineNum++ | |
if line.match /(it|describe|given)\.only/ | |
console.log "`only` found in #{file ? 'input'}:#{lineNum}: \n#{line}" | |
process.exit(1) | |
if line.match /stopTest\(\)/ | |
console.log "`stopTest()` found in #{file ? 'input'}:#{lineNum}: \n#{line}" | |
process.exit(1) | |
if argv.stdin | |
process.stdin.resume() | |
lintStream process.stdin | |
else | |
lintFiles (err) -> | |
console.error err.toString() if err? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this lives in ./scripts/hooks/pre-commit.sh | |
exitcode=0 | |
if ! git diff --staged | ./scripts/lint.coffee -s; then | |
echo "If you commit anyway, this branch will still fail in CI!" | |
exitcode=1 | |
fi | |
# find staged changes that add a console.log | |
logs=`git diff --staged | grep -iEC6 "^\+.*\bconsole\.log\b"` | |
debugs=`git diff --staged | grep -iEC6 "^\+.*\b(debugHTML|debugger)\b"` | |
if [[ ! -z $logs ]]; then | |
echo "$logs" | |
echo | |
echo "Warning: You've added console.logs." | |
echo | |
exitcode=1 | |
fi | |
if [[ ! -z $debugs ]]; then | |
echo "$debugs" | |
echo | |
echo "Warning: You've added debugging statements." | |
echo | |
exitcode=1 | |
fi | |
# any files larger than 500KB? | |
if git diff --cached --name-only | sed -e "s/\(.*\)/'\1'/" | xargs -L 1 wc -c | grep -E "^\s+([5-9]\d{5}\d*)"; then | |
echo | |
echo "You're trying to commit file(s) larger than 500KB... is this intentional?" | |
echo | |
exitcode=1 | |
fi | |
if [[ $exitcode == 1 ]]; then | |
echo "Use --no-verify to commit anyways." | |
fi | |
exit $exitcode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment