From fb749ca0adb4f6ea66e47e1017a210d34b42301a Mon Sep 17 00:00:00 2001 From: Lucas Weng Date: Tue, 21 Feb 2023 10:28:22 +0800 Subject: [PATCH] Support creating missing directories with `--output-file` option --- src/main.rs | 4 ++++ tests/cli_integration_tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main.rs b/src/main.rs index a1661fd0..363d3f08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -217,6 +217,10 @@ pub fn main() -> Result<(), std::io::Error> { } } + let output_path = Path::new(output_file); + if let Some(p) = output_path.parent() { + fs::create_dir_all(p)? + }; fs::write(output_file, code.as_bytes())?; if let Some(css_modules) = cli_args.css_modules { diff --git a/tests/cli_integration_tests.rs b/tests/cli_integration_tests.rs index 4fe1ef7a..cdb7af96 100644 --- a/tests/cli_integration_tests.rs +++ b/tests/cli_integration_tests.rs @@ -5,6 +5,7 @@ use indoc::indoc; use lightningcss::css_modules::CssModuleExport; use predicates::prelude::*; use std::collections::HashMap; +use std::fs; use std::process::Command; fn test_file() -> Result { @@ -182,6 +183,28 @@ fn output_file_option() -> Result<(), Box> { Ok(()) } +#[test] +fn output_file_option_create_missing_directories() -> Result<(), Box> { + let infile = test_file()?; + let outdir = assert_fs::TempDir::new()?; + let outfile = outdir.child("out.css"); + outdir.close()?; + let mut cmd = Command::cargo_bin("lightningcss")?; + cmd.arg(infile.path()); + cmd.arg("--output-file").arg(outfile.path()); + cmd.assert().success(); + outfile.assert(predicate::str::contains(indoc! { + r#" + .foo { + border: none; + } + "# + })); + fs::remove_dir_all(outfile.parent().unwrap())?; + + Ok(()) +} + #[test] fn minify_option() -> Result<(), Box> { let infile = test_file()?;