2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-03 08:56:10 -05:00
|
|
|
import Dirent from "./_fs_dirent.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../../_util/assert.ts";
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
export default class Dir {
|
|
|
|
private dirPath: string | Uint8Array;
|
2020-04-16 15:45:30 -04:00
|
|
|
private syncIterator!: Iterator<Deno.DirEntry> | null;
|
|
|
|
private asyncIterator!: AsyncIterator<Deno.DirEntry> | null;
|
2020-03-03 08:56:10 -05:00
|
|
|
|
|
|
|
constructor(path: string | Uint8Array) {
|
|
|
|
this.dirPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
get path(): string {
|
|
|
|
if (this.dirPath instanceof Uint8Array) {
|
|
|
|
return new TextDecoder().decode(this.dirPath);
|
|
|
|
}
|
|
|
|
return this.dirPath;
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-08-24 19:43:54 -04:00
|
|
|
read(callback?: (...args: any[]) => void): Promise<Dirent | null> {
|
2020-04-20 05:29:37 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!this.asyncIterator) {
|
2020-04-29 16:39:37 -04:00
|
|
|
this.asyncIterator = Deno.readDir(this.path)[Symbol.asyncIterator]();
|
2020-03-03 08:56:10 -05:00
|
|
|
}
|
2020-04-20 05:29:37 -04:00
|
|
|
assert(this.asyncIterator);
|
|
|
|
this.asyncIterator
|
|
|
|
.next()
|
|
|
|
.then(({ value }) => {
|
|
|
|
resolve(value ? value : null);
|
|
|
|
if (callback) {
|
|
|
|
callback(null, value ? value : null);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (callback) {
|
|
|
|
callback(err, null);
|
|
|
|
}
|
|
|
|
reject(err);
|
|
|
|
});
|
2020-03-03 08:56:10 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
readSync(): Dirent | null {
|
2020-04-16 15:45:30 -04:00
|
|
|
if (!this.syncIterator) {
|
2020-04-29 16:39:37 -04:00
|
|
|
this.syncIterator = Deno.readDirSync(this.path)![Symbol.iterator]();
|
2020-03-03 08:56:10 -05:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:45:30 -04:00
|
|
|
const file: Deno.DirEntry = this.syncIterator.next().value;
|
2020-03-03 08:56:10 -05:00
|
|
|
|
2020-04-16 15:45:30 -04:00
|
|
|
return file ? new Dirent(file) : null;
|
2020-03-03 08:56:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike Node, Deno does not require managing resource ids for reading
|
|
|
|
* directories, and therefore does not need to close directories when
|
|
|
|
* finished reading.
|
|
|
|
*/
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-08-24 19:43:54 -04:00
|
|
|
close(callback?: (...args: any[]) => void): Promise<void> {
|
2020-03-03 08:56:10 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
if (callback) {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
} catch (err) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike Node, Deno does not require managing resource ids for reading
|
|
|
|
* directories, and therefore does not need to close directories when
|
|
|
|
* finished reading
|
|
|
|
*/
|
|
|
|
closeSync(): void {
|
|
|
|
//No op
|
|
|
|
}
|
|
|
|
|
|
|
|
async *[Symbol.asyncIterator](): AsyncIterableIterator<Dirent> {
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
const dirent: Dirent | null = await this.read();
|
|
|
|
if (dirent === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
yield dirent;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await this.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|