1use anyhow::Result;
2use serde_json;
3use std::fs;
4use std::path::Path;
5use std::process::Command;
6
7use crate::config::ProjectConfig;
8
9pub fn copy_directory(src: &Path, dst: &Path) -> Result<()> {
10 if src.is_file() {
11 if let Some(parent) = dst.parent() {
12 fs::create_dir_all(parent)?;
13 }
14 fs::copy(src, dst)?;
15 } else if src.is_dir() {
16 fs::create_dir_all(dst)?;
17 for entry in fs::read_dir(src)? {
18 let entry = entry?;
19 let src_path = entry.path();
20 let dst_path = dst.join(entry.file_name());
21
22 if src_path.is_dir() {
23 copy_directory(&src_path, &dst_path)?;
24 } else {
25 fs::copy(&src_path, &dst_path)?;
26 }
27 }
28 }
29
30 Ok(())
31}
32
33pub fn update_package_json(project_path: &Path, config: &ProjectConfig) -> Result<()> {
34 let package_json_path = project_path.join("package.json");
35
36 if !package_json_path.exists() {
37 return Ok(()); }
39
40 let package_json_content = fs::read_to_string(&package_json_path)?;
41 let mut package_json: serde_json::Value = serde_json::from_str(&package_json_content)?;
42
43 if let Some(obj) = package_json.as_object_mut() {
45 obj.insert(
46 "name".to_string(),
47 serde_json::Value::String(config.name.clone()),
48 );
49 obj.insert(
50 "author".to_string(),
51 serde_json::Value::String(config.author.clone()),
52 );
53 obj.insert(
54 "description".to_string(),
55 serde_json::Value::String(config.description.clone()),
56 );
57 }
58
59 let updated_content = serde_json::to_string_pretty(&package_json)?;
60 fs::write(&package_json_path, updated_content)?;
61
62 Ok(())
63}
64
65pub fn init_git_repository(project_path: &Path) -> Result<()> {
66 let current_dir = std::env::current_dir()?;
68 std::env::set_current_dir(project_path)?;
69
70 let status = Command::new("git").args(["init"]).status();
72
73 std::env::set_current_dir(current_dir)?;
75
76 match status {
77 Ok(_) => {
78 println!("🔧 Git repository initialized");
79 }
80 Err(_) => {
81 println!("⚠️ Git not available, skipping repository initialization");
82 }
83 }
84
85 Ok(())
86}