Skip to content

Commit 4add50a

Browse files
justin808claude
andcommitted
Add comprehensive error handling for JavaScript dependency installation
- Add error handling to install_js_dependencies method with helpful warning messages - Add error handling to all npm install commands in dependency methods - Create handle_npm_failure helper method for consistent error reporting - Provide clear instructions for manual installation when automatic installation fails - Gracefully handle network failures without leaving installation in inconsistent state This addresses the code quality concern about missing error handling in BaseGenerator#add_js_dependencies and ensures users get helpful feedback when dependency installation fails. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c9fb999 commit 4add50a

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,31 @@ def add_js_dependencies
104104

105105
def install_js_dependencies
106106
# Detect which package manager to use
107-
if File.exist?(File.join(destination_root, "yarn.lock"))
108-
run "yarn install"
109-
elsif File.exist?(File.join(destination_root, "pnpm-lock.yaml"))
110-
run "pnpm install"
111-
elsif File.exist?(File.join(destination_root, "package-lock.json")) ||
112-
File.exist?(File.join(destination_root, "package.json"))
113-
# Use npm for package-lock.json or as default fallback
114-
run "npm install"
107+
success = if File.exist?(File.join(destination_root, "yarn.lock"))
108+
run "yarn install"
109+
elsif File.exist?(File.join(destination_root, "pnpm-lock.yaml"))
110+
run "pnpm install"
111+
elsif File.exist?(File.join(destination_root, "package-lock.json")) ||
112+
File.exist?(File.join(destination_root, "package.json"))
113+
# Use npm for package-lock.json or as default fallback
114+
run "npm install"
115+
else
116+
true # No package manager detected, skip
117+
end
118+
119+
unless success
120+
GeneratorMessages.add_warning(<<~MSG.strip)
121+
⚠️ JavaScript dependencies installation failed.
122+
123+
This could be due to network issues or missing package manager.
124+
You can install dependencies manually later by running:
125+
• npm install (if using npm)
126+
• yarn install (if using yarn)
127+
• pnpm install (if using pnpm)
128+
MSG
115129
end
130+
131+
success
116132
end
117133

118134
def update_gitignore_for_auto_registration
@@ -157,7 +173,8 @@ def add_react_on_rails_package
157173
return if add_npm_dependencies(react_on_rails_pkg)
158174

159175
puts "Using direct npm commands as fallback"
160-
run "npm install #{react_on_rails_pkg.join(' ')}"
176+
success = run "npm install #{react_on_rails_pkg.join(' ')}"
177+
handle_npm_failure("react-on-rails package", react_on_rails_pkg) unless success
161178
end
162179

163180
def add_react_dependencies
@@ -170,7 +187,10 @@ def add_react_dependencies
170187
babel-plugin-transform-react-remove-prop-types
171188
babel-plugin-macros
172189
]
173-
run "npm install #{react_deps.join(' ')}" unless add_npm_dependencies(react_deps)
190+
unless add_npm_dependencies(react_deps)
191+
success = run "npm install #{react_deps.join(' ')}"
192+
handle_npm_failure("React dependencies", react_deps) unless success
193+
end
174194
end
175195

176196
def add_css_dependencies
@@ -181,7 +201,10 @@ def add_css_dependencies
181201
mini-css-extract-plugin
182202
style-loader
183203
]
184-
run "npm install #{css_deps.join(' ')}" unless add_npm_dependencies(css_deps)
204+
unless add_npm_dependencies(css_deps)
205+
success = run "npm install #{css_deps.join(' ')}"
206+
handle_npm_failure("CSS dependencies", css_deps) unless success
207+
end
185208
end
186209

187210
def add_dev_dependencies
@@ -192,7 +215,8 @@ def add_dev_dependencies
192215
]
193216
return if add_npm_dependencies(dev_deps, dev: true)
194217

195-
run "npm install --save-dev #{dev_deps.join(' ')}"
218+
success = run "npm install --save-dev #{dev_deps.join(' ')}"
219+
handle_npm_failure("development dependencies", dev_deps, dev: true) unless success
196220
end
197221

198222
CONFIGURE_RSPEC_TO_COMPILE_ASSETS = <<-STR.strip_heredoc
@@ -205,6 +229,20 @@ def add_dev_dependencies
205229

206230
private
207231

232+
def handle_npm_failure(dependency_type, packages, dev: false)
233+
install_command = dev ? "npm install --save-dev" : "npm install"
234+
GeneratorMessages.add_warning(<<~MSG.strip)
235+
⚠️ Failed to install #{dependency_type}.
236+
237+
The following packages could not be installed automatically:
238+
#{packages.map { |pkg| " • #{pkg}" }.join("\n")}
239+
240+
This could be due to network issues or missing package manager.
241+
You can install them manually later by running:
242+
#{install_command} #{packages.join(' ')}
243+
MSG
244+
end
245+
208246
def copy_webpack_main_config(base_path, config)
209247
webpack_config_path = "config/webpack/webpack.config.js"
210248

0 commit comments

Comments
 (0)