Last active
March 6, 2020 16:19
-
-
Save amkisko/126461451ab3e8ca8401287466dd48e7 to your computer and use it in GitHub Desktop.
Sample pre-commit ruby script
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 ruby | |
require "rubygems" | |
require "bundler/setup" | |
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")) | |
$LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir) | |
# NOTE: https://github.com/piotrmurach/tty-command | |
require "tty-command" | |
# NOTE: https://github.com/piotrmurach/tty-logger | |
require "tty-logger" | |
# NOTE: https://github.com/piotrmurach/tty-prompt | |
require "tty-prompt" | |
require "bundler/audit/cli" | |
require "brakeman" | |
require "rubocop" | |
require "benchmark" | |
logger = TTY::Logger.new do |config| | |
config.level = ENV["DEBUG"].nil? ? :info : :debug | |
end | |
class << logger | |
def <<(msg) | |
msg.each_line do |line| | |
debug(line.rstrip) | |
end | |
end | |
end | |
cmd = TTY::Command.new(output: logger) | |
prompt = TTY::Prompt.new | |
STDIN.reopen("/dev/tty") | |
exit 0 unless prompt.yes?("Run checks?", default: false) | |
cmd.run!("bundle audit update -q") | |
out, err = cmd.run("bundle audit") | |
puts(out) | |
exit(1) if err | |
result = Brakeman.run "." | |
puts(result.warnings) | |
puts(result.errors) | |
exit(1) if err | |
rb_changes = !cmd.run!("git diff --name-only HEAD | grep .rb").failure? | |
if rb_changes | |
rubocop_cli = RuboCop::CLI.new | |
result = rubocop_cli.run | |
exit result unless result == 0 | |
end | |
exit 0 |
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
{ | |
"husky": { | |
"hooks": { | |
"pre-commit": "bin/pre-commit", | |
"pre-push": "bin/pre-push" | |
} | |
}, | |
"dependencies": { | |
"@rails/webpacker": "^4.0.7" | |
}, | |
"devDependencies": { | |
"husky": "^4.2.3", | |
"webpack-dev-server": "^3.8.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment