From 86a6265c2a7b61d4c739e91af88812011d4b17ce Mon Sep 17 00:00:00 2001 From: Frederic Bonin Date: Thu, 14 Mar 2024 19:36:43 -0400 Subject: [PATCH] feat: implement rmdirSync Refs: #249 --- API.md | 1 + src/fs/mod.rs | 3 +++ src/fs/rm.rs | 18 ++++++++++++++++++ tests/unit/fs.test.ts | 4 ++-- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 709398b322..e5f74e442c 100644 --- a/API.md +++ b/API.md @@ -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 diff --git a/src/fs/mod.rs b/src/fs/mod.rs index fdb7d4643d..d181ef351c 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -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; @@ -76,6 +77,7 @@ impl ModuleDef for FsModule { declare.declare("mkdtempSync")?; declare.declare("readdirSync")?; declare.declare("readFileSync")?; + declare.declare("rmdirSync")?; declare.declare("default")?; @@ -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(()) }) diff --git a/src/fs/rm.rs b/src/fs/rm.rs index 24d04718d7..fead94f058 100644 --- a/src/fs/rm.rs +++ b/src/fs/rm.rs @@ -23,6 +23,24 @@ pub async fn rmdir<'js>(ctx: Ctx<'js>, path: String, options: Opt>) Ok(()) } +#[allow(clippy::manual_async_fn)] +pub fn rmdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt>) -> Result<()> { + let mut recursive = false; + + if let Some(options) = options.0 { + recursive = options.get("recursive").unwrap_or_default(); + } + + 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>) -> Result<()> { let mut recursive = false; let mut force = false; diff --git a/tests/unit/fs.test.ts b/tests/unit/fs.test.ts index 30fd39ff2d..400eadcf9a 100644 --- a/tests/unit/fs.test.ts +++ b/tests/unit/fs.test.ts @@ -179,7 +179,7 @@ describe("mkdtempSync", () => { expect(dirPrefix).toStrictEqual(prefix) // Clean up the temporary directory - await fs.rmdir(dirPath); + await defaultFsImport.rmdirSync(dirPath); }); }); @@ -223,7 +223,7 @@ describe("mkdirSync", () => { expect(dirExists).toBeTruthy(); // Clean up the directory - await fs.rmdir(dirPath, { recursive: true }); + defaultFsImport.rmdirSync(dirPath, { recursive: true }); }); });