Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tailcall-prettier #1731

Merged
merged 15 commits into from
Apr 16, 2024
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ tonic-types = "0.11.0"


[dev-dependencies]
tailcall-prettier = {path = "tailcall-prettier"}
criterion = "0.5.1"
httpmock = "0.7.0"
pretty_assertions = "1.4.0"
Expand Down Expand Up @@ -195,7 +196,7 @@ members = [
".",
"tailcall-autogen",
"tailcall-aws-lambda",
"tailcall-cloudflare",
"tailcall-cloudflare", "tailcall-prettier",
"tailcall-query-plan",
]

Expand Down
12 changes: 12 additions & 0 deletions tailcall-prettier/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "tailcall-prettier"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.82"

[dev-dependencies]
insta = "1.38.0"
80 changes: 80 additions & 0 deletions tailcall-prettier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::fmt::{Display, Formatter};
use std::io::Write;
use std::process::{Command, Stdio};

use anyhow::{anyhow, Result};

enum FileTypes {
ssddOnTop marked this conversation as resolved.
Show resolved Hide resolved
Gql,
Yml,
Json,
Md,
Ts,
Js,
}

impl Display for FileTypes {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
FileTypes::Gql => write!(f, "graphql"),
FileTypes::Yml => write!(f, "yml"),
FileTypes::Json => write!(f, "json"),
FileTypes::Md => write!(f, "md"),
FileTypes::Ts => write!(f, "ts"),
FileTypes::Js => write!(f, "js"),
}
}
}
ssddOnTop marked this conversation as resolved.
Show resolved Hide resolved

impl FileTypes {
fn detect(path: &str) -> Result<Self> {
let ext = path
.split('.')
.last()
.ok_or(anyhow!("No file extension found"))?
.to_lowercase();
match ext.as_str() {
"gql" | "graphql" => Ok(FileTypes::Gql),
"yml" | "yaml" => Ok(FileTypes::Yml),
"json" => Ok(FileTypes::Json),
"md" => Ok(FileTypes::Md),
"ts" => Ok(FileTypes::Ts),
"js" => Ok(FileTypes::Js),
_ => Err(anyhow!("Unsupported file type")),
}
}
}

pub fn format_with_prettier<T: AsRef<str>>(code: T, file_ty: T) -> Result<String> {
ssddOnTop marked this conversation as resolved.
Show resolved Hide resolved
let file_ty = FileTypes::detect(file_ty.as_ref())?;

let mut child = Command::new("prettier")
tusharmath marked this conversation as resolved.
Show resolved Hide resolved
.arg("--stdin-filepath")
.arg(format!("file.{}", file_ty))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;

if let Some(ref mut stdin) = child.stdin {
stdin.write_all(code.as_ref().as_bytes())?;
}

let output = child.wait_with_output()?;
if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(anyhow!("Prettier formatting failed"))
}
}

#[cfg(test)]
mod tests {
use crate::format_with_prettier;

#[test]
fn test_js() -> anyhow::Result<()> {
let prettier = format_with_prettier("const x={a:3};", "file.ts")?;
insta::assert_snapshot!(prettier);
ssddOnTop marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tailcall-prettier/src/lib.rs
expression: prettier
---
const x = {a: 3}
14 changes: 13 additions & 1 deletion tests/execution_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,21 @@ async fn assert_spec(spec: ExecutionSpec, opentelemetry: &InMemoryTelemetry) {
// \r is added automatically in windows, it's safe to replace it with \n
let content = content.replace("\r\n", "\n");

let identity = tailcall_prettier::format_with_prettier(
identity,
spec.path.display().to_string(),
)
.unwrap();

let content = tailcall_prettier::format_with_prettier(
content,
spec.path.display().to_string(),
)
.unwrap();

pretty_assertions::assert_eq!(
identity,
content.as_ref(),
content,
ssddOnTop marked this conversation as resolved.
Show resolved Hide resolved
"Identity check failed for {:#?}",
spec.path,
);
Expand Down
Loading