jshint

Posted on July 21, 2014

安装

  • 全局安装 npm install jshint -g

使用

  • jshint filename 如果filename是- 将等待标准输入

pre-commit

#!/bin/sh

files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" = "" ]; then
  exit 0
fi

if ! type "jshint" > /dev/null; then
  echo "\t\033[31mPlease install jshint first: npm install jshint -g\033[0m"
  exit 1
fi

pass=true

echo "\nValidating JavaScript:\n"

for file in ${files}; do
  result=$(jshint ${file})
  if [ "$result" = "" ]; then
    echo "\t\033[32mJSHint Passed: ${file}\033[0m"
  else
    echo "\t\033[31mJSHint Failed: ${file}\033[0m"
    pass=false
  fi
done

echo "\nJavaScript validation complete\n"

if ! $pass; then
  echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSHint but do not. Please fix the JSHint errors and try again.\n"
  exit 1
else
  echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi