feat: runtime detection with matcha.toml override
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+106
No files matched your search
@@ -1,4 +1,5 @@
|
||||
mod cli;
|
||||
mod runtime;
|
||||
|
||||
use clap::Parser;
|
||||
use cli::{Cli, Command};
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
use std::path::Path;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum Runtime {
|
||||
Node,
|
||||
Deno,
|
||||
Bun,
|
||||
}
|
||||
|
||||
impl Runtime {
|
||||
#[allow(dead_code)]
|
||||
pub fn from_str(s: &str) -> Option<Runtime> {
|
||||
match s.trim() {
|
||||
"node" => Some(Runtime::Node),
|
||||
"deno" => Some(Runtime::Deno),
|
||||
"bun" => Some(Runtime::Bun),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn exists(dir: &Path, name: &str) -> bool {
|
||||
dir.join(name).exists()
|
||||
}
|
||||
|
||||
/// Reads `runtime = "..."` from matcha.toml without a TOML dep (one field only).
|
||||
#[allow(dead_code)]
|
||||
fn override_from_toml(dir: &Path) -> Option<Runtime> {
|
||||
let text = std::fs::read_to_string(dir.join("matcha.toml")).ok()?;
|
||||
for line in text.lines() {
|
||||
let line = line.trim();
|
||||
if let Some(rest) = line.strip_prefix("runtime") {
|
||||
let val = rest.trim().trim_start_matches('=').trim().trim_matches('"');
|
||||
return Runtime::from_str(val);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn detect(dir: &Path) -> Option<Runtime> {
|
||||
if let Some(r) = override_from_toml(dir) {
|
||||
return Some(r);
|
||||
}
|
||||
if exists(dir, "deno.json") || exists(dir, "deno.jsonc") {
|
||||
return Some(Runtime::Deno);
|
||||
}
|
||||
if exists(dir, "bun.lockb") || exists(dir, "bun.lock") {
|
||||
return Some(Runtime::Bun);
|
||||
}
|
||||
if exists(dir, "package.json") {
|
||||
return Some(Runtime::Node);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
|
||||
fn touch(dir: &std::path::Path, name: &str) {
|
||||
fs::write(dir.join(name), "{}").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deno_wins_over_package_json() {
|
||||
let d = tempdir().unwrap();
|
||||
touch(d.path(), "package.json");
|
||||
touch(d.path(), "deno.json");
|
||||
assert_eq!(detect(d.path()), Some(Runtime::Deno));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bun_lock_selects_bun() {
|
||||
let d = tempdir().unwrap();
|
||||
touch(d.path(), "package.json");
|
||||
touch(d.path(), "bun.lockb");
|
||||
assert_eq!(detect(d.path()), Some(Runtime::Bun));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_json_falls_back_to_node() {
|
||||
let d = tempdir().unwrap();
|
||||
touch(d.path(), "package.json");
|
||||
assert_eq!(detect(d.path()), Some(Runtime::Node));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matcha_toml_overrides() {
|
||||
let d = tempdir().unwrap();
|
||||
touch(d.path(), "deno.json");
|
||||
fs::write(d.path().join("matcha.toml"), "runtime = \"bun\"\n").unwrap();
|
||||
assert_eq!(detect(d.path()), Some(Runtime::Bun));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nothing_detected() {
|
||||
let d = tempdir().unwrap();
|
||||
assert_eq!(detect(d.path()), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user