module GeneratorHelper
def add_documentation_reference(message, source)
def add_documentation_reference(message, source) "#{message} \n#{source}" end
def add_npm_dependencies(packages, dev: false)
def add_npm_dependencies(packages, dev: false) pj = package_json return false unless pj begin if dev pj.manager.add(packages, type: :dev, exact: true) else pj.manager.add(packages, exact: true) end true rescue StandardError => e puts "Warning: Could not add packages via package_json gem: #{e.message}" puts "Will fall back to direct npm commands." false end end
def component_extension(options)
def component_extension(options) options.typescript? ? "tsx" : "jsx" end
def copy_file_and_missing_parent_directories(source_file, destination_file = nil)
def copy_file_and_missing_parent_directories(source_file, destination_file = nil) destination_file ||= source_file destination_path = Pathname.new(destination_file) parent_directories = destination_path.dirname empty_directory(parent_directories) unless dest_dir_exists?(parent_directories) copy_file source_file, destination_file end
def dest_dir_exists?(dir)
def dest_dir_exists?(dir) dest_dir = File.join(destination_root, dir) Dir.exist?(dest_dir) ? dest_dir : nil end
def dest_file_exists?(file)
def dest_file_exists?(file) dest_file = File.join(destination_root, file) File.exist?(dest_file) ? dest_file : nil end
def empty_directory_with_keep_file(destination, config = {})
def empty_directory_with_keep_file(destination, config = {}) empty_directory(destination, config) keep_file(destination) end
def keep_file(destination)
def keep_file(destination) create_file("#{destination}/.keep") unless options[:skip_keeps] end
def package_json
def package_json # Lazy load package_json gem only when actually needed for dependency management require "package_json" unless defined?(PackageJson) @package_json ||= PackageJson.read rescue LoadError puts "Warning: package_json gem not available. This is expected before Shakapacker installation." puts "Dependencies will be installed using the default package manager after Shakapacker setup." nil rescue StandardError => e puts "Warning: Could not read package.json: #{e.message}" puts "This is normal before Shakapacker creates the package.json file." nil end
def setup_file_error(file, data)
def setup_file_error(file, data) <<~MSG #{file} was not found. Please add the following content to your #{file} file: #{data} MSG end
def symlink_dest_file_to_dest_file(target, link)
source_root, this symlinks a file in destination_root to a file also in
As opposed to Rails::Generators::Testing.create_link, which creates a link pointing to
def symlink_dest_file_to_dest_file(target, link) target_pathname = Pathname.new(File.join(destination_root, target)) link_pathname = Pathname.new(File.join(destination_root, link)) link_directory = link_pathname.dirname link_basename = link_pathname.basename target_relative_path = target_pathname.relative_path_from(link_directory) `cd #{link_directory} && ln -s #{target_relative_path} #{link_basename}` end