Sha256: f558c8af6a9bd2adaa63d25f43943a9ff4fee15d9e0898de3f94e5c4b136babd

Contents?: true

Size: 1.83 KB

Versions: 84

Compression:

Stored size: 1.83 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require "set"

IMAGE_DIR_PATH = "app/assets/images"
SRC_DIR_PATH = "app/javascript/src"
VIEWS_DIR_PATH = "app/views"

def list_image_files(dir_path)
  Dir.glob("#{dir_path}/**/*").select { |file| File.file?(file) }
end

def find_unused_images(dir, images, is_views_path = false)
  new_images_set = Set.new(images)
  Dir.glob("#{dir}/**/*") do |file|
    if File.file?(file)
      File.open(file, "r") do |file_content|
        new_images_set.each do |image_file_path|
          destructured_image_path = split_image_path(is_views_path, image_file_path)
          if image_file_imported(file_content.read, destructured_image_path, is_views_path)
            new_images_set.delete(image_file_path)
          end
          file_content.rewind
        end
      end
    end
  end
  new_images_set
end

def image_file_imported(file, image_path, is_views_path)
  if is_views_path
    regex = create_exact_string_regex_for_views_files(image_path)
  else
    regex = create_exact_string_regex_for_src_files(image_path)
  end
  file.match(regex)
end

def create_exact_string_regex_for_src_files(str)
  /import .* from \"#{str}\"\;/
end

def create_exact_string_regex_for_views_files(str)
  /\"#{str}\"/
end

def split_image_path(is_views_path, image_file_path)
  if is_views_path
    image_file_path.split("images/").last
  else
    image_file_path[image_file_path.index("images")..image_file_path.rindex(".") - 1]
  end
end

def delete_unused_images(image_file_path, src_path, views_path)
  images = Set.new(list_image_files(image_file_path))
  images = find_unused_images(src_path, images)
  images = find_unused_images(views_path, images, is_views_path = true)
  images.each do |image_path|
    File.delete(image_path)
    puts "Deleted #{image_path}"
  end
end

delete_unused_images(IMAGE_DIR_PATH, SRC_DIR_PATH, VIEWS_DIR_PATH)

Version data entries

84 entries across 84 versions & 2 rubygems

Version Path
neetob-0.4.34 scripts/delete_unused_assets.rb
neetob-0.4.33 scripts/delete_unused_assets.rb
neetob-0.4.32 scripts/delete_unused_assets.rb
neetob-0.4.31 scripts/delete_unused_assets.rb
neetob-0.4.30 scripts/delete_unused_assets.rb
neetob-0.4.28 scripts/delete_unused_assets.rb
neetob-0.4.27 scripts/delete_unused_assets.rb
neetob-0.4.26 scripts/delete_unused_assets.rb
neetob-0.4.24 scripts/delete_unused_assets.rb
neetob-0.4.23 scripts/delete_unused_assets.rb
neetob-0.4.22 scripts/delete_unused_assets.rb
neetob-0.4.21 scripts/delete_unused_assets.rb
neetob-0.4.20 scripts/delete_unused_assets.rb
neetob-0.4.19 scripts/delete_unused_assets.rb
neetob-0.4.16 scripts/delete_unused_assets.rb
neetob-0.4.15 scripts/delete_unused_assets.rb
neetob-0.4.14 scripts/delete_unused_assets.rb
neetob-0.4.13 scripts/delete_unused_assets.rb
neetob-0.4.12 scripts/delete_unused_assets.rb
neetob-0.4.11 scripts/delete_unused_assets.rb