1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

fix(ext/node): Add Dirent.path and Dirent.parentPath (#24257)

This commit is contained in:
Divy Srivastava 2024-06-19 12:51:01 +05:30 committed by Bartek Iwańczuk
parent ab2ab0f7fc
commit e3904a784e
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
5 changed files with 25 additions and 0 deletions

View file

@ -764,6 +764,7 @@ impl FileBackedVfs {
.entries .entries
.iter() .iter()
.map(|entry| FsDirEntry { .map(|entry| FsDirEntry {
parent_path: path.to_string_lossy().into_owned(),
name: entry.name().to_string(), name: entry.name().to_string(),
is_file: matches!(entry, VfsEntry::File(_)), is_file: matches!(entry, VfsEntry::File(_)),
is_directory: matches!(entry, VfsEntry::Dir(_)), is_directory: matches!(entry, VfsEntry::Dir(_)),

View file

@ -72,6 +72,7 @@ pub enum FsFileType {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct FsDirEntry { pub struct FsDirEntry {
pub parent_path: String,
pub name: String, pub name: String,
pub is_file: bool, pub is_file: bool,
pub is_directory: bool, pub is_directory: bool,

View file

@ -785,6 +785,7 @@ fn read_dir(path: &Path) -> FsResult<Vec<FsDirEntry>> {
}; };
} }
Some(FsDirEntry { Some(FsDirEntry {
parent_path: path.to_string_lossy().to_string(),
name, name,
is_file: method_or_false!(is_file), is_file: method_or_false!(is_file),
is_directory: method_or_false!(is_dir), is_directory: method_or_false!(is_dir),

View file

@ -43,4 +43,13 @@ export default class Dirent {
get name(): string | null { get name(): string | null {
return this.entry.name; return this.entry.name;
} }
get parentPath(): string {
return this.entry.parentPath;
}
/** @deprecated */
get path(): string {
return this.parentPath;
}
} }

View file

@ -6,6 +6,7 @@ import { Dirent as Dirent_ } from "node:fs";
const Dirent = Dirent_ as any; const Dirent = Dirent_ as any;
class DirEntryMock implements Deno.DirEntry { class DirEntryMock implements Deno.DirEntry {
parentPath = "";
name = ""; name = "";
isFile = false; isFile = false;
isDirectory = false; isDirectory = false;
@ -80,3 +81,15 @@ Deno.test({
); );
}, },
}); });
Deno.test({
name: "Path and parent path is correct",
fn() {
const entry: DirEntryMock = new DirEntryMock();
entry.name = "my_file";
entry.parentPath = "/home/user";
assertEquals(new Dirent(entry).name, "my_file");
assertEquals(new Dirent(entry).path, "/home/user");
assertEquals(new Dirent(entry).parentPath, "/home/user");
},
});