diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b155767 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/resolver.iml b/.idea/resolver.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/resolver.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 8d2301c..6e161c2 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,7 @@ resolver-cli get dfd project_name The `scaffold` action is used to scaffold projects for different development tools and languages which includes: -- ReactJS -- ReactTs +- React - Hardhat - NestJs - Laravel @@ -65,13 +64,13 @@ The `scaffold` action is used to scaffold projects for different development too #### ReactJS Creates a React project with JavaScript ```sh -resolver-cli scaffold reactjs project_name +resolver-cli scaffold react --lan=j ``` #### ReactTS Creates a React project with TypeScript ```sh -resolver-cli scaffold reactts project_name +resolver-cli scaffold react --lan=t ``` #### Hardhat diff --git a/install_resolver.sh b/install_resolver.sh new file mode 100644 index 0000000..d3d89ec --- /dev/null +++ b/install_resolver.sh @@ -0,0 +1,68 @@ +#!/bin/bash +set -e +echo -e "installing the resolver cli via curl" + +RED="\033[0;31m" +GREEN="\033[0;32m" +YELLOW="\033[0;33m" +WHITE="\033[0m" + +# getting the operating system of the machine +OS=$(uname -s) +echo -e "${RED} RED first ,${GREEN} Green next, ${YELLOW} Yellow after" + +check_for_pkg_config_openssl() { + echo -e "checking if openssl and pkg_config are available" + if ! pkg-config --exists openssl; then + echo -e "could not find openssl and pkg_config" + #check for the os + echo -e "checking the machine OS and the installing the packages" + if [ "$OS" = "Darwin" ]; then + if ! command -v brew >/dev/null ; then + echo -e "homebrew is not installed, installing homebrew" + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + fi + echo -e "installing openssl and pkg_config" + brew install openssl + brew install pkg-config + + elif [ "$OS" = "Linux" ]; then + echo -e "updating linux packages" + sudo apt-get update -y || true + echo -e "installing openssl and pkg-config" + sudo apt-get install -y libssl-dev pkg-config + + else + echo -e "Unsupported OS: ${OS} detected" + exit 1 + fi + else + echo -e "${GREEN} openssl and pkg-config are already installed" + fi +} + +echo -e "checking if curl already exists" +if ! command -v curl >/dev/null; then + echo -e "${RED} curl is missing" + exit 1 +fi + +check_for_pkg_config_openssl + +echo -e "checking if rust is installed " +if ! (command -v rustc >/dev/null && cargo >/dev/null); then + #install rust here if not installed + curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh -s -- -y + source "$HOME/.cargo/env" +else + echo -e "${GREEN} rust is already installed with ${YELLOW}$(rustc --version), ${YELLOW}$(cargo --version)" +fi + + +if ! command -v resolver-cli >/dev/null; then + # install resolver here + cargo install resolver-cli + echo -e "${GREEN}resolver-cli has been installed successfully" +else + echo -e "${WHITE} resolver version ${YELLOW}$(resolver-cli --version) is already installed" +fi \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f4e6c91..c81f546 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,10 @@ use std::error::Error; use colored::*; pub mod utils; -use utils::helpers::*; -use utils::constants::*; -pub use utils::command_arguments::*; +pub use utils::{command_arguments::*, + constants::*, + helpers::{scaffold::*, installer::* + }}; pub fn resolve(args: ClapperArgs) -> Result<(), Box> { @@ -61,21 +62,27 @@ pub fn resolve(args: ClapperArgs) -> Result<(), Box> { }, EntityType::Scaffold(scaffold_command) => { match scaffold_command.command { - ScaffoldSubCommand::Reactjs(dir) => { - match create_react_app(dir.dir_name.clone()) { - Ok(_) => println!("{}", "Successfully created the React project!".bright_blue()), - Err(e) => { - return Err(e); - } - } - }, - ScaffoldSubCommand::Reactts(dir) => { - match create_react_app_with_typescript(dir.dir_name.clone()) { - Ok(_) => println!("{}", "Successfully created the TypeScript React project!".bright_blue()), - Err(e) => { - return Err(e); - } - } + ScaffoldSubCommand::React(user_choice) => { + match user_choice.lan { + ReactVariants::J => { + println!("javascript was chosen as the project language"); + match create_react_app(user_choice.dir_name.clone()) { + Ok(_) => println!("{}", "Successfully created the React project!".bright_blue()), + Err(e) => { + return Err(e); + } + } + }, + ReactVariants::T => { + println!("typescript was chosen as the project language"); + match create_react_app_with_typescript(user_choice.dir_name.clone()) { + Ok(_) => println!("{}", "Successfully created the React project!".bright_blue()), + Err(e) => { + return Err(e); + } + } + }, + } }, ScaffoldSubCommand::Hardhat(dir) => { match create_hardhat_project(dir.dir_name.clone()) { @@ -188,7 +195,7 @@ pub fn resolve(args: ClapperArgs) -> Result<(), Box> { return Err(e); } } - } + }, } }, EntityType::Install(install_command) => { diff --git a/src/utils/command_arguments.rs b/src/utils/command_arguments.rs index 4343245..ad20334 100644 --- a/src/utils/command_arguments.rs +++ b/src/utils/command_arguments.rs @@ -1,4 +1,4 @@ -use clap::{Args, Parser, Subcommand}; +use clap::{Args, Parser, Subcommand, ValueEnum}; #[derive(Debug, Parser)] #[clap(author, version, about)] @@ -53,9 +53,9 @@ pub struct ScaffoldCommand { #[derive(Debug, Subcommand)] pub enum ScaffoldSubCommand { /// Scaffolds a create-react-app JavaScript project - Reactjs(GetDir), - /// Scaffolds a create-react-app TypeScript project - Reactts(GetDir), + // Reactjs(GetDir), + // /// Scaffolds a create-react-app TypeScript project + // Reactts(GetDir), /// Scaffolds a Hardhat project Hardhat(GetDir), /// Scaffolds a NestJS project @@ -84,6 +84,8 @@ pub enum ScaffoldSubCommand { AnchorTS(GetDir), /// Scaffold an Anchor project with Rust Tests AnchorRust(GetDir), + // scaffold a react project + React(CreateReactSubCommand), } // ---------------- @@ -122,12 +124,32 @@ pub enum InstallSubCommand { // -------------------------------------- // GetDir: For passing the directory name // -------------------------------------- -#[derive(Debug, Args)] +#[derive(Debug, Args,)] pub struct GetDir { /// Specifies the name of the project directory to initialize pub dir_name: String, } + +// for passing both the directory name and the language of choice +// the args is an inbuilt library from clap used to take input from the terminal +#[derive(Debug, Args,)] +pub struct CreateReactSubCommand { + pub dir_name: String, // Specifies the name of the project directory to initialize + + #[clap(long , short)] // this is used purposely for explicit choice when dealing with the argument via terminal hence --lan t or --lan=t + pub lan: ReactVariants, // specifies the language of choice for the project +} + +// to choose the language variants +// the valueEnum , just like the Args is a library from clap used to take argument from the terminal +#[derive(Debug, ValueEnum, Clone)] +pub enum ReactVariants { + J, // javascript + T, // typescript +} + + #[derive(Debug, Args)] pub struct Version { pub version_name: String, diff --git a/src/utils/helpers.rs b/src/utils/helpers.rs deleted file mode 100644 index 6b622ad..0000000 --- a/src/utils/helpers.rs +++ /dev/null @@ -1,569 +0,0 @@ -use std::error::Error; -use std::fs; -use std::io::Error as OutputError; -use std::process::{Command, Output}; - -// ------------------- -// Checker functions -// ------------------- -pub fn is_node_installed() -> bool { - let output = Command::new("node").arg("--version").output(); - - check_output(output) -} - -pub fn is_npm_installed() -> bool { - let output = Command::new("npm").arg("--version").output(); - - check_output(output) -} - -pub fn is_nestjs_installed() -> bool { - let output = Command::new("nest").arg("--version").output(); - - check_output(output) -} - -pub fn is_php_installed() -> bool { - let output = Command::new("php").arg("--version").output(); - - check_output(output) -} - -pub fn is_composer_installed() -> bool { - let output = Command::new("composer").arg("--version").output(); - - check_output(output) -} - -pub fn is_scarb_installed() -> bool { - let output = Command::new("scarb").arg("--version").output(); - - check_output(output) -} - -pub fn is_forge_installed() -> bool { - let output = Command::new("forge").arg("--version").output(); - - check_output(output) -} - -pub fn is_python_installed() -> bool { - //TODO: Check for python3 for macos - let output = Command::new("python3").arg("--version").output(); - - check_output(output) -} - -pub fn is_pip_installed() -> bool { - let output = Command::new("pip3").arg("--version").output(); - - check_output(output) -} - -pub fn is_starkli_installed() -> bool { - let output = Command::new("starkli").arg("--version").output(); - - check_output(output) -} - -pub fn is_brew_installed() -> bool { - let output = Command::new("brew").arg("--version").output(); - - check_output(output) -} - -pub fn is_choco_installed() -> bool { - let output = Command::new("choco").arg("--version").output(); - - check_output(output) -} - -pub fn is_nargo_installed() -> bool { - let output = Command::new("noirup").arg("--version").output(); - - check_output(output) -} - -pub fn is_solana_cli_installed() -> bool { - let output = Command::new("solana").arg("--version").output(); - - check_output(output) -} - -pub fn is_anchor_installed() -> bool { - let output = Command::new("anchor").arg("--version").output(); - - check_output(output) -} - -pub fn get_os() -> String { - let os_family = std::env::consts::OS; - - os_family.to_string() -} - -fn check_output(output: Result) -> bool { - match output { - Ok(output) => { - if output.status.success() { - true - } else { - false - } - } - _ => false, - } -} - -// ------------------- -// Scaffold functions -// ------------------- -pub fn create_react_app(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npx") - .args(["create-react-app", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_react_app_with_typescript(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npx") - .args([ - "create-react-app", - project_name.as_str(), - "--template", - "typescript", - ]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_hardhat_project(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - fs::create_dir_all(project_name.as_str())?; - - Command::new("npm") - .args(["init", "--yes"]) - .current_dir(project_name.as_str()) - .spawn()? - .wait()?; - - Command::new("npx") - .args(["hardhat", "init"]) - .current_dir(project_name.as_str()) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_nestjs_app(project_name: String) -> Result<(), Box> { - if !is_nestjs_installed() { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } - - Command::new("npm") - .args(["i", "-g", "@nestjs/cli"]) - .spawn()? - .wait()?; - } - - Command::new("nest") - .args(["new", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) -} - -pub fn create_laravel_project(project_name: String) -> Result<(), Box> { - if !is_php_installed() && !is_composer_installed() { - return Err("You don't have PHP or Composer installed".into()); - } else { - println!("Creating Laravel project: {}", project_name); - - Command::new("composer") - .args(["create-project", "laravel/laravel", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_next_app(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npx") - .args(["create-next-app@latest", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_new_foundry_project(project_name: String) -> Result<(), Box> { - if !is_forge_installed() { - return Err("You don't have Forge installed".into()); - } else { - Command::new("forge") - .args(["init", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_django_project(project_name: String) -> Result<(), Box> { - if !is_python_installed() && !is_pip_installed() { - return Err("You don't have Python installed".into()); - } else { - Command::new("django-admin") - .args(["startproject", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_vue_project(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npm") - .args(["create", "vue@latest", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_vite_project(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npm") - .args(["create", "vite@latest", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_noir_project(project_name: String) -> Result<(), Box> { - if !is_nargo_installed() { - return Err("You don't have nargo installed".into()); - } else { - Command::new("nargo") - .args(["new", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_starknet_foundry_project(project_name: String) -> Result<(), Box> { - if !is_forge_installed() { - return Err("You don't have Forge installed".into()); - } else { - Command::new("snforge") - .args(["init", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_expo_app(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npx") - .args(["create-expo-app", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_adonis_project(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npm") - .args(["init", "adonisjs@latest", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_anchor_project_ts(project_name: String) -> Result<(), Box> { - if !is_solana_cli_installed() { - return Err("You don't have the Solana CLI installed".into()); - } else if !is_anchor_installed() { - return Err("You don't have Anchor installed".into()); - } else if !is_node_installed() { - return Err("You don't have Node installed".into()); - } else { - Command::new("anchor") - .args(["init", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn create_anchor_project_rs(project_name: String) -> Result<(), Box> { - if !is_solana_cli_installed() { - return Err("You don't have the Solana CLI installed".into()); - } else if !is_anchor_installed() { - return Err("You don't have Anchor installed".into()); - } else { - Command::new("anchor") - .args(["init", "--test-template", "rust", project_name.as_str()]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -// ------------------- -// Installer functions -// ------------------- - -pub fn install_brew() -> Result<(), Box> { - println!("Installing Homebrew..."); - - let script_url = "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"; - let command = format!("curl -fsSL {} | /bin/bash", script_url); - - Command::new("sh").arg("-c").arg(command).output()?; - - Ok(()) -} - -pub fn install_choco() -> Result<(), Box> { - println!("Installing Chocolatey..."); - - let powershell_script = r#" - Set-ExecutionPolicy Bypass -Scope Process -Force; - [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; - iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) - "#; - - Command::new("powershell") - .arg("-Command") - .arg(powershell_script) - .output()?; - - Ok(()) -} - -pub fn install_node() -> Result<(), Box> { - println!("Installing the Latest Version of Node..."); - - // let os_family = std::env::consts::OS; - let os_family = get_os(); - - match os_family.as_str() { - "linux" => install_node_linux(), - "windows" => install_node_windows(), - "macos" => install_node_macos(), - _ => panic!("Unsupported OS"), - } -} - -pub fn install_node_linux() -> Result<(), Box> { - println!("Installing Node.js on Linux..."); - - Command::new("sudo") - .arg("apt-get") - .args(["update"]) - .status()?; - - Command::new("sudo") - .arg("apt-get") - .args(["install", "-y", "nodejs"]) - .status()?; - - Ok(()) -} - -pub fn install_node_macos() -> Result<(), Box> { - println!("Installing Node.js on macOS..."); - - if is_brew_installed() { - Command::new("brew").args(["install", "node"]).status()?; - } - - Ok(()) -} - -pub fn install_node_windows() -> Result<(), Box> { - println!("Installing Node.js on Windows..."); - - if is_choco_installed() { - Command::new("choco") - .args(["install", "nodejs", "-y"]) - .status()?; - } - - Ok(()) -} - -pub fn install_scarb() -> Result<(), Box> { - println!("Installing the Latest Version of Scarb..."); - - let install_cmd = - "curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh"; - - Command::new("sh").arg("-c").arg(install_cmd).output()?; - - Ok(()) -} - -pub fn install_starkli() -> Result<(), Box> { - println!("Installing the Latest Version of Starkli..."); - - Command::new("sh") - .arg("-c") - .arg("curl https://get.starkli.sh | sh") - .output()?; - - Command::new("sh").arg("-c").arg("starkliup").output()?; - - Ok(()) -} - -pub fn install_composer() {} - -pub fn install_forge() -> Result<(), Box> { - println!("Installing the Latest Version of Foundry..."); - - Command::new("sh") - .arg("-c") - .arg("curl -L https://foundry.paradigm.xyz | bash") - .output()?; - - Command::new("sh").arg("-c").arg("foundryup").output()?; - - Ok(()) -} - -pub fn install_nargo() -> Result<(), Box> { - println!("Installing the Latest Version of Noir..."); - - Command::new("sh") - .arg("-c") - .arg("curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash") - .output()?; - - Command::new("sh").arg("-c").arg("noirup").output()?; - - Ok(()) -} - -pub fn install_snforge(version: String) -> Result<(), Box> { - if version.eq("latest") { - println!("Installing the Latest Version of Starknet Foundry, Please wait..."); - - Command::new("sh") - .arg("-c") - .arg("curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh") - .output()?; - - Command::new("sh").arg("-c").arg("snfoundryup").output()?; - - Ok(()) - } else { - println!( - "Installing Version {} of Starknet Foundry, Please wait...", - version - ); - - Command::new("sh") - .args(["-c", "curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh"]) - .output()?; - - Command::new("sh") - .args(["-c", "snfoundryup", "-v", version.as_str()]) - .output()?; - - Ok(()) - } -} - -pub fn create_rainbowkit_wagmi_next_app(project_name: String) -> Result<(), Box> { - if !is_npm_installed() { - return Err("You don't have npm installed".into()); - } else { - Command::new("npm") - .args([ - "init", - "@rainbow-me/rainbowkit@latest", - project_name.as_str(), - ]) - .spawn()? - .wait()?; - - Ok(()) - } -} - -pub fn install_solana_cli() -> Result<(), Box> { - if is_solana_cli_installed() { - println!("You already have the Solana CLI installed"); - } else { - println!("Installing the Latest Version of the Solana CLI..."); - Command::new("sh") - .arg("-c") - .arg("curl -sSfL https://release.anza.xyz/stable/install | sh") - .status()?; - } - Ok(()) -} -pub fn install_anchor() -> Result<(), Box> { - println!("Installing Anchor..."); - - Command::new("cargo") - .args([ - "install", - "--git", - "https://github.com/coral-xyz/anchor", - "avm", - "--force", - ]) - .spawn()? - .wait()?; - - Ok(()) -} diff --git a/src/utils/helpers/checkers.rs b/src/utils/helpers/checkers.rs new file mode 100644 index 0000000..98b9850 --- /dev/null +++ b/src/utils/helpers/checkers.rs @@ -0,0 +1,114 @@ + + +use std::io::Error as OutputError; +use std::process::{Command, Output}; + +pub fn is_node_installed() -> bool { + let output = Command::new("node").arg("--version").output(); + + check_output(output) +} + +pub fn is_npm_installed() -> bool { + let output = Command::new("npm").arg("--version").output(); + + check_output(output) +} + +pub fn is_nestjs_installed() -> bool { + let output = Command::new("nest").arg("--version").output(); + + check_output(output) +} + +pub fn is_php_installed() -> bool { + let output = Command::new("php").arg("--version").output(); + + check_output(output) +} + +pub fn is_composer_installed() -> bool { + let output = Command::new("composer").arg("--version").output(); + + check_output(output) +} + +pub fn is_scarb_installed() -> bool { + let output = Command::new("scarb").arg("--version").output(); + + check_output(output) +} + +pub fn is_forge_installed() -> bool { + let output = Command::new("forge").arg("--version").output(); + + check_output(output) +} + +pub fn is_python_installed() -> bool { + //TODO: Check for python3 for macos + let output = Command::new("python3").arg("--version").output(); + + check_output(output) +} + +pub fn is_pip_installed() -> bool { + let output = Command::new("pip3").arg("--version").output(); + + check_output(output) +} + +pub fn is_starkli_installed() -> bool { + let output = Command::new("starkli").arg("--version").output(); + + check_output(output) +} + +pub fn is_brew_installed() -> bool { + let output = Command::new("brew").arg("--version").output(); + + check_output(output) +} + +pub fn is_choco_installed() -> bool { + let output = Command::new("choco").arg("--version").output(); + + check_output(output) +} + +pub fn is_nargo_installed() -> bool { + let output = Command::new("noirup").arg("--version").output(); + + check_output(output) +} + +pub fn is_solana_cli_installed() -> bool { + let output = Command::new("solana").arg("--version").output(); + + check_output(output) +} + +pub fn is_anchor_installed() -> bool { + let output = Command::new("anchor").arg("--version").output(); + + check_output(output) +} + +pub fn get_os() -> String { + let os_family = std::env::consts::OS; + + os_family.to_string() +} + +fn check_output(output: Result) -> bool { + match output { + Ok(output) => { + if output.status.success() { + true + } else { + false + } + } + _ => false, + } +} \ No newline at end of file diff --git a/src/utils/helpers/installer.rs b/src/utils/helpers/installer.rs new file mode 100644 index 0000000..5d3e316 --- /dev/null +++ b/src/utils/helpers/installer.rs @@ -0,0 +1,211 @@ +use std::error::Error; +use std::process::{Command, }; +use crate::utils::helpers::checkers::*; + +pub fn install_brew() -> Result<(), Box> { + println!("Installing Homebrew..."); + + let script_url = "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"; + let command = format!("curl -fsSL {} | /bin/bash", script_url); + + Command::new("sh").arg("-c").arg(command).output()?; + + Ok(()) +} + +pub fn install_choco() -> Result<(), Box> { + println!("Installing Chocolatey..."); + + let powershell_script = r#" + Set-ExecutionPolicy Bypass -Scope Process -Force; + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; + iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) + "#; + + Command::new("powershell") + .arg("-Command") + .arg(powershell_script) + .output()?; + + Ok(()) +} + +pub fn install_node() -> Result<(), Box> { + println!("Installing the Latest Version of Node..."); + + // let os_family = std::env::consts::OS; + let os_family = get_os(); + + match os_family.as_str() { + "linux" => install_node_linux(), + "windows" => install_node_windows(), + "macos" => install_node_macos(), + _ => panic!("Unsupported OS"), + } +} + +pub fn install_node_linux() -> Result<(), Box> { + println!("Installing Node.js on Linux..."); + + Command::new("sudo") + .arg("apt-get") + .args(["update"]) + .status()?; + + Command::new("sudo") + .arg("apt-get") + .args(["install", "-y", "nodejs"]) + .status()?; + + Ok(()) +} + +pub fn install_node_macos() -> Result<(), Box> { + println!("Installing Node.js on macOS..."); + + if is_brew_installed() { + Command::new("brew").args(["install", "node"]).status()?; + } + + Ok(()) +} + +pub fn install_node_windows() -> Result<(), Box> { + println!("Installing Node.js on Windows..."); + + if is_choco_installed() { + Command::new("choco") + .args(["install", "nodejs", "-y"]) + .status()?; + } + + Ok(()) +} + +pub fn install_scarb() -> Result<(), Box> { + println!("Installing the Latest Version of Scarb..."); + + let install_cmd = + "curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh"; + + Command::new("sh").arg("-c").arg(install_cmd).output()?; + + Ok(()) +} + +pub fn install_starkli() -> Result<(), Box> { + println!("Installing the Latest Version of Starkli..."); + + Command::new("sh") + .arg("-c") + .arg("curl https://get.starkli.sh | sh") + .output()?; + + Command::new("sh").arg("-c").arg("starkliup").output()?; + + Ok(()) +} + +pub fn install_composer() {} + +pub fn install_forge() -> Result<(), Box> { + println!("Installing the Latest Version of Foundry..."); + + Command::new("sh") + .arg("-c") + .arg("curl -L https://foundry.paradigm.xyz | bash") + .output()?; + + Command::new("sh").arg("-c").arg("foundryup").output()?; + + Ok(()) +} + +pub fn install_nargo() -> Result<(), Box> { + println!("Installing the Latest Version of Noir..."); + + Command::new("sh") + .arg("-c") + .arg("curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash") + .output()?; + + Command::new("sh").arg("-c").arg("noirup").output()?; + + Ok(()) +} + +pub fn install_snforge(version: String) -> Result<(), Box> { + if version.eq("latest") { + println!("Installing the Latest Version of Starknet Foundry, Please wait..."); + + Command::new("sh") + .arg("-c") + .arg("curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh") + .output()?; + + Command::new("sh").arg("-c").arg("snfoundryup").output()?; + + Ok(()) + } else { + println!( + "Installing Version {} of Starknet Foundry, Please wait...", + version + ); + + Command::new("sh") + .args(["-c", "curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh"]) + .output()?; + + Command::new("sh") + .args(["-c", "snfoundryup", "-v", version.as_str()]) + .output()?; + + Ok(()) + } +} + +pub fn create_rainbowkit_wagmi_next_app(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npm") + .args([ + "init", + "@rainbow-me/rainbowkit@latest", + project_name.as_str(), + ]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn install_solana_cli() -> Result<(), Box> { + if is_solana_cli_installed() { + println!("You already have the Solana CLI installed"); + } else { + println!("Installing the Latest Version of the Solana CLI..."); + Command::new("sh") + .arg("-c") + .arg("curl -sSfL https://release.anza.xyz/stable/install | sh") + .status()?; + } + Ok(()) +} +pub fn install_anchor() -> Result<(), Box> { + println!("Installing Anchor..."); + + Command::new("cargo") + .args([ + "install", + "--git", + "https://github.com/coral-xyz/anchor", + "avm", + "--force", + ]) + .spawn()? + .wait()?; + + Ok(()) +} \ No newline at end of file diff --git a/src/utils/helpers/mod.rs b/src/utils/helpers/mod.rs new file mode 100644 index 0000000..f16afe5 --- /dev/null +++ b/src/utils/helpers/mod.rs @@ -0,0 +1,4 @@ + +pub mod scaffold; +pub mod installer; +pub mod checkers; \ No newline at end of file diff --git a/src/utils/helpers/scaffold.rs b/src/utils/helpers/scaffold.rs new file mode 100644 index 0000000..619a4eb --- /dev/null +++ b/src/utils/helpers/scaffold.rs @@ -0,0 +1,241 @@ +use std::error::Error; +use std::fs; +use std::process::{Command, }; +use crate::utils::helpers::checkers::*; + +pub fn create_react_app(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npx") + .args(["create-react-app", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_react_app_with_typescript(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npx") + .args([ + "create-react-app", + project_name.as_str(), + "--template", + "typescript", + ]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_hardhat_project(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + fs::create_dir_all(project_name.as_str())?; + + Command::new("npm") + .args(["init", "--yes"]) + .current_dir(project_name.as_str()) + .spawn()? + .wait()?; + + Command::new("npx") + .args(["hardhat", "init"]) + .current_dir(project_name.as_str()) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_nestjs_app(project_name: String) -> Result<(), Box> { + if !is_nestjs_installed() { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } + + Command::new("npm") + .args(["i", "-g", "@nestjs/cli"]) + .spawn()? + .wait()?; + } + + Command::new("nest") + .args(["new", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) +} + +pub fn create_laravel_project(project_name: String) -> Result<(), Box> { + if !is_php_installed() && !is_composer_installed() { + return Err("You don't have PHP or Composer installed".into()); + } else { + println!("Creating Laravel project: {}", project_name); + + Command::new("composer") + .args(["create-project", "laravel/laravel", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_next_app(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npx") + .args(["create-next-app@latest", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_new_foundry_project(project_name: String) -> Result<(), Box> { + if !is_forge_installed() { + return Err("You don't have Forge installed".into()); + } else { + Command::new("forge") + .args(["init", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_django_project(project_name: String) -> Result<(), Box> { + if !is_python_installed() && !is_pip_installed() { + return Err("You don't have Python installed".into()); + } else { + Command::new("django-admin") + .args(["startproject", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_vue_project(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npm") + .args(["create", "vue@latest", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_vite_project(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npm") + .args(["create", "vite@latest", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_noir_project(project_name: String) -> Result<(), Box> { + if !is_nargo_installed() { + return Err("You don't have nargo installed".into()); + } else { + Command::new("nargo") + .args(["new", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_starknet_foundry_project(project_name: String) -> Result<(), Box> { + if !is_forge_installed() { + return Err("You don't have Forge installed".into()); + } else { + Command::new("snforge") + .args(["init", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_expo_app(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npx") + .args(["create-expo-app", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_adonis_project(project_name: String) -> Result<(), Box> { + if !is_npm_installed() { + return Err("You don't have npm installed".into()); + } else { + Command::new("npm") + .args(["init", "adonisjs@latest", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_anchor_project_ts(project_name: String) -> Result<(), Box> { + if !is_solana_cli_installed() { + return Err("You don't have the Solana CLI installed".into()); + } else if !is_anchor_installed() { + return Err("You don't have Anchor installed".into()); + } else if !is_node_installed() { + return Err("You don't have Node installed".into()); + } else { + Command::new("anchor") + .args(["init", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} + +pub fn create_anchor_project_rs(project_name: String) -> Result<(), Box> { + if !is_solana_cli_installed() { + return Err("You don't have the Solana CLI installed".into()); + } else if !is_anchor_installed() { + return Err("You don't have Anchor installed".into()); + } else { + Command::new("anchor") + .args(["init", "--test-template", "rust", project_name.as_str()]) + .spawn()? + .wait()?; + + Ok(()) + } +} \ No newline at end of file