feat: matcha run detects runtime and spawns watch
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+49
-1
No files matched your search
@@ -0,0 +1,42 @@
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::runtime::{self, Runtime};
|
||||
|
||||
fn command_for(rt: Runtime) -> (&'static str, Vec<&'static str>) {
|
||||
match rt {
|
||||
Runtime::Deno => ("deno", vec!["task", "dev"]),
|
||||
Runtime::Bun => ("bun", vec!["--watch", "run", "dev"]),
|
||||
Runtime::Node => ("npm", vec!["run", "dev"]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(dir: &Path) -> std::io::Result<()> {
|
||||
let rt = runtime::detect(dir).ok_or_else(|| Error::new(
|
||||
ErrorKind::NotFound,
|
||||
"no runtime detected (need deno.json, bun.lock, or package.json)",
|
||||
))?;
|
||||
let (prog, args) = command_for(rt);
|
||||
println!("→ {prog} {}", args.join(" "));
|
||||
let status = std::process::Command::new(prog).args(&args).current_dir(dir).status()?;
|
||||
std::process::exit(status.code().unwrap_or(1));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::runtime::Runtime;
|
||||
|
||||
#[test]
|
||||
fn deno_uses_task_dev() {
|
||||
assert_eq!(command_for(Runtime::Deno), ("deno", vec!["task", "dev"]));
|
||||
}
|
||||
#[test]
|
||||
fn bun_watch() {
|
||||
assert_eq!(command_for(Runtime::Bun), ("bun", vec!["--watch", "run", "dev"]));
|
||||
}
|
||||
#[test]
|
||||
fn node_watch() {
|
||||
assert_eq!(command_for(Runtime::Node), ("npm", vec!["run", "dev"]));
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -1,5 +1,6 @@
|
||||
mod cli;
|
||||
mod cmd_new;
|
||||
mod cmd_run;
|
||||
mod runtime;
|
||||
mod template;
|
||||
|
||||
@@ -15,7 +16,12 @@ fn main() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Run => eprintln!("run: not yet implemented"),
|
||||
Command::Run => {
|
||||
if let Err(e) = cmd_run::run(std::path::Path::new(".")) {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Create { .. } => eprintln!("create: not yet implemented"),
|
||||
Command::Add { .. } => eprintln!("add: not yet implemented"),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user