use std::path::Path;
use anyhow::{Context, Result};
use arcane_engine::scripting::ArcaneRuntime;
use super::{create_import_map, type_check};
pub fn run(entry: String, path: String) -> Result<()> {
let entry_path = std::fs::canonicalize(&entry)
.with_context(|| format!("Cannot find entry file: {entry}"))?;
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 eval_source = format!(
"JSON.stringify(globalThis.__arcaneAgent?.inspect('{path}'), null, 2) ?? 'null'"
);
let result = runtime.eval_to_string(&eval_source)?;
println!("{result}");
Ok(())
}