use std::path::{Path, PathBuf};
const CANDIDATES: [&str; 2] = ["src/app.ts", "src/main.ts"];
pub fn find(dir: &Path) -> Option<PathBuf> {
CANDIDATES.iter().map(|c| dir.join(c)).find(|p| p.exists())
}
pub fn candidates() -> String {
CANDIDATES.join(" or ")
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
fn touch(dir: &Path, rel: &str) {
let p = dir.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, "").unwrap();
}
#[test]
fn prefers_app_over_main() {
let d = tempdir().unwrap();
touch(d.path(), "src/main.ts");
touch(d.path(), "src/app.ts");
assert_eq!(find(d.path()), Some(d.path().join("src/app.ts")));
}
#[test]
fn falls_back_to_main() {
let d = tempdir().unwrap();
touch(d.path(), "src/main.ts");
assert_eq!(find(d.path()), Some(d.path().join("src/main.ts")));
}
#[test]
fn none_when_neither_exists() {
let d = tempdir().unwrap();
assert_eq!(find(d.path()), None);
}
}