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
3 changes: 3 additions & 0 deletions apps/oxfmt/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ impl ConfigResolver {
.expect("`build_and_validate()` must be called before `resolve()`")
};

#[cfg(feature = "napi")]
let OxfmtOptions { sort_package_json, insert_final_newline, .. } = oxfmt_options;
#[cfg(not(feature = "napi"))]
let OxfmtOptions { insert_final_newline, .. } = oxfmt_options;

match strategy {
FormatFileStrategy::OxcFormatter { .. } => ResolvedOptions::OxcFormatter {
Expand Down
4 changes: 1 addition & 3 deletions apps/oxfmt/src/main_napi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ pub async fn run_cli(
Mode::Stdin(_) => {
init_miette();

// TODO: `.with_external_formatter()` is not needed, just pass with `new(command, external_formatter)`
let result =
StdinRunner::new(command).with_external_formatter(Some(external_formatter)).run();
let result = StdinRunner::new(command, external_formatter).run();

("stdin".to_string(), Some(result.exit_code()))
}
Expand Down
24 changes: 6 additions & 18 deletions apps/oxfmt/src/stdin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,22 @@ use crate::core::{
pub struct StdinRunner {
options: FormatCommand,
cwd: PathBuf,
external_formatter: Option<ExternalFormatter>,
external_formatter: ExternalFormatter,
}

impl StdinRunner {
/// Creates a new StdinRunner instance.
///
/// # Panics
/// Panics if the current working directory cannot be determined.
pub fn new(options: FormatCommand) -> Self {
pub fn new(options: FormatCommand, external_formatter: ExternalFormatter) -> Self {
Self {
options,
cwd: env::current_dir().expect("Failed to get current working directory"),
external_formatter: None,
external_formatter,
}
}

#[must_use]
pub fn with_external_formatter(
mut self,
external_formatter: Option<ExternalFormatter>,
) -> Self {
self.external_formatter = external_formatter;
self
}

pub fn run(self) -> CliRunResult {
let stdout = &mut BufWriter::new(io::stdout());
let stderr = &mut BufWriter::new(io::stderr());
Expand All @@ -49,9 +40,6 @@ impl StdinRunner {
let Mode::Stdin(filepath) = mode else {
unreachable!("`StdinRunner::run()` called with non-Stdin mode");
};
let Some(external_formatter) = self.external_formatter else {
unreachable!("`StdinRunner::run()` called without `external_formatter`");
};
// Single threaded for stdin formatting
let num_of_threads = 1;

Expand Down Expand Up @@ -88,7 +76,7 @@ impl StdinRunner {
}

// Use `block_in_place()` to avoid nested async runtime access
match tokio::task::block_in_place(|| external_formatter.init(num_of_threads)) {
match tokio::task::block_in_place(|| self.external_formatter.init(num_of_threads)) {
// TODO: Plugins support
Ok(_) => {}
Err(err) => {
Expand All @@ -110,8 +98,8 @@ impl StdinRunner {
let resolved_options = config_resolver.resolve(&strategy);

// Create formatter and format
let source_formatter =
SourceFormatter::new(num_of_threads).with_external_formatter(Some(external_formatter));
let source_formatter = SourceFormatter::new(num_of_threads)
.with_external_formatter(Some(self.external_formatter));

// Use `block_in_place()` to avoid nested async runtime access
match tokio::task::block_in_place(|| {
Expand Down
Loading