Skip to content

Commit

Permalink
Allow hyphen for input of CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
konradkoschel committed Sep 25, 2024
1 parent 37a35cb commit d894a54
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

#[arg(short = 'C', long = "codegen")]
/// Code generation options.
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
18 changes: 14 additions & 4 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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(())
}
}
Expand Down

0 comments on commit d894a54

Please sign in to comment.