Skip to content
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

feat: implement rmdirSync #272

Merged
merged 1 commit into from
Mar 15, 2024
Merged
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
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Available globally
[mkdtempSync](https://nodejs.org/api/fs.html#fsmkdtempsyncprefix-options)
[readdirSync](https://nodejs.org/api/fs.html#fsreaddirsyncpath-options)
[readFileSync](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options)
[rmdirSync](https://nodejs.org/api/fs.html#fsrmdirsyncpath-options)

## fs/promises

Expand Down
6 changes: 3 additions & 3 deletions src/fs/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::fs;
use crate::utils::result::ResultExt;

pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<String> {
let (recursive, mode) = get_mkdir_params(options);
let (recursive, mode) = get_params(options);

if recursive {
fs::create_dir_all(&path).await
Expand All @@ -26,7 +26,7 @@ pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>)
}

pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<String> {
let (recursive, mode) = get_mkdir_params(options);
let (recursive, mode) = get_params(options);

if recursive {
std::fs::create_dir_all(&path)
Expand All @@ -41,7 +41,7 @@ pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -
Ok(path)
}

fn get_mkdir_params(options: Opt<Object>) -> (bool, u32) {
fn get_params(options: Opt<Object>) -> (bool, u32) {
let mut recursive = false;
let mut mode = 0o777;

Expand Down
3 changes: 3 additions & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use self::rm::{rmdir, rmfile};
use self::stats::{stat_fn, Stat};
use self::write_file::write_file;
use crate::fs::mkdir::{mkdir, mkdir_sync, mkdtemp, mkdtemp_sync};
use crate::fs::rm::rmdir_sync;

pub const CONSTANT_F_OK: u32 = 0;
pub const CONSTANT_R_OK: u32 = 4;
Expand Down Expand Up @@ -76,6 +77,7 @@ impl ModuleDef for FsModule {
declare.declare("mkdtempSync")?;
declare.declare("readdirSync")?;
declare.declare("readFileSync")?;
declare.declare("rmdirSync")?;

declare.declare("default")?;

Expand All @@ -96,6 +98,7 @@ impl ModuleDef for FsModule {
default.set("mkdtempSync", Func::from(mkdtemp_sync))?;
default.set("readdirSync", Func::from(read_dir_sync))?;
default.set("readFileSync", Func::from(read_file_sync))?;
default.set("rmdirSync", Func::from(rmdir_sync))?;

Ok(())
})
Expand Down
29 changes: 24 additions & 5 deletions src/fs/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use crate::utils::result::ResultExt;

#[allow(clippy::manual_async_fn)]
pub async fn rmdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<()> {
let mut recursive = false;

if let Some(options) = options.0 {
recursive = options.get("recursive").unwrap_or_default();
}
let recursive = get_params(options);

if recursive {
fs::remove_dir_all(&path).await
Expand All @@ -23,6 +19,20 @@ pub async fn rmdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>)
Ok(())
}

#[allow(clippy::manual_async_fn)]
pub fn rmdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<()> {
let recursive = get_params(options);

if recursive {
std::fs::remove_dir_all(&path)
} else {
std::fs::remove_dir(&path)
}
.or_throw_msg(&ctx, &format!("Can't remove dir \"{}\"", &path))?;

Ok(())
}

pub async fn rmfile<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<()> {
let mut recursive = false;
let mut force = false;
Expand Down Expand Up @@ -57,3 +67,12 @@ pub async fn rmfile<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>)

Ok(())
}

fn get_params(options: Opt<Object>) -> bool {
let mut recursive = false;

if let Some(options) = options.0 {
recursive = options.get("recursive").unwrap_or_default();
}
recursive
}
4 changes: 2 additions & 2 deletions tests/unit/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe("mkdtempSync", () => {
expect(dirPrefix).toStrictEqual(prefix)

// Clean up the temporary directory
await fs.rmdir(dirPath);
await defaultFsImport.rmdirSync(dirPath);
});
});

Expand Down Expand Up @@ -223,7 +223,7 @@ describe("mkdirSync", () => {
expect(dirExists).toBeTruthy();

// Clean up the directory
await fs.rmdir(dirPath, { recursive: true });
defaultFsImport.rmdirSync(dirPath, { recursive: true });
});
});

Expand Down