Class: PhlexCustomElementGenerator::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/phlex_custom_element_generator/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

include Thor::Actions

Returns:

  • (Boolean)


17
18
19
# File 'lib/phlex_custom_element_generator/cli.rb', line 17

def self.exit_on_failure?
  true
end

Instance Method Details

#generate_components(manifest_path = "") ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/phlex_custom_element_generator/cli.rb', line 28

def generate_components(manifest_path = "")
  if manifest_path.to_s.chomp.strip == ""
    manifest_path = Prompts::TextPrompt.ask(
      label: "What is the file path of your custom elements manifest?",
      hint: "IE: ./node_modules/@shoelace-style/shoelace/dist/custom-elements.json",
      required: true
    )
  end

  directory = options[:directory]
  if directory.to_s.chomp.strip == ""
    directory = Prompts::TextPrompt.ask(
      label: "What directory should we place your components in?",
      hint: "IE: ./shoelace. Hit [Enter] to place them in the current directory.",
    )
  end

  directory = directory.to_s.chomp.strip
  if directory == ""
    directory = "."
  end
  FileUtils.mkdir_p(directory)


  namespaces = options[:namespaces]
  if namespaces.to_s.chomp.strip == ""
    namespaces = Prompts::TextPrompt.ask(
      label: "What namespaces should we place your components in? List should be comma separated.",
      hint: "IE: Views,Shoelace . Hit [Enter] to place them with no namespace.",
    )
  end

  namespaces = namespaces.split(/\s*,\s*/).reject { |str| str.to_s.chomp == "" }

  manifest = ManifestReader.new(manifest: manifest_path)
  manifest.find_all_custom_elements.each do |tag_name, mod|
    class_name = tag_name.split(/-/).map(&:capitalize).join("")

    # Symbolized attribute names
    attributes = mod["members"]
      .reject { |member| member["attribute"].to_s == "" }
      .map { |member| { attr_name: member["attribute"].gsub(/-/, "_").to_sym, default_value: member["default"] } }
      .sort_by { |member| member[:attr_name] }

    component = ComponentGenerator.new(
      class_name: class_name,
      tag_name: tag_name,
      namespaces: namespaces,
      attributes: attributes
    )
    file_path = File.join(directory, tag_name.gsub(/-/, "_") + ".rb")
    puts "Writing to: #{tag_name} to #{file_path}"
    File.write(file_path, component.create)
  end
end


98
99
100
101
102
103
104
105
106
107
# File 'lib/phlex_custom_element_generator/cli.rb', line 98

def print_phlex_registrations(manifest_path = "")
  if manifest_path.to_s.chomp.strip == ""
    manifest_path = Prompts::TextPrompt.ask(
      label: "What is the file path of your custom elements manifest?",
      hint: "IE: node_modules/@shoelace-style/shoelace/dist/custom-elements.json",
      required: true
    )
  end
  puts ManifestReader.new(manifest: manifest_path).list_tag_names.map { |tag_name| "register_element :" + tag_name.gsub(/-/, "_") }
end


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/phlex_custom_element_generator/cli.rb', line 85

def print_tag_names(manifest_path = "")
  if manifest_path.to_s.chomp.strip == ""
    manifest_path = Prompts::TextPrompt.ask(
      label: "What is the file path of your custom elements manifest?",
      hint: "IE: node_modules/@shoelace-style/shoelace/dist/custom-elements.json",
      required: true
    )
  end

  puts ManifestReader.new(manifest: manifest_path).list_tag_names
end