|
1 | 1 | use anyhow::Context;
|
2 | 2 | use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions};
|
3 |
| -use std::{ffi::OsStr, path::Path}; |
| 3 | +use std::path::Path; |
4 | 4 |
|
5 | 5 | use crate::css::process_scss;
|
6 | 6 |
|
@@ -40,64 +40,67 @@ pub(crate) fn process_file_to_with_options(
|
40 | 40 | // Processing can be slow. Write to a temporary file first and then rename it to the final output path. If everything
|
41 | 41 | // goes well. Without this, the user could quit in the middle of processing and the file will look complete to the
|
42 | 42 | // caching system even though it is empty.
|
43 |
| - let mut partial_ext = output_path.extension().unwrap_or_default().to_os_string(); |
44 |
| - partial_ext.push(OsStr::new(".partial")); |
45 |
| - let temp_output_path = output_path.with_extension(&partial_ext); |
46 |
| - let temp_output_path = &temp_output_path; |
| 43 | + let temp_path = output_path.with_file_name(format!( |
| 44 | + "partial.{}", |
| 45 | + output_path |
| 46 | + .file_name() |
| 47 | + .unwrap_or_default() |
| 48 | + .to_string_lossy() |
| 49 | + )); |
47 | 50 |
|
48 | 51 | match options {
|
49 | 52 | AssetOptions::Unknown => match source.extension().map(|e| e.to_string_lossy()).as_deref() {
|
50 | 53 | Some("css") => {
|
51 |
| - process_css(&CssAssetOptions::new(), source, temp_output_path)?; |
| 54 | + process_css(&CssAssetOptions::new(), source, &temp_path)?; |
52 | 55 | }
|
53 | 56 | Some("scss" | "sass") => {
|
54 |
| - process_scss(&CssAssetOptions::new(), source, temp_output_path)?; |
| 57 | + process_scss(&CssAssetOptions::new(), source, &temp_path)?; |
55 | 58 | }
|
56 | 59 | Some("js") => {
|
57 |
| - process_js(&JsAssetOptions::new(), source, temp_output_path, !in_folder)?; |
| 60 | + process_js(&JsAssetOptions::new(), source, &temp_path, !in_folder)?; |
58 | 61 | }
|
59 | 62 | Some("json") => {
|
60 |
| - process_json(source, temp_output_path)?; |
| 63 | + process_json(source, &temp_path)?; |
61 | 64 | }
|
62 | 65 | Some("jpg" | "jpeg" | "png" | "webp" | "avif") => {
|
63 |
| - process_image(&ImageAssetOptions::new(), source, temp_output_path)?; |
| 66 | + process_image(&ImageAssetOptions::new(), source, &temp_path)?; |
64 | 67 | }
|
65 | 68 | Some(_) | None => {
|
66 | 69 | if source.is_dir() {
|
67 |
| - process_folder(source, temp_output_path)?; |
| 70 | + process_folder(source, &temp_path)?; |
68 | 71 | } else {
|
69 | 72 | let source_file = std::fs::File::open(source)?;
|
70 | 73 | let mut reader = std::io::BufReader::new(source_file);
|
71 |
| - let output_file = std::fs::File::create(temp_output_path)?; |
| 74 | + let output_file = std::fs::File::create(&temp_path)?; |
72 | 75 | let mut writer = std::io::BufWriter::new(output_file);
|
73 | 76 | std::io::copy(&mut reader, &mut writer).with_context(|| {
|
74 | 77 | format!(
|
75 | 78 | "Failed to write file to output location: {}",
|
76 |
| - temp_output_path.display() |
| 79 | + temp_path.display() |
77 | 80 | )
|
78 | 81 | })?;
|
79 | 82 | }
|
80 | 83 | }
|
81 | 84 | },
|
82 | 85 | AssetOptions::Css(options) => {
|
83 |
| - process_css(options, source, temp_output_path)?; |
| 86 | + process_css(options, source, &temp_path)?; |
84 | 87 | }
|
85 | 88 | AssetOptions::Js(options) => {
|
86 |
| - process_js(options, source, temp_output_path, !in_folder)?; |
| 89 | + process_js(options, source, &temp_path, !in_folder)?; |
87 | 90 | }
|
88 | 91 | AssetOptions::Image(options) => {
|
89 |
| - process_image(options, source, temp_output_path)?; |
| 92 | + process_image(options, source, &temp_path)?; |
90 | 93 | }
|
91 | 94 | AssetOptions::Folder(_) => {
|
92 |
| - process_folder(source, temp_output_path)?; |
| 95 | + process_folder(source, &temp_path)?; |
93 | 96 | }
|
94 | 97 | _ => {
|
95 | 98 | tracing::warn!("Unknown asset options: {:?}", options);
|
96 | 99 | }
|
97 | 100 | }
|
98 | 101 |
|
99 | 102 | // If everything was successful, rename the temp file to the final output path
|
100 |
| - std::fs::rename(temp_output_path, output_path)?; |
| 103 | + std::fs::rename(temp_path, output_path)?; |
101 | 104 |
|
102 | 105 | Ok(())
|
103 | 106 | }
|
0 commit comments