lib/svelte_on_rails/installer/hello_world.rb



module SvelteOnRails
  module Installer
    module HelloWorld

      def self.install_hello_world(templates, force: false, app_root: nil, silent: false)

        utils_i = SvelteOnRails::Installer::Utils

        # write templates

        utils_i.write_templates(templates, ask_for_overwrite: !force, app_root: app_root, silent: silent)

        # route

        route = 'svelte_on_rails_hello_world#index'

        utils_i.add_route("  get \"#{route.sub('#', '/')}\"", app_root: app_root)
        utils_i.add_route("  get \"svelte_on_rails_hello_world/backend_frontend_rendered\"", app_root: app_root)
        utils_i.add_route("  get \"svelte_on_rails_hello_world/ssr_auto_rendered\"", app_root: app_root)

        rr = utils_i.which_root_route(app_root)
        root_url = "/"
        url = root_url + route.sub('#', '/')

        if rr && rr == route
          puts "Root route «#{route}» already exists, skipping."
          root_url
        elsif rr && utils_i.route_exists?(route.sub('#', '/'))
          puts "route «#{route}» already exists, skipping."
          url
        elsif rr
          url
        else
          utils_i.add_route('  root "svelte_on_rails_hello_world#index"', app_root: app_root)
          root_url
        end

      end

      def self.remove_hello_world(templates = ['rails_vite_hello_world'], app_root: nil, ask: true)

        utils = SvelteOnRails::Installer::Utils

        files = utils.template_paths(templates, app_root: app_root)

        existing_files = files.dup.select { |f| File.exist?(f[2]) }

        if ask && existing_files.any?
          question = "Remove Hello World component?\nThe following files will be removed:\n#{existing_files.map { |f| f[1] }.join("\n")}"
          return unless utils.ask_yn(question)
        end

        existing_files.each do |f|
          File.delete(f[2])
        end
        puts "Removed Hello World component files."

        utils.remove_line_from_file(
          utils.app_root_path(app_root).join('config/routes.rb'),
          "svelte_on_rails_hello_world",
          force: !ask,
        )

      end

    end
  end
end