Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Bueno.fs.copyFile("./examples/origin.txt", "./examples/dest.txt");

// Bueno.fs.copyDirectory("./examples/testOrigin/folder1", "./examples/testDest/folder1");

Bueno.fs.moveFolder("./examples/folder1/folder2", "./examples/destFolder/innerDestFolder/renamedFolder");
2 changes: 2 additions & 0 deletions ext/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ path = "lib.rs"
[dependencies]
deno_core.workspace = true
tokio.workspace = true

async-recursion = "1.0.5"
63 changes: 63 additions & 0 deletions ext/fs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,66 @@ 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> {
op_copy_dir_recurse(origin, dest).await?;
Ok(())
}

use async_recursion::async_recursion;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep imports at the beggining of the file

#[async_recursion]
pub async fn op_copy_dir_recurse(origin: String, dest: String) -> Result<(), AnyError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not really an op, prefix functions with op_ only when they use #[op]/#[op2] macros


// 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();

// 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? {

// Take name of file and add to destination path
let wholePathStr = entry.path().into_os_string().into_string().unwrap().clone();
let parts = wholePathStr.split("/");
let itemStr = parts.last().unwrap();
let mut next = dest.to_owned();
next.push_str(&"/");
next.push_str(&itemStr);

// 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?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please re-use wholePathStr here

}

// If file, copy file over to destination
} else {
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(())
}

#[op2(async)]
pub async fn op_move_folder(#[string] origin: String, #[string] dest: String) -> Result<(), AnyError> {
tokio::fs::rename(origin, dest).await?;
Ok(())
}
Comment on lines +106 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be one op

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, this could be just one op

44 changes: 44 additions & 0 deletions ext/fs/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,55 @@ function removeDirectory(path, recursive) {
return core.ops.op_remove_dir(path, recursive);
}

/**
* Copies file asynchronously
* @param {string} origin
* @param {string} dest
* @returns {Promise<void>}
*/
function copyFile(origin, dest) {
return core.ops.op_copy_file(origin, dest);
}

/**
* Copies folder asynchronously
* @param {string} origin
* @param {string} dest
* @returns {Promise<void>}
*/
function copyDirectory(origin, dest) {
return core.ops.op_copy_dir(origin, dest);
}

/**
* Moves file asyncrhonously
* @param {string} origin
* @param {string} dest
* @returns {Promise<void>}
*/
function moveFile(origin, dest) {
return core.ops.op_move_file(origin, dest);
}

/**
* Moves folder asyncrhonously
* @param {string} origin
* @param {string} dest
* @returns {Promise<void>}
*/
function moveFolder(origin, dest) {
return core.ops.op_move_folder(origin, dest);
}

Comment on lines +80 to +99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Im-Beast given that these basically call the same op, should this just be move?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it doesn't make sense to have two different functions which do exactly the same thing.
I also wonder if rename is not a better name, since that's how std::fs method is named

Bueno.fs = {
readFile,
readTextFile,
writeFile,
writeTextFile,
removeFile,
removeDirectory,
copyFile,
copyDirectory,
moveFile,
moveFolder,
};
4 changes: 4 additions & 0 deletions ext/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub mod extensions {
fs::op_write_text_file,
fs::op_remove_file,
fs::op_remove_dir,
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,
Expand Down