use crate::{error::CliError, ui, Result};
use forgedb_watcher::{auto_watch, RegenerateResult};
use std::path::Path;
pub struct DevOptions {
pub schema: String,
pub output: String,
pub debounce: u64,
pub clear: bool,
}
pub fn run(options: DevOptions) -> Result<()> {
let schema_path = Path::new(&options.schema);
if !schema_path.exists() {
return Err(CliError::SchemaNotFound(format!(
"Schema file not found: {}",
options.schema
)));
}
ui::header("👁️", "ForgeDB Dev Mode");
println!();
ui::info(&format!("Watching: {}", options.schema));
ui::info(&format!("Output: {}", options.output));
ui::info(&format!("Debounce: {}ms", options.debounce));
println!();
ui::info("Press Ctrl+C to stop watching");
println!();
println!("{}", "─".repeat(60));
println!();
let clear_terminal = options.clear;
let callback = Box::new(move |result: &RegenerateResult| {
if clear_terminal {
clear_screen();
}
if result.success {
ui::success(&result.message);
if let Some(ref path) = result.output_path {
ui::info(&format!("Generated: {}", path.display()));
}
} else {
ui::error(&result.message);
}
println!();
ui::info("Waiting for changes...");
println!();
});
auto_watch(
&options.schema,
&options.output,
options.debounce,
Some(callback),
)
.map_err(|e| CliError::Other(format!("Watcher error: {}", e)))?;
Ok(())
}
fn clear_screen() {
print!("\x1B[2J\x1B[1;1H");
}