feat: matcha new scaffolds from embedded starter
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+50
-1
No files matched your search
@@ -0,0 +1,21 @@
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::template;
|
||||
|
||||
pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> {
|
||||
let dest = Path::new(name);
|
||||
if dest.exists() {
|
||||
return Err(Error::new(ErrorKind::AlreadyExists,
|
||||
format!("'{name}' already exists")));
|
||||
}
|
||||
std::fs::create_dir_all(dest)?;
|
||||
|
||||
match template_url {
|
||||
None => template::write_starter(dest, name)?,
|
||||
Some(_) => return Err(Error::new(ErrorKind::Unsupported,
|
||||
"--template-url not implemented yet")),
|
||||
}
|
||||
println!("✓ created {name}\n cd {name} && matcha run");
|
||||
Ok(())
|
||||
}
|
||||
+7
-1
@@ -1,4 +1,5 @@
|
||||
mod cli;
|
||||
mod cmd_new;
|
||||
mod runtime;
|
||||
mod template;
|
||||
|
||||
@@ -8,7 +9,12 @@ use cli::{Cli, Command};
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
match cli.command {
|
||||
Command::New { .. } => eprintln!("new: not yet implemented"),
|
||||
Command::New { name, template_url } => {
|
||||
if let Err(e) = cmd_new::run(&name, template_url.as_deref()) {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Run => eprintln!("run: not yet implemented"),
|
||||
Command::Create { .. } => eprintln!("create: not yet implemented"),
|
||||
Command::Add { .. } => eprintln!("add: not yet implemented"),
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
use assert_cmd::Command;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn new_scaffolds_into_named_dir() {
|
||||
let d = tempdir().unwrap();
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "my-api"]).assert().success();
|
||||
let root = d.path().join("my-api");
|
||||
assert!(root.join("src/app.module.ts").exists());
|
||||
assert!(root.join("public/index.html").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_refuses_existing_dir() {
|
||||
let d = tempdir().unwrap();
|
||||
std::fs::create_dir(d.path().join("taken")).unwrap();
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "taken"]).assert().failure();
|
||||
}
|
||||
Reference in new issue
Block a user