-
Notifications
You must be signed in to change notification settings - Fork 5
Add file system functions #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9002dd2
cf474a7
f0e6822
c089aca
826590d
3a327d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,5 @@ path = "lib.rs" | |
| [dependencies] | ||
| deno_core.workspace = true | ||
| tokio.workspace = true | ||
|
|
||
| async-recursion = "1.0.5" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| #[async_recursion] | ||
| pub async fn op_copy_dir_recurse(origin: String, dest: String) -> Result<(), AnyError> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not really an |
||
|
|
||
| // 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?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please re-use |
||
| } | ||
|
|
||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be one op
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed, this could be just one op |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| Bueno.fs = { | ||
| readFile, | ||
| readTextFile, | ||
| writeFile, | ||
| writeTextFile, | ||
| removeFile, | ||
| removeDirectory, | ||
| copyFile, | ||
| copyDirectory, | ||
| moveFile, | ||
| moveFolder, | ||
| }; | ||
There was a problem hiding this comment.
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