Skip to content

Commit

Permalink
Clean task: keep the last two versions of the packs
Browse files Browse the repository at this point in the history
  • Loading branch information
ericboehs committed Oct 11, 2018
1 parent b508288 commit 8d5519a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/webpacker/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ def clean

files_on_filesystem = Dir.glob("#{config.public_output_path}/**/*").select { |f| File.file? f }
files_in_manifest = manifest.refresh.values.map { |f| File.join config.root_path, "public", f }

count_to_keep = 2
versions_to_keep = files_in_manifest.flat_map do |file_in_manifest|
file_prefix, file_ext = file_in_manifest.scan(/(.*)[0-9a-f]{20}(.*)/).first
versions_of_file = Dir.glob("#{file_prefix}*#{file_ext}").grep(/#{file_prefix}[0-9a-f]{20}#{file_ext}/)
versions_of_file.map do |version_of_file|
[version_of_file, File.mtime(version_of_file).utc.to_i]
end.sort_by(&:last).reverse.first(count_to_keep).map(&:first)
end

manifest_file = File.join config.public_output_path, "manifest.json"
files_to_be_removed = files_on_filesystem - files_in_manifest - [manifest_file]
files_to_be_removed = files_on_filesystem - files_in_manifest - versions_to_keep - [manifest_file]

files_to_be_removed.each { |f| File.delete f }
end
Expand Down

0 comments on commit 8d5519a

Please sign in to comment.