Created
August 16, 2017 08:44
-
-
Save iberianpig/0041ba445832181c0ad9dd712bcd2e29 to your computer and use it in GitHub Desktop.
run lints for rails with pre-commit
This file contains hidden or 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 ruby | |
| require "pry-byebug" | |
| # execute pre-commit hooks | |
| class PreCommitRunner | |
| def run | |
| check_eslint | |
| check_rubocop | |
| end | |
| def check_eslint | |
| files = `git diff --staged --name-only | grep -E '(\\.js)$'`.split("\n").join(" ") | |
| puts files | |
| return if files.empty? | |
| result = `eslint --fix #{files}` | |
| puts result | |
| return if result == '' # no printed | |
| return if result =~ /problems \(0 errors\,/ # check errors, ignore warns | |
| puts "ERROR: Check eslint hints." | |
| exit 1 # reject | |
| end | |
| def check_rubocop | |
| files = `git diff --staged --name-only | grep -E '(\\.rb)$'`.split("\n").join(" ") | |
| puts files | |
| return if files.empty? | |
| result = `bundle exec rubocop -RSD --force-exclusion #{files}` | |
| puts result | |
| return if result =~ /no offenses detected/ # check errors, ignore warns | |
| puts "ERROR: Check rubocop hints." | |
| puts "if you fix with autocorrect, run following code" | |
| puts "rubocop -a #{files}" | |
| exit 1 # reject | |
| end | |
| PreCommitRunner.new.run if __FILE__ == $PROGRAM_NAME | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment