vdev/commands/
fmt.rs

1use anyhow::Result;
2
3use crate::{app, utils::git::git_ls_files};
4
5pub(crate) const PRETTIER_EXTENSIONS: &[&str] =
6    &["*.yml", "*.yaml", "*.js", "*.ts", "*.tsx", "*.json"];
7
8/// Apply format changes across the repository
9#[derive(clap::Args, Debug)]
10#[command()]
11pub struct Cli {}
12
13impl Cli {
14    pub fn exec(self) -> Result<()> {
15        info!("Checking style (trailing spaces, line endings)...");
16        app::exec("scripts/check-style.sh", ["--fix"], true)?;
17
18        info!("Formatting Rust code...");
19        app::exec("cargo", ["fmt", "--all"], true)?;
20
21        for ext in PRETTIER_EXTENSIONS {
22            let files = git_ls_files(Some(ext))?;
23            if files.is_empty() {
24                continue;
25            }
26            info!("Formatting {ext} files with prettier...");
27            let args: Vec<&str> = ["--ignore-path", ".prettierignore", "--write"]
28                .into_iter()
29                .chain(files.iter().map(String::as_str))
30                .collect();
31            app::exec("prettier", &args, true)?;
32        }
33
34        Ok(())
35    }
36}