Skip to content

Instantly share code, notes, and snippets.

@bsa7
Last active March 29, 2017 15:05
Show Gist options
  • Save bsa7/d04012d2925dcd262939ea907fd4b6c5 to your computer and use it in GitHub Desktop.
Save bsa7/d04012d2925dcd262939ea907fd4b6c5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'colorize'
def show_process(errors)
if errors.length == 0
print '.'.green
else
print '.'.red
end
end
puts "#{'=' * 11} pre-commit hook (ruby) #{'=' * 11}"
output = `git status`
output_parsed = output.scan(/^[\s]+[a-z\s]+:[^\n\r]+$/)
output_parsed.reject! { |x| x[/deleted:\s/] }
output_parsed.map!{|str| str.sub(/^.+?:\s+/, '').gsub(/[\n\r]/, '')}
errors_js = []
errors_rb = []
output_js = []
output_rb = []
output_parsed.each do |file_name|
file_extension = file_name[/\.[^\.]+$/]
if ['.es6', '.jsx', '.js'].include?(file_extension)
command = "./front/node_modules/.bin/eslint #{file_name}"
output = []
output << 'Команда: ' + command.yellow
check_output = `#{command}`
output << check_output.gsub(/error(s?)/, 'error\1'.red)
.gsub(/warning(s?)/, 'warning\1'.cyan)
errors = check_output.scan(/[\s\t]+error[\s\t]+/)
errors_js << errors
show_process(errors)
output_js << output
elsif ['.rb', '.rake'].include?(file_extension)
command = "rubocop #{file_name}"
output = []
output << 'Команда: ' + command.yellow
check_output = `#{command}`
output << check_output.gsub(/^(.+?:\d+:\d+:)/, '\1'.cyan)
.gsub(/ (C:) /, ' \1 '.yellow)
.gsub(/ (W:) /, ' \1 '.magenta)
.gsub(/(\^+)/m, '\1'.light_black)
errors = check_output.scan(/:\d+:\d+:/)
errors_rb << errors
show_process(errors)
output_rb << output
end
end
puts ''
if errors_js.reject(&:empty?).length > 0
puts "Количество ошибок в js: #{errors_js.length}, коммит не сделан.".red
puts 'Ошибки в javascript коде:'
errors_js.each_with_index do |errors, index|
if errors.length > 0
puts output_js[index]
end
end
# exit(1)
end
if errors_rb.reject(&:empty?).length > 0
puts "Количество ошибок в ruby: #{errors_rb.length}, коммит не сделан.".red
puts 'Ошибки в Ruby коде:'
errors_rb.each_with_index do |errors, index|
if errors.length > 0
puts output_rb[index]
end
end
# exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment