Skip to content

Commit

Permalink
Make clean task simpler and fix issues
Browse files Browse the repository at this point in the history
Tidy up

Compact

Update message

Remove message

Inline
  • Loading branch information
gauravtiwari committed Dec 9, 2019
1 parent 417542c commit 8fb2632
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
6 changes: 5 additions & 1 deletion lib/tasks/webpacker/clean.rake
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
$stdout.sync = true

require "webpacker/configuration"

namespace :webpacker do
desc "Remove old compiled webpacks"
task :clean, [:keep] => ["webpacker:verify_install", :environment] do |_, args|
Webpacker.clean(Integer(args.keep || 2))
Webpacker.ensure_log_goes_to_stdout do
Webpacker.clean(Integer(args.keep || 2))
end
end
end

Expand Down
10 changes: 1 addition & 9 deletions lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
$stdout.sync = true

def ensure_log_goes_to_stdout
old_logger = Webpacker.logger
Webpacker.logger = ActiveSupport::Logger.new(STDOUT)
yield
ensure
Webpacker.logger = old_logger
end

def yarn_install_available?
rails_major = Rails::VERSION::MAJOR
rails_minor = Rails::VERSION::MINOR
Expand All @@ -27,7 +19,7 @@ namespace :webpacker do
desc "Compile JavaScript packs using webpack for production with digests"
task compile: ["webpacker:verify_install", :environment] do
Webpacker.with_node_env(ENV.fetch("NODE_ENV", "production")) do
ensure_log_goes_to_stdout do
Webpacker.ensure_log_goes_to_stdout do
if Webpacker.compile
# Successful compilation!
else
Expand Down
8 changes: 8 additions & 0 deletions lib/webpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def with_node_env(env)
ENV["NODE_ENV"] = original
end

def ensure_log_goes_to_stdout
old_logger = Webpacker.logger
Webpacker.logger = ActiveSupport::Logger.new(STDOUT)
yield
ensure
Webpacker.logger = old_logger
end

delegate :logger, :logger=, :env, to: :instance
delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance
delegate :bootstrap, :clean, :clobber, :compile, to: :commands
Expand Down
38 changes: 20 additions & 18 deletions lib/webpacker/commands.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
class Webpacker::Commands
delegate :config, :compiler, :manifest, to: :@webpacker
delegate :config, :compiler, :manifest, :logger, 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.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|
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)
def clean(count = 2)
if config.public_output_path.exist? && config.public_manifest_path.exist? && versions.count > count
versions.drop(count).flat_map(&:last).each do |file|
File.delete(file) if File.exist?(file)
logger.info "Removed #{file}"
end

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

true
end

def clobber
Expand All @@ -38,11 +32,19 @@ def compile
end

private
def process_manifest_hash(manifest_hash)
manifest_hash.values.map do |value|
next process_manifest_hash(value) if value.is_a?(Hash)
def versions
all_files = Dir.glob("#{config.public_output_path}/**/*")
manifest_config = Dir.glob("#{config.public_manifest_path}*")

packs = all_files - manifest_config - current_version
packs.group_by { |file| File.mtime(file).utc.to_i }.sort.reverse
end

def current_version
manifest.refresh.values.map do |value|
next if value.is_a?(Hash)

File.join(config.root_path, "public", value)
end.flatten
end.compact
end
end

0 comments on commit 8fb2632

Please sign in to comment.