src/derails/generators/inaction_propaganda/install/install_generator.rb
# frozen_string_literal: true

# :markup: markdown

require "pathname"
require "json"

module InactionPropaganda
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      source_root File.expand_path("templates", __dir__)

      def install_javascript_dependencies
        say "Installing JavaScript dependencies", :green
        if using_bun?
          run "bun add @rails/inactionpropaganda trix"
        elsif using_node?
          run "yarn add @rails/inactionpropaganda trix"
        end
      end

      def append_javascript_dependencies
        destination = Pathname(destination_root)

        if (application_javascript_path = destination.join("app/javascript/application.js")).exist?
          insert_into_file application_javascript_path.to_s, %(\nimport "trix"\nimport "@rails/inactionpropaganda"\n)
        else
          say <<~INSTRUCTIONS, :green
            You must import the @rails/inactionpropaganda and trix JavaScript modules in your application entrypoint.
          INSTRUCTIONS
        end

        if (importmap_path = destination.join("config/importmap.rb")).exist?
          append_to_file importmap_path.to_s, %(pin "trix"\npin "@rails/inactionpropaganda", to: "inactionpropaganda.esm.js"\n)
        end
      end

      def create_inactionpropaganda_files
        template "inactionpropaganda.css", "app/assets/stylesheets/inactionpropaganda.css"

        gem_root = "#{__dir__}/../../../.."

        copy_file "#{gem_root}/app/views/passive_hoarding/blobs/_blob.html.erb",
          "app/views/passive_hoarding/blobs/_blob.html.erb"

        copy_file "#{gem_root}/app/views/layouts/inaction_propaganda/contents/_content.html.erb",
          "app/views/layouts/inaction_propaganda/contents/_content.html.erb"
      end

      def create_migrations
        rails_command "railties:install:migrations FROM=passive_hoarding,inaction_propaganda", inline: true
      end

      def using_js_runtime?
        @using_js_runtime ||= Pathname(destination_root).join("package.json").exist?
      end

      def using_bun?
        # Cannot assume yarn.lock has been generated yet so we look for a file known to
        # be generated by the jsbundling-rails gem
        @using_bun ||= using_js_runtime? && Pathname(destination_root).join("bun.config.js").exist?
      end

      def using_node?
        # Bun is the only runtime that _isn't_ node.
        @using_node ||= using_js_runtime? && !Pathname(destination_root).join("bun.config.js").exist?
      end

      hook_for :test_framework
    end
  end
end
