/
commands.rb
49 lines (40 loc) · 1.48 KB
/
commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Webpacker::Commands
delegate :config, :compiler, :manifest, to: :@webpacker
def initialize(webpacker)
@webpacker = webpacker
end
def clean(count_to_keep = 2)
if config.public_output_path.exist? && config.public_manifest_path.exist?
files_in_manifest = process_manifest_hash(manifest.refresh)
files_to_be_removed = files_in_manifest.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}/)
next "" unless versions_of_file.any?
versions_of_file.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 }
end
end
def clobber
config.public_output_path.rmtree if config.public_output_path.exist?
config.cache_path.rmtree if config.cache_path.exist?
end
def bootstrap
manifest.refresh
end
def compile
compiler.compile.tap do |success|
manifest.refresh if success
end
end
private
def process_manifest_hash(manifest_hash)
manifest_hash.values.map do |value|
next process_manifest_hash(value) if value.is_a?(Hash)
File.join(config.root_path, "public", value)
end.flatten
end
end