-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial file system operations
Merge pull request #9 from buenojs/init-bueno-fs
- Loading branch information
Showing
10 changed files
with
288 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ path = "lib.rs" | |
|
||
[dependencies] | ||
deno_core.workspace = true | ||
bueno_ext_fs = { path = "./fs/" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "bueno_ext_fs" | ||
description = "Bueno file system extension" | ||
edition.workspace = true | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
|
||
[lib] | ||
path = "lib.rs" | ||
|
||
[dependencies] | ||
deno_core.workspace = true | ||
tokio.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use deno_core::{error::AnyError, op, op2}; | ||
|
||
// Read files | ||
#[op2(async)] | ||
#[buffer] | ||
pub async fn op_read_file(#[string] path: String) -> Result<Vec<u8>, AnyError> { | ||
let contents = tokio::fs::read(path).await?; | ||
Ok(contents) | ||
} | ||
|
||
#[op2] | ||
#[buffer] | ||
pub fn op_read_file_sync(#[string] path: String) -> Result<Vec<u8>, AnyError> { | ||
let contents = std::fs::read(path)?; | ||
Ok(contents) | ||
} | ||
|
||
// TODO: Convert it to op2 | ||
#[op] | ||
pub async fn op_read_text_file(path: String) -> Result<String, AnyError> { | ||
let contents = tokio::fs::read_to_string(path).await?; | ||
Ok(contents) | ||
} | ||
|
||
#[op2] | ||
#[string] | ||
pub fn op_read_text_file_sync(#[string] path: String) -> Result<String, AnyError> { | ||
let contents = std::fs::read_to_string(path)?; | ||
Ok(contents) | ||
} | ||
|
||
// Write files | ||
#[op2(async)] | ||
pub async fn op_write_file( | ||
#[string] path: String, | ||
#[buffer(copy)] contents: Vec<u8>, | ||
) -> Result<(), AnyError> { | ||
tokio::fs::write(path, contents).await?; | ||
Ok(()) | ||
} | ||
|
||
#[op2(fast)] | ||
pub fn op_write_file_sync( | ||
#[string] path: String, | ||
#[buffer(copy)] contents: Vec<u8>, | ||
) -> Result<(), AnyError> { | ||
std::fs::write(path, contents)?; | ||
Ok(()) | ||
} | ||
|
||
#[op2(async)] | ||
pub async fn op_write_text_file( | ||
#[string] path: String, | ||
#[string] contents: String, | ||
) -> Result<(), AnyError> { | ||
tokio::fs::write(path, contents).await?; | ||
Ok(()) | ||
} | ||
|
||
#[op2(fast)] | ||
pub fn op_write_text_file_sync( | ||
#[string] path: String, | ||
#[string] contents: String, | ||
) -> Result<(), AnyError> { | ||
std::fs::write(path, contents)?; | ||
Ok(()) | ||
} | ||
|
||
// Remove files | ||
#[op2(async)] | ||
pub async fn op_remove_file(#[string] path: String) -> Result<(), AnyError> { | ||
tokio::fs::remove_file(path).await?; | ||
Ok(()) | ||
} | ||
|
||
#[op2(fast)] | ||
pub fn op_remove_file_sync(#[string] path: String) -> Result<(), AnyError> { | ||
std::fs::remove_file(path)?; | ||
Ok(()) | ||
} | ||
|
||
#[op2(async)] | ||
pub async fn op_remove_dir(#[string] path: String, recursive: bool) -> Result<(), AnyError> { | ||
if recursive { | ||
tokio::fs::remove_dir_all(path).await?; | ||
} else { | ||
tokio::fs::remove_dir(path).await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[op2(fast)] | ||
pub fn op_remove_dir_sync(#[string] path: String, recursive: bool) -> Result<(), AnyError> { | ||
if recursive { | ||
std::fs::remove_dir_all(path)?; | ||
} else { | ||
std::fs::remove_dir(path)?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const core = Bueno.core; | ||
|
||
/** | ||
* Reads a file asynchronously | ||
* @param {string} path | ||
* @returns {Promise<Uint8Array>} | ||
*/ | ||
function readFile(path) { | ||
return core.ops.op_read_file(path); | ||
} | ||
|
||
/** | ||
* Reads a file synchronously | ||
* @param {string} path | ||
* @returns {Uint8Array} | ||
*/ | ||
function readFileSync(path) { | ||
return core.ops.op_read_file_sync(path); | ||
} | ||
|
||
/** | ||
* Reads a text file synchronously | ||
* @param {string} path | ||
* @returns {Promise<string>} | ||
*/ | ||
function readTextFile(path) { | ||
return core.ops.op_read_text_file(path); | ||
} | ||
|
||
/** | ||
* Reads a text file synchronously | ||
* @param {string} path | ||
* @returns {string} | ||
*/ | ||
function readTextFileSync(path) { | ||
return core.ops.op_read_text_file_sync(path); | ||
} | ||
|
||
/** | ||
* Writes a file asynchronously | ||
* @param {string} path | ||
* @param {Uint8Array} contents | ||
* @returns {Promise<void>} | ||
*/ | ||
function writeFile(path, contents) { | ||
core.ops.op_write_file(path, contents); | ||
} | ||
|
||
/** | ||
* Writes a file synchronously | ||
* @param {string} path | ||
* @param {Uint8Array} contents | ||
* @returns {void} | ||
*/ | ||
function writeFileSync(path, contents) { | ||
core.ops.op_write_file_sync(path, contents); | ||
} | ||
|
||
/** | ||
* Writes a text file asynchronously | ||
* @param {string} path | ||
* @param {string} contents | ||
* @returns {Promise<void>} | ||
*/ | ||
function writeTextFile(path, contents) { | ||
core.ops.op_write_text_file(path, contents); | ||
} | ||
|
||
/** | ||
* Writes a text file synchronously | ||
* @param {string} path | ||
* @param {string} contents | ||
* @returns {void} | ||
*/ | ||
function writeTextFileSync(path, contents) { | ||
core.ops.op_write_text_file_sync(path, contents); | ||
} | ||
|
||
/** | ||
* Deletes file asynchronously | ||
* @param {string} path | ||
* @returns {Promise<void>} | ||
*/ | ||
function removeFile(path) { | ||
core.ops.op_remove_file(path); | ||
} | ||
|
||
/** | ||
* Deletes file synchronously | ||
* @param {string} path | ||
* @returns {void} | ||
*/ | ||
function removeFileSync(path) { | ||
core.ops.op_remove_file_sync(path); | ||
} | ||
|
||
/** | ||
* Deletes directory asynchronously | ||
* @param {string} path | ||
* @param {boolean} recursive | ||
* @returns {Promise<void>} | ||
*/ | ||
function removeDirectory(path, recursive) { | ||
core.ops.op_remove_dir(path, recursive); | ||
} | ||
|
||
/** | ||
* Deletes directory synchronously | ||
* @param {string} path | ||
* @param {boolean} recursive | ||
* @returns {void} | ||
*/ | ||
function removeDirectorySync(path, recursive) { | ||
core.ops.op_remove_dir_sync(path, recursive); | ||
} | ||
|
||
Bueno.fs = { | ||
readFile, | ||
readFileSync, | ||
readTextFile, | ||
readTextFileSync, | ||
writeFile, | ||
writeFileSync, | ||
writeTextFile, | ||
writeTextFileSync, | ||
removeFile, | ||
removeFileSync, | ||
removeDirectory, | ||
removeDirectorySync, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters