Class: Antoinette::CLI::Commands::Config

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/antoinette/cli/commands.rb

Instance Method Summary collapse

Instance Method Details

#call(stdout:, custom_views: [], layout_dirs: []) ⇒ Object



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

def call(stdout:, custom_views: [], layout_dirs: [], **)
  out = Antoinette::CLI.output
  config_path = Rails.root.join("config", "antoinette.json")

  existing_config = if File.exist?(config_path)
    JSON.parse(File.read(config_path))
  else
    {}
  end
  existing_custom = existing_config["custom_view_paths"] || []
  existing_elm_path = existing_config["elm_path"] || "elm"
  existing_layout_dirs = existing_config["layout_dirs"] || []
  all_custom_views = (existing_custom + custom_views).uniq
  all_layout_dirs = (["app/views/layouts"] + existing_layout_dirs + layout_dirs).uniq

  analyzer = Antoinette::ElmAppUsageAnalyzer.new(
    layout_dirs: all_layout_dirs,
    custom_view_paths: all_custom_views
  )
  layout_resolver = Antoinette::LayoutResolver.new(
    layout_dirs: all_layout_dirs
  )
  weaver = Antoinette::Weaver.new(
    elm_analyzer: analyzer,
    layout_resolver: layout_resolver,
    custom_view_paths: all_custom_views
  )

  output = JSON.parse(weaver.generate_json)
  output["elm_path"] = existing_elm_path
  extra_layout_dirs = all_layout_dirs - ["app/views/layouts"]
  output["layout_dirs"] = extra_layout_dirs if extra_layout_dirs.any?
  json_output = JSON.pretty_generate(output)

  if stdout
    out.puts json_output
  else
    File.write(config_path, json_output)
    out.puts "Generated #{config_path}"
  end
end