use std::fs;
use std::path::Path;
use std::process;
use super::json::{extraction_to_json, ts_extraction_to_json};
use super::ts_fragment::extract_ts_file;
use super::ts_fragment_types::TsFragment;
use super::walk::extract;
pub fn run_extract_dump(path: &str) {
let source = fs::read_to_string(path).unwrap_or_else(|err| {
eprintln!("failed to read {path}: {err}");
process::exit(1);
});
if let Some(grammar) = crate::parse::ts_grammar_for(path) {
let file_path = Path::new(path);
let root = file_path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let rel = file_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(path);
match extract_ts_file(root, rel, &source, grammar) {
Some(ts) => println!("{}", ts_extraction_to_json(&ts.purpose, &ts.fragment)),
None => println!("{}", ts_extraction_to_json(&None, &TsFragment::default())),
}
return;
}
let result = extract(&source);
println!("{}", extraction_to_json(&result));
}