Skip to content

Commit

Permalink
feat(core): fix copying single files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Jun 16, 2023
1 parent 8790b71 commit b4f57e6
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/nx/src/native/cache/file_ops.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fs;
use std::path::{Path, PathBuf};

#[napi]
pub fn remove(src: String) -> anyhow::Result<()> {
fs_extra::remove_items(&[src]).map_err(anyhow::Error::from)
Expand All @@ -7,9 +10,16 @@ pub fn remove(src: String) -> anyhow::Result<()> {
pub fn copy(src: String, dest: String) -> anyhow::Result<()> {
let copy_options = fs_extra::dir::CopyOptions::new()
.overwrite(true)
.skip_exist(false)
.copy_inside(true);
fs_extra::copy_items(&[src], dest, &copy_options)?;
.skip_exist(false);

let dest: PathBuf = dest.into();
let dest_parent = dest.parent().unwrap_or(&dest);

if !dest_parent.exists() {
fs::create_dir_all(dest_parent)?;
}

fs_extra::copy_items(&[src], dest_parent, &copy_options)?;
Ok(())
}

Expand All @@ -20,7 +30,7 @@ mod test {
use assert_fs::TempDir;

#[test]
fn should_copy_files() {
fn should_copy_directories() {
let temp = TempDir::new().unwrap();
temp.child("parent")
.child("child")
Expand All @@ -37,4 +47,19 @@ mod test {
.child("new-parent/child/grand-child/.config/file.txt")
.exists());
}

#[test]
fn should_copy_single_files() {
let temp = TempDir::new().unwrap();
temp.child("parent")
.child("file.txt")
.write_str("content")
.unwrap();

let src = temp.join("parent/file.txt");
let dest = temp.join("new-parent/file.txt");
copy(src.to_string_lossy().into(), dest.to_string_lossy().into()).unwrap();

assert!(temp.child("new-parent/file.txt").exists());
}
}

0 comments on commit b4f57e6

Please sign in to comment.