Class: Shadwire::Commands::Remove

Inherits:
Object
  • Object
show all
Defined in:
lib/shadwire/commands/remove.rb

Overview

Uninstalls one or more components. It deletes only files unique to the removed items — never the shared base (ui_component/ui_helper/shadwire.css) and never a file still listed by another installed entry — then prunes any emptied directory under the components root and drops the items from shadwire.json. Importmap pins that no remaining component uses are reported (not auto-removed — dep cleanup is left to the user). Confirms first unless --yes.

Instance Method Summary collapse

Constructor Details

#initialize(root:, names:, yes: false, registry: nil, json: false, ui: UI.new(yes:)) ⇒ Remove

Returns a new instance of Remove.



15
16
17
18
19
20
21
22
# File 'lib/shadwire/commands/remove.rb', line 15

def initialize(root:, names:, yes: false, registry: nil, json: false, ui: UI.new(yes:))
  @root = root.to_s
  @names = names
  @yes = yes
  @registry_override = registry
  @json = json
  @ui = ui
end

Instance Method Details

#callObject

Raises:



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
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shadwire/commands/remove.rb', line 24

def call
  raise Shadwire::Error, "remove requires at least one component name" if @names.empty?

  Project.new(@root).raise_unless_rails!
  config = Config.load(@root)
  client = RegistryClient.new(@registry_override || config.registry)
  @names.each do |name|
    raise Shadwire::Error, "#{name} is not installed" unless config.installed.key?(name)
  end

  base_targets = client.index.fetch("base").fetch("files").map { |file| file["target"] }
  removed_targets = targets_for(config, @names)
  remaining = config.installed.keys - @names
  other_targets = targets_for(config, remaining)

  deletable = (removed_targets - base_targets - other_targets).select { |target| on_disk?(target) }
  kept_shared = base_targets & removed_targets

  unless @yes || @ui.confirm?("Remove #{deletable.size} files for #{@names.join(", ")}?")
    @ui.say("Aborted; nothing removed.")
    return { removed: [], deleted: [], keptShared: kept_shared, unusedPins: [] }
  end

  deletable.each { |target| File.delete(File.join(@root, target)) }
  prune_empty_dirs(deletable, config)

  @names.each { |name| config.forget(name) }
  config.save

  result = {
    removed: @names,
    deleted: deletable,
    keptShared: kept_shared,
    unusedPins: unused_pins(client, remaining)
  }
  emit(result)
  result
end