Skip to content

Commit

Permalink
Unify the code for new and init commands
Browse files Browse the repository at this point in the history
* The new command creates the project directory and calls the init
  command.
* The init code removes the git origin from the project directory.
  • Loading branch information
gligneul committed Nov 8, 2024
1 parent b4bd15a commit 4c9472c
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions main/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down

0 comments on commit 4c9472c

Please sign in to comment.