From 69521006549244754bbf2d6be04e52fbc21ed88a Mon Sep 17 00:00:00 2001 From: Alex Dunae Date: Mon, 2 Dec 2019 16:30:23 -0800 Subject: [PATCH] Ignore missing files when cleaning old assets Fixes #2372 --- lib/webpacker/commands.rb | 16 +++++++++++++--- test/command_test.rb | 10 ++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/webpacker/commands.rb b/lib/webpacker/commands.rb index 8203dc305..c7b554113 100644 --- a/lib/webpacker/commands.rb +++ b/lib/webpacker/commands.rb @@ -10,15 +10,21 @@ def clean(count_to_keep = 2) files_in_manifest = process_manifest_hash(manifest.refresh) files_to_be_removed = 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| + + versions_of_file(file_prefix, file_ext).map do |version_of_file| next if version_of_file == file_in_manifest [version_of_file, File.mtime(version_of_file).utc.to_i] end.compact.sort_by(&:last).reverse.drop(count_to_keep).map(&:first) end - files_to_be_removed.each { |f| File.delete f } + files_to_be_removed.each do |f| + begin + File.delete f + rescue Errno::ENOENT + nil + end + end end end @@ -45,4 +51,8 @@ def process_manifest_hash(manifest_hash) File.join(config.root_path, "public", value) end.flatten end + + def versions_of_file(file_prefix, file_ext) + Dir.glob("#{file_prefix}*#{file_ext}").grep(/#{file_prefix}[0-9a-f]{20}#{file_ext}/) + end end diff --git a/test/command_test.rb b/test/command_test.rb index 236ce7d11..a33d587b6 100644 --- a/test/command_test.rb +++ b/test/command_test.rb @@ -30,4 +30,14 @@ def test_clean_command_works_with_nested_hashes_and_without_any_compiled_files assert Webpacker.commands.clean end end + + def test_clean_command_does_not_fail_on_missing_files + Webpacker.commands.stub(:versions_of_file, %w[v1 v2 v3]) do + File.stub(:mtime, OpenStruct.new(utc: 0)) do + File.stub :delete, lambda { |n| raise Errno::ENOENT } do + Webpacker.clean + end + end + end + end end