[node] add a new option, withFileTypes, to fs.promises.readdir (#35213)

* [node] add withFileTypes option to fs.promises.readdir

node v12 newly added withFileTypes option to fs.promises.readdir

* [node] add a test for fs.promises.readdir
This commit is contained in:
Eon S. Jeon 2019-05-15 06:56:34 +09:00 committed by Nathan Shively-Sanders
parent 216d877abf
commit 500da47fe5
2 changed files with 22 additions and 3 deletions

13
types/node/fs.d.ts vendored
View File

@ -2056,21 +2056,28 @@ declare module "fs" {
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
*/
function readdir(path: PathLike, options?: { encoding?: BufferEncoding | null } | BufferEncoding | null): Promise<string[]>;
function readdir(path: PathLike, options?: { encoding?: BufferEncoding | null; withFileTypes?: false } | BufferEncoding | null): Promise<string[]>;
/**
* Asynchronous readdir(3) - read a directory.
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
*/
function readdir(path: PathLike, options: { encoding: "buffer" } | "buffer"): Promise<Buffer[]>;
function readdir(path: PathLike, options: { encoding: "buffer"; withFileTypes?: false } | "buffer"): Promise<Buffer[]>;
/**
* Asynchronous readdir(3) - read a directory.
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
*/
function readdir(path: PathLike, options?: { encoding?: string | null } | string | null): Promise<string[] | Buffer[]>;
function readdir(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): Promise<string[] | Buffer[]>;
/**
* Asynchronous readdir(3) - read a directory.
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
* @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
*/
function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise<Dirent[]>;
/**
* Asynchronous readlink(2) - read value of a symbolic link.

View File

@ -278,6 +278,18 @@ import Module = require("module");
mode: 0o777,
});
}
{
let names: Promise<string[]>;
let buffers: Promise<Buffer[]>;
let namesOrBuffers: Promise<string[] | Buffer[]>;
let entries: Promise<fs.Dirent[]>;
names = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: false });
buffers = fs.promises.readdir('/path/to/dir', { encoding: 'buffer', withFileTypes: false });
namesOrBuffers = fs.promises.readdir('/path/to/dir', { encoding: 'SOME OTHER', withFileTypes: false });
entries = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: true });
}
}
////////////////////////////////////////////////////