Sha256: 499bd207cb744b548b05704fc7b27be0fdcbde656a6f330fcc5f11b20692d3ed

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module Overcommit::Hook::PreCommit
  # Checks for images that can be optimized with `image_optim`.
  class ImageOptim < Base
    def run
      begin
        require 'image_optim'
      rescue LoadError
        return :warn, 'image_optim not installed -- run `gem install image_optim`'
      end

      optimized_images =
        begin
          optimize_images(applicable_files)
        rescue ::ImageOptim::BinResolver::BinNotFound => e
          return :fail, "#{e.message}. The image_optim gem is dependendent on this binary."
        end

      if optimized_images.any?
        return :fail,
          "The following images are optimizable:\n#{optimized_images.join("\n")}" \
          "\n\nOptimize them by running:\n" \
          "  image_optim --skip-missing-workers #{optimized_images.join(' ')}"
      end

      :pass
    end

    private

    def optimize_images(image_paths)
      image_optim = ::ImageOptim.new(skip_missing_workers: true)

      optimized_images =
        image_optim.optimize_images(image_paths) do |path, optimized|
          path if optimized
        end

      optimized_images.compact
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
overcommit-0.22.0 lib/overcommit/hook/pre_commit/image_optim.rb
jawshooah-overcommit-0.22.0 lib/overcommit/hook/pre_commit/image_optim.rb
overcommit-0.21.0 lib/overcommit/hook/pre_commit/image_optim.rb
overcommit-0.20.0 lib/overcommit/hook/pre_commit/image_optim.rb