Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor js registration and generator implementation #140

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions lib/generators/rbui/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
29 changes: 5 additions & 24 deletions lib/generators/rbui/templates/index.js.tt
Original file line number Diff line number Diff line change
@@ -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);
104 changes: 41 additions & 63 deletions lib/rbui/index.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 };