Skip to content

Commit

Permalink
feat: initial file system operations
Browse files Browse the repository at this point in the history
Merge pull request #9 from buenojs/init-bueno-fs
  • Loading branch information
Im-Beast committed Sep 2, 2023
2 parents 8df2009 + 7cdd5b6 commit 2aaf6e5
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 21 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ members = ["ext", "bueno"]
[workspace.dependencies]
deno_core = "0.205.0"
deno_ast = { version = "0.28.0", features = ["transpiling"] }
clap = { version = "4.4.0", features = ["derive"] }
tokio = { version = "1.19.2", features = ["full"] }

bueno_ext = { path = "./ext/" }
14 changes: 8 additions & 6 deletions bueno/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ name = "bueno"
path = "main.rs"

[dependencies]
deno_core = "0.205.0"
deno_ast = { version = "0.28.0", features = ["transpiling"] }
clap = { version = "4.4.0" }
tokio = { version = "1.19.2", features = ["full"] }
deno_core.workspace = true
deno_ast.workspace = true
tokio.workspace = true
bueno_ext.workspace = true

clap = "4.4.0"
reqwest = "0.11.20"
shellexpand = "3.1.0"

bueno_ext = { path = "../ext" }

[build-dependencies]
deno_core = "0.205.0"
deno_core.workspace = true
bueno_ext.workspace = true
16 changes: 2 additions & 14 deletions bueno/build.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
use std::env;
use std::path::PathBuf;

use bueno_ext::extensions::bueno;

fn main() {
// Build the file path to the snapshot.
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let snapshot_path = out.join("BUENO_RUNTIME_SNAPSHOT.bin");

deno_core::extension!(
bueno,
esm_entry_point = "ext:bueno/runtime.js",
esm = [
dir "../ext",

"bueno.js",
"console.js",
"runtime.js",
"io/mod.js",
"io/stdio.js",
],
);

// Create the snapshot.
let output = deno_core::snapshot_util::create_snapshot(
deno_core::snapshot_util::CreateSnapshotOptions {
Expand Down
1 change: 1 addition & 0 deletions ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ path = "lib.rs"

[dependencies]
deno_core.workspace = true
bueno_ext_fs = { path = "./fs/" }
15 changes: 15 additions & 0 deletions ext/fs/Cargo.toml
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
102 changes: 102 additions & 0 deletions ext/fs/lib.rs
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(())
}
130 changes: 130 additions & 0 deletions ext/fs/mod.js
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,
};
18 changes: 18 additions & 0 deletions ext/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
pub mod extensions {
use bueno_ext_fs as fs;

deno_core::extension!(
bueno,
ops = [
// read
fs::op_read_file,
fs::op_read_file_sync,
fs::op_read_text_file,
fs::op_read_text_file_sync,
// write
fs::op_write_file,
fs::op_write_file_sync,
fs::op_write_text_file,
fs::op_write_text_file_sync,
// remove
fs::op_remove_file,
fs::op_remove_file_sync
],
esm_entry_point = "ext:bueno/runtime.js",
esm = [
"bueno.js",
"console.js",
"runtime.js",
"io/mod.js",
"io/stdio.js",
"fs/mod.js"
],
);

Expand Down
1 change: 1 addition & 0 deletions ext/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import "ext:bueno/bueno.js";
import "ext:bueno/console.js";

import "ext:bueno/io/mod.js";
import "ext:bueno/fs/mod.js";

0 comments on commit 2aaf6e5

Please sign in to comment.