Module: PrecompiledAssets::Tasks

Defined in:
lib/precompiled_assets/tasks.rb

Class Method Summary collapse

Class Method Details

.remove_allObject



6
7
8
# File 'lib/precompiled_assets/tasks.rb', line 6

def remove_all
  LocalPath.pathname.glob('*').each(&:rmtree)
end

.remove_unusedObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/precompiled_assets/tasks.rb', line 10

def remove_unused
  manifest = Manifest.new

  pathnames = LocalPath.pathname.glob('**/*').sort

  # Remove all files not listed in the manifest; also keep the manifest file itself.
  pathnames.select(&:file?).each do |asset_pathname|
    next if manifest.pathname == asset_pathname
    next if manifest.includes_digested_path?(asset_pathname)
    next if manifest.includes_digested_path?(asset_pathname.to_s.delete_suffix('.map'))

    puts "Removing unused asset: #{asset_pathname}"
    asset_pathname.delete
  end

  # After removing all unused files, remove any empty directories that remain.
  # Iterating in reverse to start with most deeply nested directories first, allow to clean up empty branches.
  pathnames.select(&:directory?).reverse_each do |directory|
    next unless directory.empty?

    puts "Removing empty directory: #{directory}"
    directory.rmdir
  end
end