class Overcommit::Hook::PreCommit::ImageOptim

Checks for images that can be optimized with ‘image_optim`.

@see github.com/toy/image_optim

Public Instance Methods

run() click to toggle source
# File lib/overcommit/hook/pre_commit/image_optim.rb, line 8
def run
  result = execute(command, args: applicable_files)
  return [:fail, result.stdout + result.stderr] unless result.success?

  optimized_files = extract_optimized_files(result.stdout)
  return :pass if optimized_files.empty?

  output = "The following images are optimizable:\n#{optimized_files.join("\n")}"
  output += "\n\nOptimize them by running `#{command.join(' ')} #{optimized_files.join(' ')}`"
  [:fail, output]
end

Private Instance Methods

extract_optimized_files(output) click to toggle source
# File lib/overcommit/hook/pre_commit/image_optim.rb, line 22
def extract_optimized_files(output)
  output.split("\n").
         select { |line| line =~ /^\d+/ }.
         map    { |line| line.split(/\s+/).last }
end