Class: Shakapacker::Commands

Inherits:
Object
  • Object
show all
Defined in:
sig/shakapacker/commands.rbs,
lib/shakapacker/commands.rb

Overview

Build operation commands for Shakapacker

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Commands

Creates a new commands instance

Parameters:



4
5
6
# File 'sig/shakapacker/commands.rbs', line 4

def initialize(instance)
  @instance = instance
end

Instance Method Details

#bootstrapvoid

This method returns an undefined value.

Creates the default configuration files and directory structure



13
14
15
# File 'sig/shakapacker/commands.rbs', line 13

def bootstrap
  manifest.refresh
end

#clean(count = 2, age = 3600) ⇒ Boolean

Cleanup old assets in the compile directory

Parameters:

  • (Integer)
  • (Integer)

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shakapacker/commands.rb', line 18

def clean(count = 2, age = 3600)
  if config.public_output_path.exist? && config.manifest_path.exist?
    packs
      .map do |paths|
        paths.map { |path| [Time.now - File.mtime(path), path] }
        .sort
        .reject.with_index do |(file_age, _), index|
          file_age < age || index < count
        end
        .map { |_, path| path }
      end
      .flatten
      .compact
      .each do |file|
        if File.file?(file)
          File.delete(file)
          logger.info "Removed #{file}"
        end
      end
  end

  true
end

#clobbervoid

This method returns an undefined value.

Removes all compiled packs



10
11
12
13
# File 'sig/shakapacker/commands.rbs', line 10

def clobber
  config.public_output_path.rmtree if config.public_output_path.exist?
  config.cache_path.rmtree if config.cache_path.exist?
end

#compileBoolean

Compiles all webpack/rspack packs

Returns:

  • (Boolean)


16
17
18
19
20
# File 'sig/shakapacker/commands.rbs', line 16

def compile
  compiler.compile.tap do |success|
    manifest.refresh if success
  end
end

#compilerCompiler

Returns the compiler instance

Returns:



22
# File 'sig/shakapacker/commands.rbs', line 22

def compiler: () -> Compiler

#configConfiguration

Returns the configuration object

Returns:



19
# File 'sig/shakapacker/commands.rbs', line 19

def config: () -> Configuration

#current_versionArray[String]

Returns:

  • (Array[String])


69
70
71
72
73
74
75
76
77
78
# File 'lib/shakapacker/commands.rb', line 69

def current_version
  packs = manifest.refresh.values.map do |value|
    value = value["src"] if value.is_a?(Hash)
    next unless value.is_a?(String)

    File.join(config.root_path, "public", "#{value}*")
  end.compact

  Dir.glob(packs).uniq
end

#loggerActiveSupport::TaggedLogging

Returns the logger instance

Returns:

  • (ActiveSupport::TaggedLogging)


28
# File 'sig/shakapacker/commands.rbs', line 28

def logger: () -> ActiveSupport::TaggedLogging

#manifestManifest

Returns the manifest instance

Returns:



25
# File 'sig/shakapacker/commands.rbs', line 25

def manifest: () -> Manifest

#packsArray[String]

Returns:

  • (Array[String])


58
59
60
61
62
63
64
65
66
67
# File 'lib/shakapacker/commands.rb', line 58

def packs
  all_files       = Dir.glob("#{config.public_output_path}/**/*")
  manifest_config = Dir.glob("#{config.manifest_path}*")

  packs = all_files - manifest_config - current_version
  packs.reject { |file| File.directory?(file) }.group_by do |path|
    base, _, ext = File.basename(path).scan(/(.*)(-[\da-f]+)([.\w]+)/).flatten
    "#{File.dirname(path)}/#{base}#{ext}"
  end.values
end