arcane-cli 0.10.2

CLI for Arcane - agent-native 2D game engine (dev server, testing, project scaffolding)
use std::path::Path;

use anyhow::{Context, Result};
use arcane_engine::scripting::ArcaneRuntime;

use super::{create_import_map, type_check};

/// Run the `arcane describe` command: load a game entry file headless and
/// call its agent describe function.
pub fn run(entry: String, verbosity: Option<String>) -> Result<()> {
    let entry_path = std::fs::canonicalize(&entry)
        .with_context(|| format!("Cannot find entry file: {entry}"))?;

    // Type check before running
    if !type_check::should_skip_type_check() {
        type_check::check_types(&entry_path)?;
    }

    let base_dir = entry_path.parent().unwrap_or_else(|| Path::new("."));
    let import_map = create_import_map(base_dir);
    let mut runtime = ArcaneRuntime::new_with_import_map(import_map);

    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()?;

    rt.block_on(async {
        runtime.execute_file(&entry_path).await
    })?;

    let verbosity_arg = verbosity
        .map(|v| format!("'{v}'"))
        .unwrap_or_else(|| "undefined".to_string());

    let eval_source = format!(
        "globalThis.__arcaneAgent?.describe({{ verbosity: {verbosity_arg} }}) ?? 'No agent registered.'"
    );

    let result = runtime.eval_to_string(&eval_source)?;
    println!("{result}");

    Ok(())
}