From d894a548425cea28a7f75d5aea307f2b8bd8b381 Mon Sep 17 00:00:00 2001 From: Konrad Koschel Date: Wed, 25 Sep 2024 09:29:13 +0200 Subject: [PATCH] Allow hyphen for input of CLI --- crates/cli/src/commands.rs | 4 ++-- crates/cli/src/js.rs | 2 +- crates/cli/src/main.rs | 18 ++++++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/cli/src/commands.rs b/crates/cli/src/commands.rs index c1e57a57..67e1cae4 100644 --- a/crates/cli/src/commands.rs +++ b/crates/cli/src/commands.rs @@ -83,9 +83,9 @@ pub struct BuildCommandOpts { /// Path of the JavaScript input file. pub input: PathBuf, - #[arg(short, default_value = "index.wasm")] + #[arg(short)] /// Desired path of the WebAssembly output file. - pub output: PathBuf, + pub output: Option, #[arg(short = 'C', long = "codegen")] /// Code generation options. diff --git a/crates/cli/src/js.rs b/crates/cli/src/js.rs index 9d08732c..6ec0f271 100644 --- a/crates/cli/src/js.rs +++ b/crates/cli/src/js.rs @@ -32,7 +32,7 @@ pub struct JS { } impl JS { - fn from_string(source_code: String) -> JS { + pub fn from_string(source_code: String) -> JS { JS { source_code: Rc::new(source_code), } diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 011606ac..98fcceac 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -15,7 +15,7 @@ use javy_config::Config; use js::JS; use std::fs; use std::fs::File; -use std::io::Write; +use std::io::{Read, Write}; fn main() -> Result<()> { let args = Cli::parse(); @@ -58,7 +58,14 @@ fn main() -> Result<()> { Ok(()) } Command::Build(opts) => { - let js = JS::from_file(&opts.input)?; + let js = match opts.input.to_str() { + Some("-") => { + let mut content = String::new(); + std::io::stdin().read_to_string(&mut content)?; + JS::from_string(content) + } + _ => JS::from_file(&opts.input)?, + }; let codegen: CodegenOptionGroup = opts.codegen.clone().try_into()?; let mut builder = CodeGenBuilder::new(); builder @@ -74,8 +81,11 @@ fn main() -> Result<()> { }; let wasm = gen.generate(&js)?; - - fs::write(&opts.output, wasm)?; + if let Some(path) = opts.output.as_ref() { + fs::write(path, wasm)?; + } else { + std::io::stdout().write_all(&wasm)?; + } Ok(()) } }