Skip to content

Commit

Permalink
cli: Add ability for autofmt to read stdin
Browse files Browse the repository at this point in the history
This change adds the ability for autofmt to read from stdin by using
'-f -' or '--file -'
  • Loading branch information
mertzt89 committed Oct 7, 2023
1 parent 248d78f commit 153a346
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/cli/src/cli/autoformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,28 @@ impl Autoformat {

// Format single file
if let Some(file) = self.file {
let file_content = fs::read_to_string(&file);
let file_content = if file == "-" {
let mut contents = String::new();
std::io::stdin().read_to_string(&mut contents)?;
Ok(contents)
} else {
fs::read_to_string(&file)
};

match file_content {
Ok(s) => {
let edits = dioxus_autofmt::fmt_file(&s);
let out = dioxus_autofmt::apply_formats(&s, edits);
match fs::write(&file, out) {
Ok(_) => {
println!("formatted {}", file);
}
Err(e) => {
eprintln!("failed to write formatted content to file: {}", e);
if file == "-" {
print!("{}", out);
} else {
match fs::write(&file, out) {
Ok(_) => {
println!("formatted {}", file);
}
Err(e) => {
eprintln!("failed to write formatted content to file: {}", e);
}
}
}
}
Expand Down

0 comments on commit 153a346

Please sign in to comment.