use super::{DetectResult, ProjectDef};
use std::path::Path;

use anyhow::Result;

pub const DEF: ProjectDef = ProjectDef {
    name: "remix",
    detect: detect,
    build_cmd: &["npm", "run", "build"],
    run_cmd: &["npm", "run", "start"],
};

fn detect(dir: &Path) -> Result<DetectResult> {
    if dir.join("remix.config.js").exists()
        || dir.join("remix.config.ts").exists()
        || dir.join("remix.config.mjs").exists()
    {
        return Ok(DetectResult::matched());
    }

    let pkg = dir.join("package.json");
    if pkg.exists() {
        let content = std::fs::read_to_string(&pkg)?;
        if content.contains("@remix-run/") {
            return Ok(DetectResult::matched());
        }
    }

    Ok(DetectResult::no_match())
}
