Skip to content

Commit

Permalink
feat: implement rmdirSync
Browse files Browse the repository at this point in the history
  • Loading branch information
fredbonin committed Mar 14, 2024
1 parent 8bcbfe4 commit 86a6265
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
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
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
18 changes: 18 additions & 0 deletions src/fs/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ 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 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<Object<'js>>) -> Result<()> {
let mut recursive = false;
let mut force = false;
Expand Down
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

0 comments on commit 86a6265

Please sign in to comment.