From 9002dd239c6aaa24b687fe3ef03752212cb80f8d Mon Sep 17 00:00:00 2001 From: Bamboozle-jpg Date: Mon, 9 Oct 2023 10:46:16 -0700 Subject: [PATCH 1/6] new package, addd copy_dir fs method --- examples/files.ts | 3 +++ ext/fs/Cargo.toml | 2 ++ ext/fs/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ ext/fs/mod.js | 22 ++++++++++++++++++++++ ext/lib.rs | 2 ++ 5 files changed, 76 insertions(+) create mode 100644 examples/files.ts diff --git a/examples/files.ts b/examples/files.ts new file mode 100644 index 0000000..38638e1 --- /dev/null +++ b/examples/files.ts @@ -0,0 +1,3 @@ +Bueno.fs.copyFile("./examples/origin.txt", "./examples/dest.txt"); + +Bueno.fs.copyDirectory("./examples/testOrigin/folder1", "./examples/testDest/folder1") \ No newline at end of file diff --git a/ext/fs/Cargo.toml b/ext/fs/Cargo.toml index 1c2bc29..cbd0b19 100644 --- a/ext/fs/Cargo.toml +++ b/ext/fs/Cargo.toml @@ -13,3 +13,5 @@ path = "lib.rs" [dependencies] deno_core.workspace = true tokio.workspace = true + +async-recursion = "1.0.5" diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index cd31ec5..62d28c0 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -51,3 +51,50 @@ pub async fn op_remove_dir(#[string] path: String, recursive: bool) -> Result<() Ok(()) } + +#[op2(async)] +pub async fn op_copy_file(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> { + tokio::fs::copy(origin, dest).await?; + Ok(()) +} + +#[op2(async)] +pub async fn op_copy_dir(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> { + print!("got here"); + op_copy_dir_recurse(origin, dest).await?; + Ok(()) +} + +use async_recursion::async_recursion; +#[async_recursion] +pub async fn op_copy_dir_recurse(origin: String, dest: String) -> Result<(), AnyError> { + let orgForMetadata = origin.clone(); + let attr = tokio::fs::metadata(orgForMetadata); + let dirBool = attr.await?.is_dir(); + print!("attributes : {:?}\n", dirBool); + if dirBool { + + let destForCreate = dest.clone(); + tokio::fs::create_dir(destForCreate).await?; + + let mut entries = tokio::fs::read_dir(origin).await?; + while let Some(entry) = entries.next_entry().await? { + + let mut next = dest.to_owned(); + + let wholePathStr = entry.path().into_os_string().into_string().unwrap().clone(); + print!("entry path : {}\n", entry.path().into_os_string().into_string().unwrap()); + let parts = wholePathStr.split("/"); + let itemStr = parts.last().unwrap(); + + print!("item = {}\n", itemStr); + next.push_str(&"/"); + next.push_str(&itemStr); + print!("next : {}\n", next); + op_copy_dir_recurse(entry.path().into_os_string().into_string().unwrap(), next).await?; + } + } else { + tokio::fs::copy(origin, dest).await?; + } + Ok(()) +} \ No newline at end of file diff --git a/ext/fs/mod.js b/ext/fs/mod.js index 068c081..994ddf6 100644 --- a/ext/fs/mod.js +++ b/ext/fs/mod.js @@ -57,6 +57,26 @@ function removeDirectory(path, recursive) { return core.ops.op_remove_dir(path, recursive); } +/** + * Copies file asynchronously + * @param {string} origin + * @param {string} dest + * @returns {Promise} + */ +function copyFile(origin, dest) { + return core.ops.op_copy_file(origin, dest); +} + +/** + * Copies folder asynchronously + * @param {string} origin + * @param {string} dest + * @returns {Promise} + */ +function copyDirectory(origin, dest) { + return core.ops.op_copy_dir(origin, dest); +} + Bueno.fs = { readFile, readTextFile, @@ -64,4 +84,6 @@ Bueno.fs = { writeTextFile, removeFile, removeDirectory, + copyFile, + copyDirectory, }; diff --git a/ext/lib.rs b/ext/lib.rs index 07c2fea..d45e78f 100644 --- a/ext/lib.rs +++ b/ext/lib.rs @@ -23,6 +23,8 @@ pub mod extensions { fs::op_write_text_file, fs::op_remove_file, fs::op_remove_dir, + fs::op_copy_file, + fs::op_copy_dir, performance::op_high_res_time, performance::op_time_origin, timers::op_timers_sleep, From cf474a7243225ea5652c0256bac8ca6d52cc90c1 Mon Sep 17 00:00:00 2001 From: Bamboozle-jpg Date: Mon, 9 Oct 2023 10:51:03 -0700 Subject: [PATCH 2/6] add copyDir comments and remove prints --- ext/fs/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 62d28c0..b384c71 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -68,31 +68,36 @@ pub async fn op_copy_dir(#[string] origin: String, #[string] dest: String) -> Re use async_recursion::async_recursion; #[async_recursion] pub async fn op_copy_dir_recurse(origin: String, dest: String) -> Result<(), AnyError> { + + // Setup for viewing directory contents and checking if dir let orgForMetadata = origin.clone(); let attr = tokio::fs::metadata(orgForMetadata); let dirBool = attr.await?.is_dir(); - print!("attributes : {:?}\n", dirBool); + + // if directory, make a directory at destination and loop through contents if dirBool { + // Make a new directory at destination let destForCreate = dest.clone(); tokio::fs::create_dir(destForCreate).await?; + // Loop through all contents of folder let mut entries = tokio::fs::read_dir(origin).await?; while let Some(entry) = entries.next_entry().await? { - let mut next = dest.to_owned(); - + // Take name of file and add to destination path let wholePathStr = entry.path().into_os_string().into_string().unwrap().clone(); - print!("entry path : {}\n", entry.path().into_os_string().into_string().unwrap()); let parts = wholePathStr.split("/"); let itemStr = parts.last().unwrap(); - - print!("item = {}\n", itemStr); + let mut next = dest.to_owned(); next.push_str(&"/"); next.push_str(&itemStr); - print!("next : {}\n", next); + + // Recursive call to copy over contents of folder being looked at op_copy_dir_recurse(entry.path().into_os_string().into_string().unwrap(), next).await?; } + + // If file, copy file over to destination } else { tokio::fs::copy(origin, dest).await?; } From f0e68222c9c2e193e4bd21a42eaf7fefd1ecf44c Mon Sep 17 00:00:00 2001 From: Bamboozle-jpg Date: Mon, 9 Oct 2023 10:51:41 -0700 Subject: [PATCH 3/6] removed extra print --- ext/fs/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index b384c71..1b8b4d7 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -60,7 +60,6 @@ pub async fn op_copy_file(#[string] origin: String, #[string] dest: String) -> R #[op2(async)] pub async fn op_copy_dir(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> { - print!("got here"); op_copy_dir_recurse(origin, dest).await?; Ok(()) } From c089acac0f091c5c960446206acdea8ac2e25500 Mon Sep 17 00:00:00 2001 From: Bamboozle-jpg Date: Mon, 9 Oct 2023 10:53:57 -0700 Subject: [PATCH 4/6] update cargo.lock --- Cargo.lock | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 8871911..b5deb1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,17 @@ dependencies = [ "syn 2.0.37", ] +[[package]] +name = "async-recursion" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd55a5ba1179988837d24ab4c7cc8ed6efdeff578ede0416b4225a5fca35bd0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -220,6 +231,7 @@ dependencies = [ name = "bueno_ext_fs" version = "0.0.1" dependencies = [ + "async-recursion", "deno_core", "tokio", ] From 826590d023de6180a425865cc67d0391cc01528a Mon Sep 17 00:00:00 2001 From: Bamboozle-jpg Date: Mon, 9 Oct 2023 11:25:33 -0700 Subject: [PATCH 5/6] move file implemented --- examples/files.ts | 6 ++++-- ext/fs/lib.rs | 6 ++++++ ext/fs/mod.js | 11 +++++++++++ ext/lib.rs | 1 + 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/examples/files.ts b/examples/files.ts index 38638e1..f755844 100644 --- a/examples/files.ts +++ b/examples/files.ts @@ -1,3 +1,5 @@ -Bueno.fs.copyFile("./examples/origin.txt", "./examples/dest.txt"); +// Bueno.fs.copyFile("./examples/origin.txt", "./examples/dest.txt"); -Bueno.fs.copyDirectory("./examples/testOrigin/folder1", "./examples/testDest/folder1") \ No newline at end of file +// Bueno.fs.copyDirectory("./examples/testOrigin/folder1", "./examples/testDest/folder1"); + +Bueno.fs.moveFile("./examples/folder1/folder2/test.txt", "./examples/destFolder/innerDestFolder/renamed.txt"); \ No newline at end of file diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 1b8b4d7..72c84f0 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -101,4 +101,10 @@ pub async fn op_copy_dir_recurse(origin: String, dest: String) -> Result<(), Any tokio::fs::copy(origin, dest).await?; } Ok(()) +} + +#[op2(async)] +pub async fn op_move_file(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> { + tokio::fs::rename(origin, dest).await?; + Ok(()) } \ No newline at end of file diff --git a/ext/fs/mod.js b/ext/fs/mod.js index 994ddf6..b808b57 100644 --- a/ext/fs/mod.js +++ b/ext/fs/mod.js @@ -77,6 +77,16 @@ function copyDirectory(origin, dest) { return core.ops.op_copy_dir(origin, dest); } +/** + * Moves file asyncrhonously + * @param {string} origin + * @param {string} dest + * @returns {Promise} + */ +function moveFile(origin, dest) { + return core.ops.op_move_file(origin, dest); +} + Bueno.fs = { readFile, readTextFile, @@ -86,4 +96,5 @@ Bueno.fs = { removeDirectory, copyFile, copyDirectory, + moveFile }; diff --git a/ext/lib.rs b/ext/lib.rs index d45e78f..826c407 100644 --- a/ext/lib.rs +++ b/ext/lib.rs @@ -25,6 +25,7 @@ pub mod extensions { fs::op_remove_dir, fs::op_copy_file, fs::op_copy_dir, + fs::op_move_file, performance::op_high_res_time, performance::op_time_origin, timers::op_timers_sleep, From 3a327d6b1a4de9492cc197c4f698923c13e400e5 Mon Sep 17 00:00:00 2001 From: Bamboozle-jpg Date: Mon, 9 Oct 2023 11:53:32 -0700 Subject: [PATCH 6/6] implemented move folder --- examples/files.ts | 2 +- ext/fs/lib.rs | 6 ++++++ ext/fs/mod.js | 13 ++++++++++++- ext/lib.rs | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/examples/files.ts b/examples/files.ts index f755844..1de8f2d 100644 --- a/examples/files.ts +++ b/examples/files.ts @@ -2,4 +2,4 @@ // Bueno.fs.copyDirectory("./examples/testOrigin/folder1", "./examples/testDest/folder1"); -Bueno.fs.moveFile("./examples/folder1/folder2/test.txt", "./examples/destFolder/innerDestFolder/renamed.txt"); \ No newline at end of file +Bueno.fs.moveFolder("./examples/folder1/folder2", "./examples/destFolder/innerDestFolder/renamedFolder"); \ No newline at end of file diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 72c84f0..55b0363 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -107,4 +107,10 @@ pub async fn op_copy_dir_recurse(origin: String, dest: String) -> Result<(), Any pub async fn op_move_file(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> { tokio::fs::rename(origin, dest).await?; Ok(()) +} + +#[op2(async)] +pub async fn op_move_folder(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> { + tokio::fs::rename(origin, dest).await?; + Ok(()) } \ No newline at end of file diff --git a/ext/fs/mod.js b/ext/fs/mod.js index b808b57..16a13cf 100644 --- a/ext/fs/mod.js +++ b/ext/fs/mod.js @@ -87,6 +87,16 @@ function moveFile(origin, dest) { return core.ops.op_move_file(origin, dest); } +/** + * Moves folder asyncrhonously + * @param {string} origin + * @param {string} dest + * @returns {Promise} + */ +function moveFolder(origin, dest) { + return core.ops.op_move_folder(origin, dest); +} + Bueno.fs = { readFile, readTextFile, @@ -96,5 +106,6 @@ Bueno.fs = { removeDirectory, copyFile, copyDirectory, - moveFile + moveFile, + moveFolder, }; diff --git a/ext/lib.rs b/ext/lib.rs index 826c407..b2dc574 100644 --- a/ext/lib.rs +++ b/ext/lib.rs @@ -26,6 +26,7 @@ pub mod extensions { fs::op_copy_file, fs::op_copy_dir, fs::op_move_file, + fs::op_move_folder, performance::op_high_res_time, performance::op_time_origin, timers::op_timers_sleep,