|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 2 | +// If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 3 | + |
| 4 | +/// Andromeda Satellite - Format |
| 5 | +/// |
| 6 | +/// A minimal executable focused solely on formatting JavaScript/TypeScript files. |
| 7 | +/// Designed for container instances where only formatting capability is needed. |
| 8 | +use andromeda::{CliError, CliResult}; |
| 9 | +use clap::Parser as ClapParser; |
| 10 | +use console::Style; |
| 11 | +use std::path::PathBuf; |
| 12 | + |
| 13 | +#[derive(Debug, ClapParser)] |
| 14 | +#[command(name = "andromeda-fmt")] |
| 15 | +#[command(about = "Andromeda Satellite - Format JavaScript/TypeScript files")] |
| 16 | +#[command(version = env!("CARGO_PKG_VERSION"))] |
| 17 | +struct Cli { |
| 18 | + /// The file(s) or directory(ies) to format |
| 19 | + #[arg(required = false)] |
| 20 | + paths: Vec<PathBuf>, |
| 21 | +} |
| 22 | + |
| 23 | +fn main() -> CliResult<()> { |
| 24 | + andromeda::error::init_error_reporting(); |
| 25 | + |
| 26 | + let cli = Cli::parse(); |
| 27 | + |
| 28 | + let config = andromeda::config::ConfigManager::load_or_default(None); |
| 29 | + |
| 30 | + let files_to_format = |
| 31 | + andromeda::helper::find_formattable_files_for_format(&cli.paths, &config.format) |
| 32 | + .map_err(|e| CliError::TestExecution(format!("{e}")))?; |
| 33 | + |
| 34 | + if files_to_format.is_empty() { |
| 35 | + let warning = Style::new().yellow().bold().apply_to("⚠️"); |
| 36 | + let msg = Style::new() |
| 37 | + .yellow() |
| 38 | + .apply_to("No formattable files found."); |
| 39 | + println!("{warning} {msg}"); |
| 40 | + return Ok(()); |
| 41 | + } |
| 42 | + |
| 43 | + let count = Style::new().cyan().apply_to(files_to_format.len()); |
| 44 | + println!("Found {count} file(s) to format"); |
| 45 | + println!("{}", Style::new().dim().apply_to("─".repeat(40))); |
| 46 | + |
| 47 | + let mut already_formatted_count = 0; |
| 48 | + let mut formatted_count = 0; |
| 49 | + |
| 50 | + for path in &files_to_format { |
| 51 | + match andromeda::format::format_file(path) |
| 52 | + .map_err(|e| CliError::TestExecution(format!("{e}")))? |
| 53 | + { |
| 54 | + andromeda::format::FormatResult::Changed => formatted_count += 1, |
| 55 | + andromeda::format::FormatResult::AlreadyFormatted => already_formatted_count += 1, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + println!(); |
| 60 | + let success = Style::new().green().bold().apply_to("✅"); |
| 61 | + let complete_msg = Style::new().green().bold().apply_to("Formatting complete"); |
| 62 | + println!("{success} {complete_msg}:"); |
| 63 | + |
| 64 | + if formatted_count > 0 { |
| 65 | + let formatted_icon = Style::new().green().apply_to("📄"); |
| 66 | + let formatted_num = Style::new().green().bold().apply_to(formatted_count); |
| 67 | + let formatted_text = if formatted_count == 1 { |
| 68 | + "file" |
| 69 | + } else { |
| 70 | + "files" |
| 71 | + }; |
| 72 | + println!(" {formatted_icon} {formatted_num} {formatted_text} formatted"); |
| 73 | + } |
| 74 | + |
| 75 | + if already_formatted_count > 0 { |
| 76 | + let already_icon = Style::new().cyan().apply_to("✨"); |
| 77 | + let already_num = Style::new().cyan().bold().apply_to(already_formatted_count); |
| 78 | + let already_text = if already_formatted_count == 1 { |
| 79 | + "file" |
| 80 | + } else { |
| 81 | + "files" |
| 82 | + }; |
| 83 | + let already_msg = Style::new().cyan().apply_to("already formatted"); |
| 84 | + println!(" {already_icon} {already_num} {already_text} {already_msg}"); |
| 85 | + } |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
0 commit comments