diff --git a/src/libs/base.rs b/src/libs/base.rs index a810317..560b32e 100644 --- a/src/libs/base.rs +++ b/src/libs/base.rs @@ -101,12 +101,12 @@ fn route_response(buffer: &mut String, response: String, mag: &str, proj_name: & route_response(buffer, response, mag, proj_name); }, "rust" => rust::init(buffer, mag, proj_name), - "js" => js::init(buffer), + "js" => js::init(proj_name), "ts" => ts::init(buffer), "react" => react::init(buffer), "vue" => vue::init(buffer), "svelte" => svelte::init(buffer), - "zig" => zig::init(buffer, mag, proj_name), + "zig" => zig::init(proj_name), _ => { buffer.clear(); println!( @@ -114,7 +114,7 @@ fn route_response(buffer: &mut String, response: String, mag: &str, proj_name: & "Invalid response".bold().red(), "(or type".bright_black(), "ls".purple(), - "to list all valid languages)".bright_black()); + "to list all valid languages)\n".bright_black()); let response = get_language(buffer, mag); route_response(buffer, response, mag, proj_name); }, @@ -242,3 +242,20 @@ pub fn gen_help_template() -> String { out } + +pub fn git_init() { + std::process::Command::new("git") + .arg("init") + .output() + .expect("Error spawning child process."); + + std::process::Command::new("git") + .args(["add", "-A"]) + .output() + .expect("Error spawning child process."); + + std::process::Command::new("git") + .args(["commit", "-S", "-m", "\"Initial commit\""]) + .output() + .expect("Error spawning child process."); +} diff --git a/src/libs/js.rs b/src/libs/js.rs index 780b959..b83678e 100644 --- a/src/libs/js.rs +++ b/src/libs/js.rs @@ -1,4 +1,129 @@ -pub fn init(buffer: &mut String) { - todo!("JavaScript init script needed"); +use colored::Colorize; +use crate::libs::base::{gen_readme, git_init}; + +pub fn init(proj_name: &String) { + let runtime = get_runtime(); + match runtime { + Runtime::BUN => { + init_bun(proj_name); + let readme_path = std::path::Path::new("./README.md"); + gen_readme(&proj_name, &readme_path).unwrap(); + git_init(); + println!("{}", "Done!".green().bold()); + }, + Runtime::NODE => { + init_node(proj_name); + let readme_path = std::path::Path::new("./README.md"); + gen_readme(&proj_name, &readme_path).unwrap(); + git_init(); + println!("{}", "Done!".green().bold()); + }, + Runtime::NONE => { + eprintln!("{}:\n{}", + "Error".bright_red().bold(), + "No valid JavaScript runtime found."); + }, + } +} + +fn init_bun(proj_name: &String) { + let cwd = std::env::current_dir() + .expect("Error getting CWD") + .as_path() + .as_os_str() + .to_str() + .unwrap() + .to_owned() + "/" + proj_name; + println!( + "{}{}{}{}{}\x1b[90m", + "Creating Bun project ".purple().dimmed(), + proj_name.purple().bold(), + " in ".purple().dimmed(), + cwd.purple(), + "...".purple().dimmed()); + + let mut handle = std::process::Command::new("mkdir") + .arg(proj_name) + .spawn() + .expect("Error spawning child process."); + let exit_status = handle.wait().unwrap(); + if !exit_status.success() { + panic!("Couldn't make file."); + } + + let target_dir = std::path::Path::new(&cwd); + std::env::set_current_dir(target_dir) + .expect("Couldnt change directories."); + + + std::process::Command::new("bun") + .args(["init", "-y"]) + .output() + .expect("Error initializing bun project"); +} + +fn init_node(proj_name: &String) { + let cwd = std::env::current_dir() + .expect("Error getting CWD") + .as_path() + .as_os_str() + .to_str() + .unwrap() + .to_owned() + "/" + proj_name; + println!( + "{}{}{}{}{}\x1b[90m", + "Creating Node project ".purple().dimmed(), + proj_name.purple().bold(), + " in ".purple().dimmed(), + cwd.purple(), + "...".purple().dimmed()); + + let mut handle = std::process::Command::new("mkdir") + .arg(proj_name) + .spawn() + .expect("Error spawning child process."); + let exit_status = handle.wait().unwrap(); + if !exit_status.success() { + panic!("Couldn't make file."); + } + + let target_dir = std::path::Path::new(&cwd); + std::env::set_current_dir(target_dir) + .expect("Couldnt change directories."); + + + std::process::Command::new("npm") + .args(["init", "-y"]) + .output() + .expect("Error initializing bun project"); + +} + +fn get_runtime() -> Runtime { + let handle = std::process::Command::new("bun") + .arg("--version") + .stdout(std::process::Stdio::null()) + .spawn(); + + match handle { + Ok(_) => Runtime::BUN, + Err(_) => { + let handle = std::process::Command::new("npm") + .arg("--version") + .stdout(std::process::Stdio::null()) + .spawn(); + match handle { + Ok(_) => Runtime::NODE, + Err(_) => Runtime::NONE, + } + } + } +} + +#[derive(Debug)] +enum Runtime { + BUN, + NODE, + NONE, } diff --git a/src/libs/zig.rs b/src/libs/zig.rs index 9cf6659..52566dd 100644 --- a/src/libs/zig.rs +++ b/src/libs/zig.rs @@ -1,14 +1,14 @@ use colored::*; -use crate::libs::base::{query, gen_readme}; +use crate::libs::base::{gen_readme, git_init}; -pub fn init(buffer: &mut String, mag: &str, proj_name: &String) { +pub fn init(proj_name: &String) { let cwd = std::env::current_dir() .expect("Error getting CWD") .as_path() .as_os_str() .to_str() .unwrap() - .to_owned(); + .to_owned() + "/" + proj_name; println!( "{}{}{}{}{}\x1b[90m", "Creating Zig project ".purple().dimmed(), @@ -26,27 +26,16 @@ pub fn init(buffer: &mut String, mag: &str, proj_name: &String) { panic!("Couldn't make file."); } - let mut handle = std::process::Command::new("cd") - .arg("./".to_string() + proj_name) - .spawn() - .expect("Error spawning child process."); - let exit_status = handle.wait().unwrap(); - if !exit_status.success() { - panic!("Couldn't change directories."); - } - let mut handle = std::process::Command::new("zig") + let target_dir = std::path::Path::new(&cwd); + std::env::set_current_dir(target_dir) + .expect("Couldnt change directories."); + + std::process::Command::new("zig") .arg("init") - .spawn() + .output() .expect("Error spawning child process."); - let exit_status = handle.wait().unwrap(); - let proj_dir = format!("./{}", proj_name); - let readme_dir = proj_dir + "/README.md"; - let readme_path = std::path::Path::new(&readme_dir); - println!("\x1b[0m"); + let readme_path = std::path::Path::new("./README.md"); gen_readme(&proj_name, &readme_path).unwrap(); - if exit_status.success() { - println!("{}", "Done!".green().bold()); - } + git_init(); + println!("{}", "Done!".green().bold()); } - -