use std::process::Command;
use std::time::Instant;
use anyhow::Result;
use tracing::{error, info};
use crate::utils::{install_npm_tool, is_command_available};
pub fn run_cspell_linter() -> Result<()> {
if !is_command_available("cspell") {
install_npm_tool("cspell")?;
}
let t = Instant::now();
info!(target: "cspell", "Running spell check on all files...");
let mut cmd = Command::new("cspell");
cmd.args([".", ".github/**/*", "--no-progress", "--show-context"]);
let output = cmd.output()?;
if output.status.success() {
info!(target: "cspell", "All files passed spell checking! ({:.3}s)", t.elapsed().as_secs_f64());
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
if !stdout.is_empty() {
println!("{stdout}");
}
if !stderr.is_empty() {
eprintln!("{stderr}");
}
println!();
println!("💡 To fix spelling issues:");
println!(" 1. Fix actual misspellings in the files");
println!(" 2. Add technical terms/proper nouns to project-words.txt");
println!(" 3. Use cspell suggestions: cspell --show-suggestions <file>");
println!();
error!(target: "cspell", "Spell checking failed. Please fix the issues above. ({:.3}s)", t.elapsed().as_secs_f64());
Err(anyhow::anyhow!("Spell checking failed"))
}
}