-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add
tailcall-prettier
(#1731)
Co-authored-by: Tushar Mathur <[email protected]>
- Loading branch information
1 parent
66eb1bc
commit 69f22a6
Showing
8 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
lazy_static = "1.4.0" | ||
strum_macros = "0.26.2" | ||
tokio.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::sync::Arc; | ||
mod parser; | ||
mod prettier; | ||
use anyhow::Result; | ||
pub use parser::Parser; | ||
use prettier::Prettier; | ||
|
||
lazy_static::lazy_static! { | ||
static ref PRETTIER: Arc<Prettier> = Arc::new(Prettier::new()); | ||
} | ||
|
||
pub async fn format<T: AsRef<str>>(source: T, parser: Parser) -> Result<String> { | ||
PRETTIER.format(source.as_ref().to_string(), parser).await | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{format, Parser}; | ||
|
||
#[tokio::test] | ||
async fn test_js() -> anyhow::Result<()> { | ||
let prettier = format("const x={a:3};", Parser::Js).await?; | ||
assert_eq!("const x = {a: 3}\n", prettier); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use anyhow::{anyhow, Result}; | ||
|
||
#[derive(strum_macros::Display)] | ||
pub enum Parser { | ||
Gql, | ||
Yml, | ||
Json, | ||
Md, | ||
Ts, | ||
Js, | ||
} | ||
|
||
impl Parser { | ||
pub 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(Parser::Gql), | ||
"yml" | "yaml" => Ok(Parser::Yml), | ||
"json" => Ok(Parser::Json), | ||
"md" => Ok(Parser::Md), | ||
"ts" => Ok(Parser::Ts), | ||
"js" => Ok(Parser::Js), | ||
_ => Err(anyhow!("Unsupported file type")), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::io::Write; | ||
use std::process::{Command, Stdio}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
|
||
pub use super::Parser; | ||
|
||
pub struct Prettier { | ||
runtime: tokio::runtime::Runtime, | ||
} | ||
|
||
impl Prettier { | ||
pub fn new() -> Prettier { | ||
let runtime = tokio::runtime::Builder::new_multi_thread() | ||
.max_blocking_threads(1024) | ||
.build() | ||
.unwrap(); | ||
|
||
Self { runtime } | ||
} | ||
|
||
pub async fn format(&self, source: String, parser: Parser) -> Result<String> { | ||
self.runtime | ||
.spawn_blocking(move || { | ||
let mut command = command(); | ||
let mut child = command | ||
.arg("--stdin-filepath") | ||
.arg(format!("file.{}", parser)) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn()?; | ||
|
||
if let Some(ref mut stdin) = child.stdin { | ||
stdin.write_all(source.as_bytes())?; | ||
} | ||
|
||
let output = child.wait_with_output()?; | ||
if output.status.success() { | ||
Ok(String::from_utf8(output.stdout)?) | ||
} else { | ||
Err(anyhow!("Prettier formatting failed")) | ||
} | ||
}) | ||
.await? | ||
} | ||
} | ||
|
||
fn command() -> Command { | ||
if cfg!(target_os = "windows") { | ||
Command::new("prettier.cmd") | ||
} else { | ||
Command::new("prettier") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69f22a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running 30s test @ http://localhost:8000/graphql
4 threads and 100 connections
415183 requests in 30.02s, 2.08GB read
Requests/sec: 13832.17
Transfer/sec: 71.00MB