File: pngquant

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (63 lines) | stat: -rwxr-xr-x 1,597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rails'
require 'png_quantizator'
require 'parallel'
require_relative '../tooling/lib/tooling/image'

return if Rails.env.production?

def usage
  puts <<~EOF
    Usage: #{$0} [compress|lint] [PATH..]

      compress  Compress all documentation PNG images using pngquant.
      lint      Checks that all documentation PNG images have been compressed with pngquant.

      PATH      One or more files or directories. If empty, `doc/**/*.png` is used.

  EOF
end

command = ARGV.shift
paths = ARGV.presence || ['doc']

files = paths.flat_map do |path|
  File.directory?(path) ? Dir.glob(File.join(path, '/**/*.png')) : path
end

case command
when 'compress'
  puts "Compressing #{files.size} PNG files"

  Parallel.each(files) do |file|
    was_uncompressed, savings = Tooling::Image.compress_image(file)

    if was_uncompressed
      puts "#{file} was reduced by #{savings} bytes"
    end
  end
when 'lint'
  puts "Checking #{files.size} PNG files"

  uncompressed_files = Parallel.map(files) do |file|
    is_uncompressed, _ = Tooling::Image.compress_image(file, true)
    if is_uncompressed
      puts Rainbow("Uncompressed file detected: ").red + file
      file
    end
  end.compact

  if uncompressed_files.empty?
    puts Rainbow("All documentation images are optimally compressed!").green
  else
    warn(
      Rainbow("The #{uncompressed_files.size} image(s) above have not been optimally compressed using pngquant.").red,
      'Please run "bin/pngquant compress" and commit the result.'
    )
    abort
  end
else
  usage
end