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/mkdir.rs b/src/fs/mkdir.rs index 22494ed7ad..aa0fb7c6b2 100644 --- a/src/fs/mkdir.rs +++ b/src/fs/mkdir.rs @@ -9,7 +9,7 @@ use tokio::fs; use crate::utils::result::ResultExt; pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt>) -> Result { - let (recursive, mode) = get_mkdir_params(options); + let (recursive, mode) = get_params(options); if recursive { fs::create_dir_all(&path).await @@ -26,7 +26,7 @@ pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt>) } pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt>) -> Result { - let (recursive, mode) = get_mkdir_params(options); + let (recursive, mode) = get_params(options); if recursive { std::fs::create_dir_all(&path) @@ -41,7 +41,7 @@ pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt>) - Ok(path) } -fn get_mkdir_params(options: Opt) -> (bool, u32) { +fn get_params(options: Opt) -> (bool, u32) { let mut recursive = false; let mut mode = 0o777; diff --git a/src/fs/mod.rs b/src/fs/mod.rs index fdb7d4643d..892598ca06 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..6e56bf8143 100644 --- a/src/fs/rm.rs +++ b/src/fs/rm.rs @@ -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>) -> 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 @@ -23,6 +19,20 @@ 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 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>) -> Result<()> { let mut recursive = false; let mut force = false; @@ -57,3 +67,12 @@ pub async fn rmfile<'js>(ctx: Ctx<'js>, path: String, options: Opt>) Ok(()) } + +fn get_params(options: Opt) -> bool { + let mut recursive = false; + + if let Some(options) = options.0 { + recursive = options.get("recursive").unwrap_or_default(); + } + recursive +} 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 }); }); });