diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs index d6855d100..92d24e98d 100644 --- a/crates/typst-cli/src/args.rs +++ b/crates/typst-cli/src/args.rs @@ -224,6 +224,10 @@ pub struct CompileArgs { #[arg(long = "format", short = 'f')] pub format: Option, + /// Directory to write output to. + #[clap(long = "output-path", env = "TYPST_OUTPUT_PATH", value_name = "DIR")] + pub output_path: Option, + /// World arguments. #[clap(flatten)] pub world: WorldArgs, diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 2b6a7d820..19902ea0a 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -112,7 +112,7 @@ impl CompileConfig { OutputFormat::Pdf }; - let output = args.output.clone().unwrap_or_else(|| { + let mut output = args.output.clone().unwrap_or_else(|| { let Input::Path(path) = &input else { panic!("output must be specified when input is from stdin, as guarded by the CLI"); }; @@ -126,6 +126,19 @@ impl CompileConfig { )) }); + if let (Some(output_path), Output::Path(output_file_name)) = + (args.output_path.as_ref(), &mut output) + { + if !output_file_name.is_absolute() { + if !output_path.exists() { + fs::create_dir_all(output_path) + .expect("could not create output path: {err}"); + } + *output_file_name = + output_path.join(&output_file_name.file_name().unwrap()); + } + } + let pages = args.pages.as_ref().map(|export_ranges| { PageRanges::new(export_ranges.iter().map(|r| r.0.clone()).collect()) });