diff --git a/lib/generators/rbui/component_generator.rb b/lib/generators/rbui/component_generator.rb index a1ed956..58db059 100644 --- a/lib/generators/rbui/component_generator.rb +++ b/lib/generators/rbui/component_generator.rb @@ -35,19 +35,15 @@ def copy_component_files def update_index_file index_path = File.join(destination_root, "app/components/rbui/index.js") + content = File.read(index_path) - update_last_updated_date(content) - update_controller_registration(content) + add_controller_registration(content) File.write(index_path, content) end - def update_last_updated_date(content) - content.sub!(/Last updated: .*/, "Last updated: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}") - end - - def update_controller_registration(content) + def add_controller_registration(content) all_js_controllers = Dir.glob(File.join(destination_path, "**", "*_controller.js")) # Collect all valid controller information @@ -61,7 +57,7 @@ def update_controller_registration(content) { import: "import #{new_controller} from \"#{new_path}\";", - registration: "RBUI.unload(\"#{registration_name}\");\napplication.register(\"#{registration_name}\", #{new_controller});", + registration: "application.register(\"#{registration_name}\", #{new_controller});", export: "export { default as #{new_controller} } from \"#{new_path}\";" } end @@ -77,9 +73,9 @@ def update_controller_registration(content) content.sub!(/\/\/ Register all controllers.*?(?=\n\n)/m, "// Register all controllers\n#{registration_block}") # Update exports - exports = valid_controllers.map { |c| c[:export] }.sort - export_block = exports.join("\n") - content.sub!(/\/\/ Export all controllers.*?(?=\n\n)/m, "// Export all controllers so user of npm package can lazy load controllers\n#{export_block}") + # exports = valid_controllers.map { |c| c[:export] }.sort + # export_block = exports.join("\n") + # content.sub!(/\/\/ Export all controllers.*?(?=\n\n)/m, "// Export all controllers so user of npm package can lazy load controllers\n#{export_block}") content end diff --git a/lib/generators/rbui/templates/index.js.tt b/lib/generators/rbui/templates/index.js.tt index 1199605..ff6e702 100644 --- a/lib/generators/rbui/templates/index.js.tt +++ b/lib/generators/rbui/templates/index.js.tt @@ -1,29 +1,10 @@ -/** - * IMPORTANT: DO NOT EDIT THIS FILE MANUALLY - * -------------------------------------------- - * This file is automatically generated and managed. - * - * Last updated: [DATE] - */ - -// Load and export all of the stimulus controllers -import { Application } from "@hotwired/stimulus"; - -const application = Application.start(); - -// Configure Stimulus development experience -application.debug = false; -window.Stimulus = application; - -import { application as RBUI } from "rbui-js"; +import { application } from "../../../app/javascript/controllers/application"; // Import all controller files +// import ComboboxController from "./combobox/combobox_controller"; // Register all controllers +// application.register("rbui--combobox", ComboboxController); -// Export all controllers so user of npm package can lazy load controllers - -// Export application -export { application }; - - +import RBUI from "rbui-js"; +RBUI.initialize(application); diff --git a/lib/rbui/index.js b/lib/rbui/index.js index 6bb21a2..838a546 100644 --- a/lib/rbui/index.js +++ b/lib/rbui/index.js @@ -1,12 +1,3 @@ -// Load and export all of the stimulus controllers -import { Application } from "@hotwired/stimulus"; - -const application = Application.start(); - -// Configure Stimulus development experience -application.debug = false; -window.Stimulus = application; - // Import all controller files import AccordionController from "./accordion/accordion_controller"; import AlertDialogController from "./alert_dialog/alert_dialog_controller"; @@ -34,59 +25,46 @@ import SelectItemController from "./select/select_item_controller"; import SheetController from "./sheet/sheet_controller"; import SheetContentController from "./sheet/sheet_content_controller"; -// Register all controllers -application.register("rbui--accordion", AccordionController); -application.register("rbui--alert-dialog", AlertDialogController); -application.register("rbui--calendar", CalendarController); -application.register("rbui--calendar-input", CalendarInputController); -application.register("rbui--collapsible", CollapsibleController); -application.register("rbui--chart", ChartController); -application.register("rbui--checkbox-group", CheckboxGroupController); -application.register("rbui--clipboard", ClipboardController); -application.register("rbui--combobox", ComboboxController); -application.register("rbui--combobox-content", ComboboxContentController); -application.register("rbui--combobox-item", ComboboxItemController); -application.register("rbui--command", CommandController); -application.register("rbui--context-menu", ContextMenuController); -application.register("rbui--dialog", DialogController); -application.register("rbui--dropdown-menu", DropdownMenuController); -application.register("rbui--form-field", FormFieldController); -application.register("rbui--hover-card", HoverCardController); -application.register("rbui--popover", PopoverController); -application.register("rbui--tabs", TabsController); -application.register("rbui--theme-toggle", ThemeToggleController); -application.register("rbui--tooltip", TooltipController); -application.register("rbui--select", SelectController); -application.register("rbui--select-item", SelectItemController); -application.register("rbui--sheet", SheetController); -application.register("rbui--sheet-content", SheetContentController); +function initialize(application) { + const registerIfNotExists = (identifier, controller) => { + if (!application.router.modulesByIdentifier.has(identifier)) { + application.register(identifier, controller); + // console.log(`Registered: ${identifier}`); + } + }; + + // Register all controllers + registerIfNotExists("rbui--accordion", AccordionController); + registerIfNotExists("rbui--alert-dialog", AlertDialogController); + registerIfNotExists("rbui--calendar", CalendarController); + registerIfNotExists("rbui--calendar-input", CalendarInputController); + registerIfNotExists("rbui--collapsible", CollapsibleController); + registerIfNotExists("rbui--chart", ChartController); + registerIfNotExists("rbui--checkbox-group", CheckboxGroupController); + registerIfNotExists("rbui--clipboard", ClipboardController); + registerIfNotExists("rbui--combobox", ComboboxController); + registerIfNotExists("rbui--combobox-content", ComboboxContentController); + registerIfNotExists("rbui--combobox-item", ComboboxItemController); + registerIfNotExists("rbui--command", CommandController); + registerIfNotExists("rbui--context-menu", ContextMenuController); + registerIfNotExists("rbui--dialog", DialogController); + registerIfNotExists("rbui--dropdown-menu", DropdownMenuController); + registerIfNotExists("rbui--form-field", FormFieldController); + registerIfNotExists("rbui--hover-card", HoverCardController); + registerIfNotExists("rbui--popover", PopoverController); + registerIfNotExists("rbui--tabs", TabsController); + registerIfNotExists("rbui--theme-toggle", ThemeToggleController); + registerIfNotExists("rbui--tooltip", TooltipController); + registerIfNotExists("rbui--select", SelectController); + registerIfNotExists("rbui--select-item", SelectItemController); + registerIfNotExists("rbui--sheet", SheetController); + registerIfNotExists("rbui--sheet-content", SheetContentController); +} + +const RBUI = { + initialize, +}; -// Export all controllers so user of npm package can lazy load controllers -export { default as AccordionController } from "./accordion/accordion_controller"; -export { default as AlertDialogController } from "./alert_dialog/alert_dialog_controller"; -export { default as CalendarController } from "./calendar/calendar_controller"; -export { default as CalendarInputController } from "./calendar/calendar_input_controller"; -export { default as CollapsibleController } from "./collapsible/collapsible_controller"; -export { default as ChartController } from "./chart/chart_controller"; -export { default as CheckboxGroupController } from "./checkbox/checkbox_group_controller"; -export { default as ClipboardController } from "./clipboard/clipboard_controller"; -export { default as ComboboxController } from "./combobox/combobox_controller"; -export { default as ComboboxContentController } from "./combobox/combobox_content_controller"; -export { default as ComboboxItemController } from "./combobox/combobox_item_controller"; -export { default as CommandController } from "./command/command_controller"; -export { default as ContextMenuController } from "./context_menu/context_menu_controller"; -export { default as DialogController } from "./dialog/dialog_controller"; -export { default as DropdownMenuController } from "./dropdown_menu/dropdown_menu_controller"; -export { default as FormFieldController } from "./form/form_field_controller"; -export { default as HoverCardController } from "./hover_card/hover_card_controller"; -export { default as PopoverController } from "./popover/popover_controller"; -export { default as TabsController } from "./tabs/tabs_controller"; -export { default as ThemeToggleController } from "./theme_toggle/theme_toggle_controller"; -export { default as TooltipController } from "./tooltip/tooltip_controller"; -export { default as SelectController } from "./select/select_controller"; -export { default as SelectItemController } from "./select/select_item_controller"; -export { default as SheetController } from "./sheet/sheet_controller"; -export { default as SheetContentController } from "./sheet/sheet_content_controller"; +export default RBUI; -// Export application -export { application }; +export { initialize };