Skip to content

Commit ff7f4a7

Browse files
committed
feat(api): support package manager
1 parent f274b92 commit ff7f4a7

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/leetcode_api_runner.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ impl LeetcodeApiRunner {
5555
let pb_name = pb_desc.name.replace(" ", "_");
5656
let md_desc = html2md::parse_html(&pb_desc.content);
5757

58-
let problem_dir = self.prepare_problem_directory(id, &pb_name)?;
58+
let problem_dir =
59+
self.prepare_problem_directory(id, &pb_name, &language)?;
5960

6061
self.write_readme(&problem_dir, id, &pb_name, &md_desc)?;
6162
self.generate_starter_code(&problem_dir, language, &pb)?;
@@ -69,11 +70,15 @@ impl LeetcodeApiRunner {
6970

7071
/// Prepares the problem directory.
7172
fn prepare_problem_directory(
72-
&self, id: u32, pb_name: &str,
73+
&self, id: u32, pb_name: &str, language: &ProgrammingLanguage,
7374
) -> io::Result<std::path::PathBuf> {
7475
let leetcode_dir = self.config.resolve_leetcode_dir()?;
7576
let problem_dir = leetcode_dir.join(format!("{}_{}", id, pb_name));
7677
ensure_directory_exists(&problem_dir)?;
78+
79+
// Initialize language-specific project structure
80+
self.initialize_language_project(&problem_dir, pb_name, language)?;
81+
7782
Ok(problem_dir)
7883
}
7984

@@ -141,4 +146,49 @@ impl LeetcodeApiRunner {
141146
id, test_response
142147
))
143148
}
149+
150+
/// Initializes language-specific project structure.
151+
fn initialize_language_project(
152+
&self, problem_dir: &std::path::Path, pb_name: &str,
153+
language: &ProgrammingLanguage,
154+
) -> io::Result<()> {
155+
use std::process::Command;
156+
157+
let result = match language {
158+
ProgrammingLanguage::Rust => Command::new("cargo")
159+
.args(&["init", "--name", pb_name, "--vcs", "none"])
160+
.current_dir(problem_dir)
161+
.output(),
162+
ProgrammingLanguage::JavaScript
163+
| ProgrammingLanguage::TypeScript => Command::new("npm")
164+
.args(&["init", "-y"])
165+
.current_dir(problem_dir)
166+
.output(),
167+
ProgrammingLanguage::Go => {
168+
let module_name =
169+
format!("leetcode-{}", pb_name.replace("_", "-"));
170+
Command::new("go")
171+
.args(&["mod", "init", &module_name])
172+
.current_dir(problem_dir)
173+
.output()
174+
},
175+
_ => return Ok(()),
176+
};
177+
178+
match result {
179+
Ok(output) if !output.status.success() => {
180+
eprintln!(
181+
"Warning: Failed to initialize project: {}",
182+
String::from_utf8_lossy(&output.stderr)
183+
);
184+
},
185+
Err(e) => eprintln!(
186+
"Warning: Failed to run initialization command: {}",
187+
e
188+
),
189+
_ => {},
190+
}
191+
192+
Ok(())
193+
}
144194
}

0 commit comments

Comments
 (0)