Skip to content

Commit

Permalink
Implement brioche fmt --check argument
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Jan 15, 2024
1 parent 5a86b43 commit 3ba8119
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
19 changes: 19 additions & 0 deletions crates/brioche/src/brioche/script/format.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use crate::brioche::project::Project;

#[tracing::instrument(skip(project), err)]
Expand All @@ -13,6 +15,23 @@ pub async fn format(project: &Project) -> anyhow::Result<()> {
Ok(())
}

#[tracing::instrument(skip(project), err)]
pub async fn check_format(project: &Project) -> anyhow::Result<Vec<PathBuf>> {
let mut unformatted = vec![];

for path in project.local_module_paths() {
let contents = tokio::fs::read_to_string(path).await?;

let formatted_contents = format_code(&contents)?;

if contents != formatted_contents {
unformatted.push(path.to_owned());
}
}

Ok(unformatted)
}

pub fn format_code(contents: &str) -> anyhow::Result<String> {
let file_source =
biome_js_syntax::JsFileSource::ts().with_module_kind(biome_js_syntax::ModuleKind::Module);
Expand Down
27 changes: 24 additions & 3 deletions crates/brioche/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,32 @@ async fn format(args: FormatArgs) -> anyhow::Result<ExitCode> {

let format_future = async {
let project = brioche::brioche::project::resolve_project(&brioche, &args.project).await?;
brioche::brioche::script::format::format(&project).await?;

guard.shutdown_console().await;
if args.check {
let mut unformatted_files =
brioche::brioche::script::format::check_format(&project).await?;
unformatted_files.sort();

anyhow::Ok(ExitCode::SUCCESS)
guard.shutdown_console().await;

if unformatted_files.is_empty() {
println!("All files formatted");
Ok(ExitCode::SUCCESS)
} else {
println!("The following files are not formatted:");
for file in unformatted_files {
println!("- {}", file.display());
}

Ok(ExitCode::FAILURE)
}
} else {
brioche::brioche::script::format::format(&project).await?;

guard.shutdown_console().await;

anyhow::Ok(ExitCode::SUCCESS)
}
};

let exit_code = format_future
Expand Down

0 comments on commit 3ba8119

Please sign in to comment.