Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions apps/oxfmt/src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,35 @@ pub struct FormatCommand {
pub enum Mode {
/// Default CLI mode run against files and directories
Cli(OutputMode),
#[cfg(feature = "napi")]
/// Initialize `.oxfmtrc.jsonc` with default values
Init,
#[cfg(feature = "napi")]
/// Start language server protocol (LSP) server
Lsp,
}

fn mode() -> impl bpaf::Parser<Mode> {
let init = bpaf::long("init")
.help("Initialize `.oxfmtrc.jsonc` with default values")
.req_flag(Mode::Init)
.hide_usage();
let lsp = bpaf::long("lsp")
.help("Start language server protocol (LSP) server")
.req_flag(Mode::Lsp)
.hide_usage();
let mode_options = bpaf::construct!([init, lsp]).group_help("Mode Options:");

let output_mode_options = output_mode().map(Mode::Cli);

bpaf::construct!([mode_options, output_mode_options]).fallback(Mode::Cli(OutputMode::Write))
#[cfg(feature = "napi")]
{
let init = bpaf::long("init")
.help("Initialize `.oxfmtrc.jsonc` with default values")
.req_flag(Mode::Init)
.hide_usage();
let lsp = bpaf::long("lsp")
.help("Start language server protocol (LSP) server")
.req_flag(Mode::Lsp)
.hide_usage();
let mode_options = bpaf::construct!([init, lsp]).group_help("Mode Options:");

bpaf::construct!([mode_options, output_mode_options]).fallback(Mode::Cli(OutputMode::Write))
}
#[cfg(not(feature = "napi"))]
{
output_mode_options.fallback(Mode::Cli(OutputMode::Write))
}
}

/// Format output mode
Expand Down
2 changes: 2 additions & 0 deletions apps/oxfmt/src/cli/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl FormatRunner {
let cwd = self.cwd;
let FormatCommand { paths, mode, config_options, ignore_options, runtime_options } =
self.options;
// If `napi` feature is disabled, there is no other mode.
#[cfg_attr(not(feature = "napi"), expect(irrefutable_let_patterns))]
let Mode::Cli(format_mode) = mode else {
unreachable!("`FormatRunner` should only be called with Mode::Cli");
};
Expand Down
24 changes: 7 additions & 17 deletions apps/oxfmt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
use oxfmt::cli::{
CliRunResult, FormatRunner, Mode, format_command, init_miette, init_rayon, init_tracing,
CliRunResult, FormatRunner, format_command, init_miette, init_rayon, init_tracing,
};
use oxfmt::init::run_init;
use oxfmt::lsp::run_lsp;

// Pure Rust CLI entry point.
// For JS CLI entry point, see `format()` exported by `main_napi.rs`.
// This CLI only supports the basic `Cli` mode.
// For full featured JS CLI entry point, see `format()` exported by `main_napi.rs`.

#[tokio::main]
async fn main() -> CliRunResult {
// Parse command line arguments from std::env::args()
let command = format_command().run();

match command.mode {
Mode::Init => run_init(),
Mode::Lsp => {
run_lsp().await;
CliRunResult::None
}
Mode::Cli(_) => {
init_tracing();
init_miette();
init_rayon(command.runtime_options.threads);
FormatRunner::new(command).run()
}
}
init_tracing();
init_miette();
init_rayon(command.runtime_options.threads);
FormatRunner::new(command).run()
}
2 changes: 1 addition & 1 deletion tasks/website_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ doctest = false
[dependencies]
bpaf = { workspace = true, features = ["docgen"] }
oxc_formatter = { path = "../../crates/oxc_formatter" }
oxfmt = { path = "../../apps/oxfmt", default-features = false }
oxfmt = { path = "../../apps/oxfmt" }
pico-args = { workspace = true }
schemars = { workspace = true }
serde_json = { workspace = true }
Expand Down
Loading