From 4c9472ce51b9d922497c2bfb05614bcbb260e1a8 Mon Sep 17 00:00:00 2001 From: Gabriel de Quadros Ligneul Date: Fri, 8 Nov 2024 11:41:44 -0300 Subject: [PATCH] Unify the code for new and init commands * The new command creates the project directory and calls the init command. * The init code removes the git origin from the project directory. --- main/src/new.rs | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/main/src/new.rs b/main/src/new.rs index a9ecc2b..6a1fdf8 100644 --- a/main/src/new.rs +++ b/main/src/new.rs @@ -7,31 +7,18 @@ use crate::util::{ sys, }; use eyre::{bail, Context, Result}; -use std::{env::current_dir, path::Path}; +use std::{env, fs, path::Path}; -/// Creates a new Stylus project in the current directory -pub fn new(name: &Path, minimal: bool) -> Result<()> { - let repo = match minimal { - true => GITHUB_TEMPLATE_REPO_MINIMAL, - false => GITHUB_TEMPLATE_REPO, - }; - let output = sys::new_command("git") - .arg("clone") - .arg(repo) - .arg(name) - .output() - .wrap_err("git clone failed")?; - - if !output.status.success() { - bail!("git clone command failed"); - } - let path = current_dir().wrap_err("no current dir")?.join(name); - println!("{GREY}new project at: {}", path.to_string_lossy().mint()); - Ok(()) +/// Creates a new directory given the path and then initialize a stylus project. +pub fn new(path: &Path, minimal: bool) -> Result<()> { + fs::create_dir_all(path).wrap_err("failed to create project dir")?; + env::set_current_dir(path).wrap_err("failed to set project dir")?; + init(minimal) } +/// Creates a new Stylus project in the current directory. pub fn init(minimal: bool) -> Result<()> { - let current_dir = current_dir().wrap_err("no current dir")?; + let current_dir = env::current_dir().wrap_err("no current dir")?; let repo = if minimal { GITHUB_TEMPLATE_REPO_MINIMAL } else { @@ -51,6 +38,17 @@ pub fn init(minimal: bool) -> Result<()> { bail!("git clone command failed"); } + let output = sys::new_command("git") + .arg("remote") + .arg("remove") + .arg("origin") + .output() + .wrap_err("git remote remove failed")?; + + if !output.status.success() { + bail!("git remote remove command failed"); + } + println!( "{GREY}initialized project in: {}", current_dir.to_string_lossy().mint()