Skip to content

Instantly share code, notes, and snippets.

@iberianpig
Created August 16, 2017 08:44
Show Gist options
  • Save iberianpig/0041ba445832181c0ad9dd712bcd2e29 to your computer and use it in GitHub Desktop.
Save iberianpig/0041ba445832181c0ad9dd712bcd2e29 to your computer and use it in GitHub Desktop.
run lints for rails with pre-commit
#!/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