Skip to content

Commit a784d0d

Browse files
committed
Check if prettier is installed
This adds a small amount of startup time. But I expect the net effects to be positive.
1 parent 746db6b commit a784d0d

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/main.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919
io,
2020
ops::Range,
2121
path::Path,
22-
process::{Child, Command, Stdio, exit},
22+
process::{Child, Command, ExitStatus, Stdio, exit},
2323
sync::{
2424
Condvar, LazyLock, Mutex, MutexGuard,
2525
atomic::{AtomicBool, Ordering},
@@ -100,6 +100,8 @@ fn main() -> Result<()> {
100100
opts.max_width = rustfmt_max_width()?;
101101
}
102102

103+
check_if_prettier_is_installed().with_context(|| "failed to run `prettier`")?;
104+
103105
let mut backups = Vec::new();
104106
let mut handles = Vec::new();
105107
// smoelius: Split off `opts.patterns` so that its contents are not cloned before each call to
@@ -216,6 +218,22 @@ fn rustfmt_max_width() -> Result<Option<usize>> {
216218
Ok(Some(max_width))
217219
}
218220

221+
fn check_if_prettier_is_installed() -> Result<()> {
222+
match Command::new("prettier")
223+
.arg("-v")
224+
.stdout(Stdio::null())
225+
.stderr(Stdio::null())
226+
.status_wc()
227+
{
228+
Ok(status) if status.success() => Ok(()),
229+
Ok(status) => Err(anyhow!(
230+
"`prettier -v` exited {}",
231+
exit_status_to_string(status)
232+
)),
233+
Err(error) => Err(error),
234+
}
235+
}
236+
219237
fn format_file(opts: Options, path: impl AsRef<Path>) -> Result<()> {
220238
let check = opts.check;
221239
#[allow(clippy::disallowed_methods)]
@@ -391,11 +409,7 @@ fn format_chunk(receiver: &Receiver<Child>, chunk: &Chunk) -> Result<String> {
391409
ensure!(
392410
output.status.success(),
393411
"prettier exited {}",
394-
output
395-
.status
396-
.code_wc()
397-
.map(|code| format!("with code {code}"))
398-
.unwrap_or(String::from("abnormally"))
412+
exit_status_to_string(output.status)
399413
);
400414

401415
decrement_used_parallelism();
@@ -441,6 +455,13 @@ fn postprocess_docs(characteristics: Characteristics, docs: &str) -> String {
441455
.collect()
442456
}
443457

458+
fn exit_status_to_string(status: ExitStatus) -> String {
459+
status
460+
.code_wc()
461+
.map(|code| format!("with code {code}"))
462+
.unwrap_or(String::from("abnormally"))
463+
}
464+
444465
fn join_anyhow<T>(handle: thread::JoinHandle<Result<T>>) -> Result<T> {
445466
handle
446467
.join()

0 commit comments

Comments
 (0)