diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-04-16 06:40:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-16 01:40:30 -0400 |
commit | 5ac728a5f1af575d011c2143f5c9273b0fb4c5bb (patch) | |
tree | dfba0fdb3ba17989fd6c3af89ce17a0a71a4df0c /std/node/_fs | |
parent | 6441852a1d5eef0d05ed172a00bef58ad5988842 (diff) |
refactor(cli/js/ops/fs): Improve readdir() and FileInfo interfaces (#4763)
Diffstat (limited to 'std/node/_fs')
-rw-r--r-- | std/node/_fs/_fs_dir.ts | 7 | ||||
-rw-r--r-- | std/node/_fs/_fs_dirent.ts | 8 | ||||
-rw-r--r-- | std/node/_fs/_fs_dirent_test.ts | 51 |
3 files changed, 29 insertions, 37 deletions
diff --git a/std/node/_fs/_fs_dir.ts b/std/node/_fs/_fs_dir.ts index c5bb368c9..af27fb4f2 100644 --- a/std/node/_fs/_fs_dir.ts +++ b/std/node/_fs/_fs_dir.ts @@ -29,7 +29,10 @@ export default class Dir { return new Promise(async (resolve, reject) => { try { if (this.initializationOfDirectoryFilesIsRequired()) { - const denoFiles: Deno.FileInfo[] = await Deno.readdir(this.path); + const denoFiles: Deno.DirEntry[] = []; + for await (const dirEntry of Deno.readdir(this.path)) { + denoFiles.push(dirEntry); + } this.files = denoFiles.map((file) => new Dirent(file)); } const nextFile = this.files.pop(); @@ -55,7 +58,7 @@ export default class Dir { readSync(): Dirent | null { if (this.initializationOfDirectoryFilesIsRequired()) { this.files.push( - ...Deno.readdirSync(this.path).map((file) => new Dirent(file)) + ...[...Deno.readdirSync(this.path)].map((file) => new Dirent(file)) ); } const dirent: Dirent | undefined = this.files.pop(); diff --git a/std/node/_fs/_fs_dirent.ts b/std/node/_fs/_fs_dirent.ts index 38cd23d88..55fbad142 100644 --- a/std/node/_fs/_fs_dirent.ts +++ b/std/node/_fs/_fs_dirent.ts @@ -1,7 +1,7 @@ import { notImplemented } from "../_utils.ts"; export default class Dirent { - constructor(private entry: Deno.FileInfo) {} + constructor(private entry: Deno.DirEntry) {} isBlockDevice(): boolean { return this.entry.blocks != null; @@ -12,7 +12,7 @@ export default class Dirent { } isDirectory(): boolean { - return this.entry.isDirectory(); + return this.entry.isDirectory; } isFIFO(): boolean { @@ -23,7 +23,7 @@ export default class Dirent { } isFile(): boolean { - return this.entry.isFile(); + return this.entry.isFile; } isSocket(): boolean { @@ -32,7 +32,7 @@ export default class Dirent { } isSymbolicLink(): boolean { - return this.entry.isSymlink(); + return this.entry.isSymlink; } get name(): string | null { diff --git a/std/node/_fs/_fs_dirent_test.ts b/std/node/_fs/_fs_dirent_test.ts index 1b1d38d38..548fa6b8a 100644 --- a/std/node/_fs/_fs_dirent_test.ts +++ b/std/node/_fs/_fs_dirent_test.ts @@ -2,7 +2,10 @@ const { test } = Deno; import { assert, assertEquals, assertThrows } from "../../testing/asserts.ts"; import Dirent from "./_fs_dirent.ts"; -class FileInfoMock implements Deno.FileInfo { +class DirEntryMock implements Deno.DirEntry { + isFile = false; + isDirectory = false; + isSymlink = false; size = -1; modified = -1; accessed = -1; @@ -17,26 +20,12 @@ class FileInfoMock implements Deno.FileInfo { rdev = -1; blksize = -1; blocks: number | null = null; - - isFileMock = false; - isDirectoryMock = false; - isSymlinkMock = false; - - isFile(): boolean { - return this.isFileMock; - } - isDirectory(): boolean { - return this.isDirectoryMock; - } - isSymlink(): boolean { - return this.isSymlinkMock; - } } test({ name: "Block devices are correctly identified", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); + const fileInfo: DirEntryMock = new DirEntryMock(); fileInfo.blocks = 5; assert(new Dirent(fileInfo).isBlockDevice()); assert(!new Dirent(fileInfo).isCharacterDevice()); @@ -46,7 +35,7 @@ test({ test({ name: "Character devices are correctly identified", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); + const fileInfo: DirEntryMock = new DirEntryMock(); fileInfo.blocks = null; assert(new Dirent(fileInfo).isCharacterDevice()); assert(!new Dirent(fileInfo).isBlockDevice()); @@ -56,10 +45,10 @@ test({ test({ name: "Directories are correctly identified", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); - fileInfo.isDirectoryMock = true; - fileInfo.isFileMock = false; - fileInfo.isSymlinkMock = false; + const fileInfo: DirEntryMock = new DirEntryMock(); + fileInfo.isDirectory = true; + fileInfo.isFile = false; + fileInfo.isSymlink = false; assert(new Dirent(fileInfo).isDirectory()); assert(!new Dirent(fileInfo).isFile()); assert(!new Dirent(fileInfo).isSymbolicLink()); @@ -69,10 +58,10 @@ test({ test({ name: "Files are correctly identified", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); - fileInfo.isDirectoryMock = false; - fileInfo.isFileMock = true; - fileInfo.isSymlinkMock = false; + const fileInfo: DirEntryMock = new DirEntryMock(); + fileInfo.isDirectory = false; + fileInfo.isFile = true; + fileInfo.isSymlink = false; assert(!new Dirent(fileInfo).isDirectory()); assert(new Dirent(fileInfo).isFile()); assert(!new Dirent(fileInfo).isSymbolicLink()); @@ -82,10 +71,10 @@ test({ test({ name: "Symlinks are correctly identified", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); - fileInfo.isDirectoryMock = false; - fileInfo.isFileMock = false; - fileInfo.isSymlinkMock = true; + const fileInfo: DirEntryMock = new DirEntryMock(); + fileInfo.isDirectory = false; + fileInfo.isFile = false; + fileInfo.isSymlink = true; assert(!new Dirent(fileInfo).isDirectory()); assert(!new Dirent(fileInfo).isFile()); assert(new Dirent(fileInfo).isSymbolicLink()); @@ -95,7 +84,7 @@ test({ test({ name: "File name is correct", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); + const fileInfo: DirEntryMock = new DirEntryMock(); fileInfo.name = "my_file"; assertEquals(new Dirent(fileInfo).name, "my_file"); }, @@ -104,7 +93,7 @@ test({ test({ name: "Socket and FIFO pipes aren't yet available", fn() { - const fileInfo: FileInfoMock = new FileInfoMock(); + const fileInfo: DirEntryMock = new DirEntryMock(); assertThrows( () => { new Dirent(fileInfo).isFIFO(); |