Sha256: f558c8af6a9bd2adaa63d25f43943a9ff4fee15d9e0898de3f94e5c4b136babd

Contents?: true

Size: 1.83 KB

Versions: 110

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

110 entries across 110 versions & 2 rubygems

Version Path
neetob-ud-0.2.0 scripts/delete_unused_assets.rb
neetob-ud-0.1.3 scripts/delete_unused_assets.rb
neetob-ud-0.1.2 scripts/delete_unused_assets.rb
neetob-ud-0.1.1 scripts/delete_unused_assets.rb
neetob-ud-0.1.0 scripts/delete_unused_assets.rb
neetob-0.4.3 scripts/delete_unused_assets.rb
neetob-0.4.2 scripts/delete_unused_assets.rb
neetob-0.4.1 scripts/delete_unused_assets.rb
neetob-0.4.0 scripts/delete_unused_assets.rb
neetob-0.3.2 scripts/delete_unused_assets.rb
neetob-0.3.1 scripts/delete_unused_assets.rb
neetob-0.3.0 scripts/delete_unused_assets.rb
neetob-0.2.7 scripts/delete_unused_assets.rb
neetob-0.2.6 scripts/delete_unused_assets.rb
neetob-0.2.5 scripts/delete_unused_assets.rb
neetob-0.2.4 scripts/delete_unused_assets.rb
neetob-0.2.3 scripts/delete_unused_assets.rb
neetob-0.2.2 scripts/delete_unused_assets.rb
neetob-0.2.1 scripts/delete_unused_assets.rb
neetob-0.2.0 scripts/delete_unused_assets.rb