Skip to content

Commit d894a54

Browse files
committed
Allow hyphen for input of CLI
1 parent 37a35cb commit d894a54

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

crates/cli/src/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ pub struct BuildCommandOpts {
8383
/// Path of the JavaScript input file.
8484
pub input: PathBuf,
8585

86-
#[arg(short, default_value = "index.wasm")]
86+
#[arg(short)]
8787
/// Desired path of the WebAssembly output file.
88-
pub output: PathBuf,
88+
pub output: Option<PathBuf>,
8989

9090
#[arg(short = 'C', long = "codegen")]
9191
/// Code generation options.

crates/cli/src/js.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct JS {
3232
}
3333

3434
impl JS {
35-
fn from_string(source_code: String) -> JS {
35+
pub fn from_string(source_code: String) -> JS {
3636
JS {
3737
source_code: Rc::new(source_code),
3838
}

crates/cli/src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use javy_config::Config;
1515
use js::JS;
1616
use std::fs;
1717
use std::fs::File;
18-
use std::io::Write;
18+
use std::io::{Read, Write};
1919

2020
fn main() -> Result<()> {
2121
let args = Cli::parse();
@@ -58,7 +58,14 @@ fn main() -> Result<()> {
5858
Ok(())
5959
}
6060
Command::Build(opts) => {
61-
let js = JS::from_file(&opts.input)?;
61+
let js = match opts.input.to_str() {
62+
Some("-") => {
63+
let mut content = String::new();
64+
std::io::stdin().read_to_string(&mut content)?;
65+
JS::from_string(content)
66+
}
67+
_ => JS::from_file(&opts.input)?,
68+
};
6269
let codegen: CodegenOptionGroup = opts.codegen.clone().try_into()?;
6370
let mut builder = CodeGenBuilder::new();
6471
builder
@@ -74,8 +81,11 @@ fn main() -> Result<()> {
7481
};
7582

7683
let wasm = gen.generate(&js)?;
77-
78-
fs::write(&opts.output, wasm)?;
84+
if let Some(path) = opts.output.as_ref() {
85+
fs::write(path, wasm)?;
86+
} else {
87+
std::io::stdout().write_all(&wasm)?;
88+
}
7989
Ok(())
8090
}
8191
}

0 commit comments

Comments
 (0)