Skip to content
Open
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
14 changes: 13 additions & 1 deletion vite_rails/lib/vite_rails/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setup_app_files
replace_first_line config.config_path, "app/frontend", %( "sourceCodeDir": "#{dir}",)
end
setup_content_security_policy root.join("config/initializers/content_security_policy.rb")
append root.join("Procfile.dev"), "web: bin/rails s"
append_if_no_web_entry root.join("Procfile.dev"), "web: bin/rails s"
end

# Internal: Configure CSP rules that allow to load @vite/client correctly.
Expand All @@ -56,6 +56,18 @@ def setup_content_security_policy(csp_file)
CSP
end

# Internal: Append a line to a file only if no line starts with 'web:'
# @param path [Pathname] The path to the file
# @param contents [String] The line to append
def append_if_no_web_entry(path, contents)
content = read_lines(path)

# Check if any line starts with 'web:'
return if content.any? { |line| line.strip.start_with?("web:") }

append path, contents
end

# Override: Create a sample JS file and attempt to inject it in an HTML template.
def install_sample_files
unless config.resolved_entrypoints_dir.join("application.js").exist?
Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def install_sample_files

def_delegators "ViteRuby", :config

%i[append cp inject_line_after inject_line_after_last inject_line_before replace_first_line write].each do |util|
%i[append read_lines cp inject_line_after inject_line_after_last inject_line_before replace_first_line write].each do |util|
define_method(util) { |*args|
ViteRuby::CLI::FileUtils.send(util, *args) rescue nil
}
Expand Down