summaryrefslogtreecommitdiff
path: root/cli/util/path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/util/path.rs')
-rw-r--r--cli/util/path.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/cli/util/path.rs b/cli/util/path.rs
index a3109ad04..3c848edea 100644
--- a/cli/util/path.rs
+++ b/cli/util/path.rs
@@ -42,6 +42,32 @@ pub fn get_extension(file_path: &Path) -> Option<String> {
.map(|e| e.to_lowercase());
}
+pub fn get_atomic_dir_path(file_path: &Path) -> PathBuf {
+ let rand = gen_rand_path_component();
+ let new_file_name = format!(
+ ".{}_{}",
+ file_path
+ .file_name()
+ .map(|f| f.to_string_lossy())
+ .unwrap_or(Cow::Borrowed("")),
+ rand
+ );
+ file_path.with_file_name(new_file_name)
+}
+
+pub fn get_atomic_file_path(file_path: &Path) -> PathBuf {
+ let rand = gen_rand_path_component();
+ let extension = format!("{rand}.tmp");
+ file_path.with_extension(extension)
+}
+
+fn gen_rand_path_component() -> String {
+ (0..4).fold(String::new(), |mut output, _| {
+ output.push_str(&format!("{:02x}", rand::random::<u8>()));
+ output
+ })
+}
+
/// TypeScript figures out the type of file based on the extension, but we take
/// other factors into account like the file headers. The hack here is to map the
/// specifier passed to TypeScript to a new specifier with the file extension.