-
-
Notifications
You must be signed in to change notification settings - Fork 638
Fix duplicate messages in install generator #1788
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,9 @@ def run_generators | |
if installation_prerequisites_met? || options.ignore_warnings? | ||
invoke_generators | ||
add_bin_scripts | ||
add_post_install_message | ||
# Only add the post install message if not using Redux | ||
# Redux generator handles its own messages | ||
add_post_install_message unless options.redux? | ||
else | ||
error = <<~MSG.strip | ||
🚫 React on Rails generator prerequisites not met! | ||
|
@@ -77,6 +79,14 @@ def invoke_generators | |
else | ||
invoke "react_on_rails:react_no_redux", [], { typescript: options.typescript? } | ||
end | ||
setup_react_dependencies | ||
end | ||
|
||
def setup_react_dependencies | ||
@added_dependencies_to_package_json ||= false | ||
@ran_direct_installs ||= false | ||
add_js_dependencies | ||
install_js_dependencies if @added_dependencies_to_package_json && !@ran_direct_installs | ||
end | ||
|
||
# NOTE: other requirements for existing files such as .gitignore or application. | ||
|
@@ -410,6 +420,134 @@ def create_typescript_config | |
puts Rainbow("✅ Created tsconfig.json").green | ||
end | ||
|
||
def add_js_dependencies | ||
add_react_on_rails_package | ||
add_react_dependencies | ||
add_css_dependencies | ||
add_dev_dependencies | ||
end | ||
|
||
def add_react_on_rails_package | ||
major_minor_patch_only = /\A\d+\.\d+\.\d+\z/ | ||
|
||
# Try to use package_json gem first, fall back to direct npm commands | ||
react_on_rails_pkg = if ReactOnRails::VERSION.match?(major_minor_patch_only) | ||
["react-on-rails@#{ReactOnRails::VERSION}"] | ||
else | ||
puts "Adding the latest react-on-rails NPM module. " \ | ||
"Double check this is correct in package.json" | ||
["react-on-rails"] | ||
end | ||
|
||
puts "Installing React on Rails package..." | ||
if add_npm_dependencies(react_on_rails_pkg) | ||
@added_dependencies_to_package_json = true | ||
return | ||
end | ||
|
||
puts "Using direct npm commands as fallback" | ||
success = system("npm", "install", *react_on_rails_pkg) | ||
@ran_direct_installs = true if success | ||
handle_npm_failure("react-on-rails package", react_on_rails_pkg) unless success | ||
end | ||
|
||
def add_react_dependencies | ||
puts "Installing React dependencies..." | ||
react_deps = %w[ | ||
react | ||
react-dom | ||
@babel/preset-react | ||
prop-types | ||
babel-plugin-transform-react-remove-prop-types | ||
babel-plugin-macros | ||
] | ||
if add_npm_dependencies(react_deps) | ||
@added_dependencies_to_package_json = true | ||
return | ||
end | ||
|
||
success = system("npm", "install", *react_deps) | ||
@ran_direct_installs = true if success | ||
handle_npm_failure("React dependencies", react_deps) unless success | ||
end | ||
|
||
def add_css_dependencies | ||
puts "Installing CSS handling dependencies..." | ||
css_deps = %w[ | ||
css-loader | ||
css-minimizer-webpack-plugin | ||
mini-css-extract-plugin | ||
style-loader | ||
] | ||
if add_npm_dependencies(css_deps) | ||
@added_dependencies_to_package_json = true | ||
return | ||
end | ||
|
||
success = system("npm", "install", *css_deps) | ||
@ran_direct_installs = true if success | ||
handle_npm_failure("CSS dependencies", css_deps) unless success | ||
end | ||
|
||
def add_dev_dependencies | ||
puts "Installing development dependencies..." | ||
dev_deps = %w[ | ||
@pmmmwh/react-refresh-webpack-plugin | ||
react-refresh | ||
] | ||
if add_npm_dependencies(dev_deps, dev: true) | ||
@added_dependencies_to_package_json = true | ||
return | ||
end | ||
|
||
success = system("npm", "install", "--save-dev", *dev_deps) | ||
@ran_direct_installs = true if success | ||
handle_npm_failure("development dependencies", dev_deps, dev: true) unless success | ||
end | ||
|
||
def install_js_dependencies | ||
# Detect which package manager to use | ||
success = if File.exist?(File.join(destination_root, "yarn.lock")) | ||
system("yarn", "install") | ||
elsif File.exist?(File.join(destination_root, "pnpm-lock.yaml")) | ||
system("pnpm", "install") | ||
elsif File.exist?(File.join(destination_root, "package-lock.json")) || | ||
File.exist?(File.join(destination_root, "package.json")) | ||
# Use npm for package-lock.json or as default fallback | ||
system("npm", "install") | ||
else | ||
true # No package manager detected, skip | ||
end | ||
|
||
unless success | ||
GeneratorMessages.add_warning(<<~MSG.strip) | ||
⚠️ JavaScript dependencies installation failed. | ||
|
||
This could be due to network issues or missing package manager. | ||
You can install dependencies manually later by running: | ||
• npm install (if using npm) | ||
• yarn install (if using yarn) | ||
• pnpm install (if using pnpm) | ||
MSG | ||
end | ||
|
||
success | ||
end | ||
Comment on lines
+508
to
+535
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support bun and avoid invoking missing CLIs; otherwise installs may fail silently. You accept bun as a valid package manager earlier but don’t handle it here, and you might call yarn/pnpm when those CLIs aren’t present. Add bun detection and guard each branch by CLI availability. Apply: @@
- success = if File.exist?(File.join(destination_root, "yarn.lock"))
- system("yarn", "install")
- elsif File.exist?(File.join(destination_root, "pnpm-lock.yaml"))
- system("pnpm", "install")
- elsif File.exist?(File.join(destination_root, "package-lock.json")) ||
- File.exist?(File.join(destination_root, "package.json"))
- # Use npm for package-lock.json or as default fallback
- system("npm", "install")
- else
- true # No package manager detected, skip
- end
+ success = if File.exist?(File.join(destination_root, "yarn.lock")) && cli_exists?("yarn")
+ system("yarn", "install")
+ elsif File.exist?(File.join(destination_root, "pnpm-lock.yaml")) && cli_exists?("pnpm")
+ system("pnpm", "install")
+ elsif File.exist?(File.join(destination_root, "bun.lockb")) && cli_exists?("bun")
+ system("bun", "install")
+ elsif (File.exist?(File.join(destination_root, "package-lock.json")) ||
+ File.exist?(File.join(destination_root, "package.json")))
+ # Use npm for package-lock.json or as default fallback
+ system("npm", "install")
+ else
+ true # No supported package manager detected, skip
+ end
@@
- • pnpm install (if using pnpm)
+ • pnpm install (if using pnpm)
+ • bun install (if using bun)
🤖 Prompt for AI Agents
|
||
|
||
def handle_npm_failure(dependency_type, packages, dev: false) | ||
install_command = dev ? "npm install --save-dev" : "npm install" | ||
GeneratorMessages.add_warning(<<~MSG.strip) | ||
⚠️ Failed to install #{dependency_type}. | ||
|
||
The following packages could not be installed automatically: | ||
#{packages.map { |pkg| " • #{pkg}" }.join("\n")} | ||
|
||
This could be due to network issues or missing package manager. | ||
You can install them manually later by running: | ||
#{install_command} #{packages.join(' ')} | ||
MSG | ||
end | ||
|
||
# Removed: Shakapacker auto-installation logic (now explicit dependency) | ||
|
||
# Removed: Shakapacker 8+ is now required as explicit dependency | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix RSpec injection: current gsub replaces only the opener with a full block (breaks rails_helper).
Replacing just "RSpec.configure do |config|" with an entire block will leave the original block body and closing
end
in place, yielding invalid Ruby and/or duplicated config. Inject inside the existing block instead.Apply this diff to change the constant to a snippet (no open/close), then insert it inside the existing block (method change shown below):
Update the injector method (outside this hunk) to insert rather than replace:
🤖 Prompt for AI Agents