Skip to content

Commit c8d5fdc

Browse files
authored
fix: handle npm install errors (#312)
* fix: handle npm install errors * chore: fmt
1 parent 5913600 commit c8d5fdc

File tree

1 file changed

+46
-18
lines changed
  • crates/stellar-scaffold-cli/src/commands

1 file changed

+46
-18
lines changed

crates/stellar-scaffold-cli/src/commands/init.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,6 @@ impl Cmd {
106106
let env_path = absolute_project_path.join(".env");
107107
copy(example_path, env_path)?;
108108

109-
// If git is installed, run init and make initial commit
110-
if git_exists() {
111-
git_init(&absolute_project_path);
112-
git_add(&absolute_project_path, &["-A"]);
113-
git_commit(&absolute_project_path, "initial commit");
114-
}
115-
116109
// Update the project's OpenZeppelin examples with the latest editions
117110
if !self.vers.tutorial {
118111
let example_contracts = ["nft-enumerable", "fungible-allowlist"];
@@ -124,16 +117,7 @@ impl Cmd {
124117
}
125118

126119
// Install npm dependencies
127-
printer.infoln("Installing npm dependencies...");
128-
let npm_install_command = Command::new("npm")
129-
.arg("install")
130-
.current_dir(&absolute_project_path)
131-
.output()?;
132-
if !npm_install_command.status.success() {
133-
printer.warnln(
134-
"Failed to install dependencies, run 'npm install' in the project directory",
135-
);
136-
}
120+
let npm_status = npm_install(&absolute_project_path, &printer);
137121

138122
// Build contracts and create contract clients
139123
printer.infoln("Building contracts and generating client code...");
@@ -151,14 +135,21 @@ impl Cmd {
151135
printer.warnln(format!("Failed to build contract clients: {e}"));
152136
}
153137

138+
// If git is installed, run init and make initial commit
139+
if git_exists() {
140+
git_init(&absolute_project_path);
141+
git_add(&absolute_project_path, &["-A"]);
142+
git_commit(&absolute_project_path, "initial commit");
143+
}
144+
154145
printer.blankln("\n\n");
155146
printer.checkln(format!(
156147
"Project successfully created at {}!",
157148
absolute_project_path.display()
158149
));
159150
printer.blankln(" You can now run the application with:\n");
160151
printer.blankln(format!("\tcd {}", self.project_path.display()));
161-
if !npm_install_command.status.success() {
152+
if !npm_status {
162153
printer.blankln("\tnpm install");
163154
}
164155
printer.blankln("\tnpm start\n");
@@ -229,6 +220,43 @@ impl Cmd {
229220
}
230221
}
231222

223+
// Check if npm is installed and exists in PATH
224+
fn npm_exists() -> bool {
225+
Command::new("npm").arg("--version").output().is_ok()
226+
}
227+
228+
// Install npm dependencies
229+
fn npm_install(path: &PathBuf, printer: &Print) -> bool {
230+
if !npm_exists() {
231+
printer.warnln("Failed to install dependencies, npm is not installed");
232+
return false;
233+
}
234+
235+
printer.infoln("Installing npm dependencies...");
236+
match Command::new("npm")
237+
.arg("install")
238+
.current_dir(path)
239+
.output()
240+
{
241+
Ok(output) if output.status.success() => true,
242+
Ok(output) => {
243+
// Command ran without panic, but failed for some other reason
244+
// like network issue or missing dependency, etc.
245+
printer.warnln("Failed to install dependencies: Please run 'npm install' manually");
246+
if !output.stderr.is_empty()
247+
&& let Ok(stderr) = String::from_utf8(output.stderr)
248+
{
249+
printer.warnln(format!("Error: {}", stderr.trim()));
250+
}
251+
false
252+
}
253+
Err(e) => {
254+
printer.warnln(format!("Failed to run npm install: {e}"));
255+
false
256+
}
257+
}
258+
}
259+
232260
// Check if git is installed and exists in PATH
233261
fn git_exists() -> bool {
234262
Command::new("git").arg("--version").output().is_ok()

0 commit comments

Comments
 (0)