From afe98804b38f0f4cbcfdd21a731f3f1698eb8090 Mon Sep 17 00:00:00 2001 From: Benny van Reeven Date: Fri, 17 May 2019 23:12:12 +0200 Subject: [PATCH] Port option withFileTypes for fs.promises.readdir to Node v10 & v11 (#35533) --- types/node/v10/fs.d.ts | 13 ++++++++++--- types/node/v10/node-tests.ts | 12 ++++++++++++ types/node/v11/fs.d.ts | 13 ++++++++++--- types/node/v11/node-tests.ts | 12 ++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/types/node/v10/fs.d.ts b/types/node/v10/fs.d.ts index f00e4f2640..90db179550 100644 --- a/types/node/v10/fs.d.ts +++ b/types/node/v10/fs.d.ts @@ -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; + function readdir(path: PathLike, options?: { encoding?: BufferEncoding | null; withFileTypes?: false } | BufferEncoding | null): Promise; /** * 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; + function readdir(path: PathLike, options: { encoding: "buffer"; withFileTypes?: false } | "buffer"): Promise; /** * 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; + function readdir(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): Promise; + + /** + * 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; /** * Asynchronous readlink(2) - read value of a symbolic link. diff --git a/types/node/v10/node-tests.ts b/types/node/v10/node-tests.ts index d654aaa3d1..22dab935f2 100644 --- a/types/node/v10/node-tests.ts +++ b/types/node/v10/node-tests.ts @@ -430,6 +430,18 @@ import { Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer, transcode, mode: 0o777, }); } + + { + let names: Promise; + let buffers: Promise; + let namesOrBuffers: Promise; + let entries: Promise; + + 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 }); + } } /////////////////////////////////////////////////////// diff --git a/types/node/v11/fs.d.ts b/types/node/v11/fs.d.ts index 88eaef6b59..bb100fc831 100644 --- a/types/node/v11/fs.d.ts +++ b/types/node/v11/fs.d.ts @@ -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; + function readdir(path: PathLike, options?: { encoding?: BufferEncoding | null; withFileTypes?: false } | BufferEncoding | null): Promise; /** * 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; + function readdir(path: PathLike, options: { encoding: "buffer"; withFileTypes?: false } | "buffer"): Promise; /** * 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; + function readdir(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): Promise; + + /** + * 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; /** * Asynchronous readlink(2) - read value of a symbolic link. diff --git a/types/node/v11/node-tests.ts b/types/node/v11/node-tests.ts index 06ba08771f..69981a7722 100644 --- a/types/node/v11/node-tests.ts +++ b/types/node/v11/node-tests.ts @@ -278,6 +278,18 @@ import Module = require("module"); mode: 0o777, }); } + + { + let names: Promise; + let buffers: Promise; + let namesOrBuffers: Promise; + let entries: Promise; + + 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 }); + } } ////////////////////////////////////////////////////