Skip to content

Commit

Permalink
stimulus controller copy - closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
elvinaspredkelis committed Jan 2, 2025
1 parent f0ee22a commit a68d83f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ruby/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.ruby-lsp
.prettierrc
*.gem

# Just to make sure to ignore dummy files.
app
11 changes: 10 additions & 1 deletion ruby/lib/essence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ module Essence
autoload :Avatar, "essence/components/avatar"
autoload :Badge, "essence/components/badge"
autoload :Button, "essence/components/button"
autoload :Essence, "essence/components/essence" # Base component
# autoload :Essence, "essence/components/essence" # Base component
autoload :Link, "essence/components/link"
autoload :Row, "essence/components/row"
autoload :Skeleton, "essence/components/skeleton"

autoload :CLI, "essence/cli"

class << self
def root_path
File.dirname(__dir__)
end

# CONFIGURATION
def configuration
@configuration ||= Configuration.new
Expand Down Expand Up @@ -64,6 +68,11 @@ def components
name: "Skeleton",
class_name: "Essence::Skeleton",
stimulus: false
},
tabs: {
name: "Tabs",
class_name: "Essence::Tabs",
stimulus: true
}
}
end
Expand Down
16 changes: 13 additions & 3 deletions ruby/lib/essence/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module Commands
extend Dry::CLI::Registry

# Constants
STIMULUS_CONTROLLERS_DIR = Pathname.new(::Essence.root_path).join("essence/stimulus")
STIMULUS_CONTROLLERS_DESTINATION_DIR = Pathname.new(File.expand_path(Dir.pwd)).join(::Essence.configuration.stimulus_controller_path)

COMPONENTS_DIR = Pathname.new(File.expand_path("components", __dir__))
DESTINATION_DIR = Pathname.new(File.expand_path(Dir.pwd)).join(::Essence.configuration.phlex_components_path)

STIMULUS_CONTROLLERS_DIR = Pathname.new(File.expand_path(__dir__)).join("essence/stimulus")
STIMULUS_CONTROLLERS_DESTINATION_DIR = Pathname.new(File.expand_path(Dir.pwd)).join(::Essence.configuration.stimulus_controller_path)

COMPONENT_DEFINITION_PREFIX = "class Essence::"
COMPONENT_DEFINITION_SUFFIX = "< Essence::Essence"
PHLEX_COMPONENT_DEFINITION_PREFIX = "class Components::"
Expand All @@ -36,6 +36,7 @@ module Commands
private

# UTILITIES
# PHLEX COMPONENTS
def self.copy_component(component_name:)
source_path = COMPONENTS_DIR.join("#{component_name}.rb")
destination_path = DESTINATION_DIR.join("#{component_name}.rb")
Expand All @@ -62,6 +63,15 @@ def self.rename_component_file(from:, to:)

FileUtils.mv(from, to)
end

# STIMULUS CONTROLLERS
def self.copy_controller(component_name:)
source_path = STIMULUS_CONTROLLERS_DIR.join("#{component_name}_controller.js")
destination_path = STIMULUS_CONTROLLERS_DESTINATION_DIR.join("#{component_name}_controller.js")

FileUtils.mkdir_p(STIMULUS_CONTROLLERS_DESTINATION_DIR)
FileUtils.copy(source_path, destination_path)
end
end
end
end
12 changes: 10 additions & 2 deletions ruby/lib/essence/cli/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ class Add < Dry::CLI::Command

def call(*args)
component_name = args[0][:component_name]
key = component_name.to_sym
has_stimulus_controller = Essence.components[key][:stimulus] || false

return puts "> #{component_name}: component unsupported" unless Essence.component_names.include?(component_name.to_sym)
return puts "> #{component_name}: component unsupported" unless Essence.component_keys.include?(key)
puts "> [Essence] #{Essence.components[key][:name]}"

if has_stimulus_controller
Essence::CLI::Commands.copy_controller(component_name:)
puts "> [Stimulus] #{Essence::CLI::Commands::STIMULUS_CONTROLLERS_DESTINATION_DIR.join("#{component_name}_controller.js")}"
end

Essence::CLI::Commands.copy_component(component_name:)
Essence::CLI::Commands.replace_component_contents(component_name:)
Expand All @@ -20,7 +28,7 @@ def call(*args)
to: Essence::CLI::Commands::PHLEX_COMPONENT_DEFINITION_SUFFIX
)

puts "> Successfully added #{component_name} component to #{Essence::CLI::Commands::DESTINATION_DIR.join("#{component_name}.rb")}"
puts "> [Phlex] #{Essence::CLI::Commands::DESTINATION_DIR.join("#{component_name}.rb")}"
end
end
end
Expand Down

0 comments on commit a68d83f

Please sign in to comment.