nsh-cli 0.7.1

The nsh command line tool: build, package and run apps.
//! Finds an app's entry module: commands take the entry itself or the
//! app's directory (the current one by default), whose `nsh.json` names
//! the entry (`"entry": "app/App.tsx"`, relative to it), else whose
//! `App.tsx` is the entry. That name matches in any case (`app.tsx` too,
//! also on case-sensitive file systems). A directory whose `nsh.json` names
//! a Rust root component (`"root"`) is a Rust app: its entry is that
//! `nsh.json` (`rust_app::is_root`), and its `App.tsx`, if any, is used
//! only when named.

use std::path::{Path, PathBuf};

use anyhow::bail;
pub(crate) use nsh_bundler::app_dir::ENTRY;

/// The entry module `path` names: the file itself, or the directory's: the
/// one its `nsh.json` names (`"entry"`), else its [`ENTRY`].
pub(crate) fn resolve(path: &Path) -> anyhow::Result<PathBuf> {
    if !path.is_dir() {
        if path.exists() {
            return Ok(path.to_owned());
        }
        bail!("{} does not exist", path.display());
    }
    let config = path.join("nsh.json");
    if nsh_bundler::app_dir::read(&config)
        .ok()
        .and_then(|text| serde_json::from_str::<serde_json::Value>(&text).ok())
        .is_some_and(|json| json.get("root").is_some())
    {
        return Ok(config);
    }
    match nsh_bundler::app_dir::config_entry(path) {
        Ok(Some(entry)) if entry.is_file() => return Ok(entry),
        Ok(Some(entry)) => bail!(
            "{}: \"entry\" names {}, which does not exist",
            config.display(),
            entry.display()
        ),
        Ok(None) => {}
        Err(e) => bail!("{}: {e}", config.display()),
    }
    let entry = std::fs::read_dir(path)?
        .filter_map(|e| e.ok().map(|e| e.path()))
        .filter(|p| p.is_file())
        .find(|p| {
            p.file_name()
                .and_then(|n| n.to_str())
                .is_some_and(|n| n.eq_ignore_ascii_case(ENTRY))
        });
    match entry {
        Some(entry) => Ok(entry),
        None => bail!(
            "no {ENTRY} in {}: add one, or pass the entry module",
            path.display()
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn conventional_entry() {
        let dir = std::env::temp_dir().join(format!("nsh-entry-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        assert!(resolve(&dir).is_err());
        std::fs::write(dir.join("Other.tsx"), "").unwrap();
        assert!(resolve(&dir).is_err());
        std::fs::write(dir.join("app.tsx"), "").unwrap();
        assert_eq!(resolve(&dir).unwrap(), dir.join("app.tsx"));
        // A file is the entry whatever its name.
        assert_eq!(
            resolve(&dir.join("Other.tsx")).unwrap(),
            dir.join("Other.tsx")
        );
        assert!(resolve(&dir.join("missing.tsx")).is_err());
        // A TSX app whose directory is its crate (`nsh init --template
        // mixed`): with no `"root"`, its `App.tsx`.
        std::fs::write(dir.join("nsh.json"), r#"{ "rust": "." }"#).unwrap();
        assert_eq!(resolve(&dir).unwrap(), dir.join("app.tsx"));
        // A Rust app: its `nsh.json`, unless the TSX entry is named.
        std::fs::write(dir.join("nsh.json"), r#"{ "rust": ".", "root": "App" }"#).unwrap();
        assert_eq!(resolve(&dir).unwrap(), dir.join("nsh.json"));
        // A BOM (a Windows editor's UTF-8) does not hide it.
        std::fs::write(
            dir.join("nsh.json"),
            "\u{feff}{ \"rust\": \".\", \"root\": \"App\" }",
        )
        .unwrap();
        assert_eq!(resolve(&dir).unwrap(), dir.join("nsh.json"));
        assert_eq!(resolve(&dir.join("app.tsx")).unwrap(), dir.join("app.tsx"));
        // The entry `nsh.json` names, relative to it.
        std::fs::create_dir_all(dir.join("app")).unwrap();
        std::fs::write(dir.join("nsh.json"), r#"{ "entry": "app/Main.tsx" }"#).unwrap();
        assert!(resolve(&dir).is_err());
        std::fs::write(dir.join("app/Main.tsx"), "").unwrap();
        assert_eq!(resolve(&dir).unwrap(), dir.join("app/Main.tsx"));
        std::fs::write(dir.join("nsh.json"), r#"{ "entry": "../Main.tsx" }"#).unwrap();
        assert!(resolve(&dir).is_err());
        std::fs::remove_dir_all(&dir).unwrap();
    }
}