lib/lookbook/engine.rb in lookbook-1.1.1 vs lib/lookbook/engine.rb in lookbook-1.2.0
- old
+ new
@@ -4,34 +4,37 @@
module Lookbook
class Engine < Rails::Engine
isolate_namespace Lookbook
- config.autoload_paths << File.expand_path(Lookbook::Engine.root.join("app/components"))
- config.lookbook = Lookbook.config
+ config.autoload_paths << File.expand_path(root.join("app/components"))
initializer "lookbook.viewcomponent.config" do
- config.lookbook.preview_paths += config.view_component.preview_paths
- config.lookbook.preview_controller ||= config.view_component.preview_controller
+ Lookbook.config.preview_paths += config.view_component.preview_paths
+ Lookbook.config.preview_controller ||= config.view_component.preview_controller
- config.lookbook.components_path = config.view_component.view_component_path if config.view_component.view_component_path.present?
+ Lookbook.config.components_path = config.view_component.view_component_path if config.view_component.view_component_path.present?
- config.lookbook.listen_paths += config.lookbook.preview_paths
- config.lookbook.listen_paths << config.lookbook.components_path
+ Lookbook.config.listen_paths += Lookbook.config.preview_paths
+ Lookbook.config.listen_paths << Lookbook.config.components_path
end
initializer "lookbook.parser.tags" do
- Lookbook::Parser.define_tags(Lookbook.config.preview_tags)
+ Lookbook::Parser.define_tags(Engine.tags)
end
initializer "lookbook.assets.serve" do
config.app_middleware.use(
Rack::Static,
- urls: ["/lookbook-assets"], root: Lookbook::Engine.root.join("public").to_s
+ urls: ["/lookbook-assets"], root: root.join("public").to_s
)
end
+ config.before_configuration do
+ config.lookbook = Lookbook.config
+ end
+
config.after_initialize do
@preview_controller = Lookbook.config.preview_controller.constantize
@preview_controller.include(Lookbook::PreviewController)
parser.after_parse do |registry|
@@ -44,34 +47,34 @@
Rails.application.server do
init_listeners
end
else
# Fallback for older Rails versions - don't start listeners if running in a rake task.
- unless Lookbook::Engine.prevent_listening?
+ unless prevent_listening?
init_listeners
end
end
parser.parse do
- Lookbook::Engine.run_hooks(:after_initialize)
+ run_hooks(:after_initialize)
end
end
at_exit do
- if Lookbook::Engine.listeners.any?
+ if listeners.any?
Lookbook.logger.debug "Stopping listeners"
- Lookbook::Engine.stop_listeners
+ stop_listeners
end
- Lookbook::Engine.run_hooks(:before_exit)
+ run_hooks(:before_exit)
end
class << self
def init_listeners
config = Lookbook.config
return unless config.listen == true
- listen_paths = config.listen_paths.uniq
+ listen_paths = PathUtils.normalize_all(config.listen_paths)
if listen_paths.any?
preview_listener = Listen.to(*listen_paths,
only: /\.(#{config.listen_extensions.join("|")})$/,
wait_for_delay: 0.5,
force_polling: config.listen_use_polling) do |modified, added, removed|
@@ -80,11 +83,11 @@
end
end
register_listener(preview_listener)
end
- page_paths = config.page_paths.uniq
+ page_paths = PathUtils.normalize_all(config.page_paths)
if page_paths.any?
page_listener = Listen.to(*page_paths,
only: /\.(html.*|md.*)$/,
force_polling: config.listen_use_polling) do |modified, added, removed|
changes = {modified: modified, added: added, removed: removed}
@@ -115,23 +118,24 @@
ws
end
end
def websocket_mount_path
- "#{mounted_path}#{config.lookbook.cable_mount_path}".gsub("//", "/") if websocket?
+ "#{mounted_path}/cable".gsub("//", "/") if websocket?
end
def websocket?
websocket.present?
end
def mounted_path
- Lookbook::Engine.routes.find_script_name({})
+ routes.find_script_name({})
end
def parser
- @parser ||= Lookbook::Parser.new(config.lookbook.preview_paths)
+ preview_paths = PathUtils.normalize_all(Lookbook.config.preview_paths)
+ @parser ||= Lookbook::Parser.new(preview_paths)
end
def log_level
Lookbook.logger.level
end
@@ -157,11 +161,11 @@
def stop_listeners
listeners.each { |listener| listener.stop }
end
def run_hooks(event_name, *args)
- config.lookbook.hooks[event_name].each do |hook|
+ hooks.for_event(event_name).each do |hook|
hook.call(Lookbook, *args)
end
end
def reload_ui
@@ -176,9 +180,25 @@
if defined?(Rake) && Rake.respond_to?(:application)
File.basename($0) == "rake" || Rake.application.top_level_tasks.any?
else
false
end
+ end
+
+ def panels
+ @panels ||= PanelStore.init_from_config
+ end
+
+ def inputs
+ @inputs ||= InputStore.init_from_config
+ end
+
+ def tags
+ @tags ||= TagStore.init_from_config
+ end
+
+ def hooks
+ @hooks ||= HookStore.init_from_config
end
attr_reader :preview_controller
end
end