Skip to content

Commit

Permalink
Added JavaScript solution
Browse files Browse the repository at this point in the history
  • Loading branch information
uptudev committed Feb 20, 2024
1 parent cd20cce commit ad31b5f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 28 deletions.
23 changes: 20 additions & 3 deletions src/libs/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ 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!(
"{}; please enter a valid language.\n{} {} {}",
"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);
},
Expand Down Expand Up @@ -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.");
}
129 changes: 127 additions & 2 deletions src/libs/js.rs
Original file line number Diff line number Diff line change
@@ -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,
}

35 changes: 12 additions & 23 deletions src/libs/zig.rs
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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());
}


0 comments on commit ad31b5f

Please sign in to comment.