matcha-cli 26.11.0-rc.2

The green-tea CLI: scaffold a project, generate controllers, steps and providers already wired into their module, and run with your runtime detected. One native binary, no JS runtime needed to install it.
use std::io::{Error, ErrorKind};
use std::path::Path;

use crate::runtime::Runtime;
use crate::template;

pub fn run(name: &str, template_url: Option<&str>, runtime: &str) -> std::io::Result<()> {
    let dest = Path::new(name);
    if dest.exists() {
        return Err(Error::new(
            ErrorKind::AlreadyExists,
            format!("'{name}' already exists"),
        ));
    }

    match template_url {
        None => {
            let rt = Runtime::from_str(runtime).ok_or_else(|| {
                Error::new(
                    ErrorKind::InvalidInput,
                    format!("unknown runtime '{runtime}'"),
                )
            })?;
            std::fs::create_dir_all(dest)?;
            template::write_starter(dest, name, rt)?;
        }
        Some(url) => clone_template(url, dest)?,
    }
    println!("✓ created {name}\n  cd {name} && matcha run");
    Ok(())
}

fn resolve_url(spec: &str) -> String {
    if let Some(rest) = spec.strip_prefix("gh:") {
        format!("https://github.com/{rest}")
    } else {
        spec.to_string()
    }
}

fn clone_template(url: &str, dest: &Path) -> std::io::Result<()> {
    let status = std::process::Command::new("git")
        .args(["clone", "--depth", "1", &resolve_url(url)])
        .arg(dest)
        .status()?;
    if !status.success() {
        return Err(Error::other("git clone failed"));
    }
    std::fs::remove_dir_all(dest.join(".git")).ok();
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn gh_shorthand_expands() {
        assert_eq!(
            resolve_url("gh:green-tea/tmpl"),
            "https://github.com/green-tea/tmpl"
        );
    }
    #[test]
    fn full_url_passes_through() {
        assert_eq!(
            resolve_url("https://example.com/x.git"),
            "https://example.com/x.git"
        );
    }
}