From 7fa9316973403c6f5ea8e3f5b97e7c6ff13e5c64 Mon Sep 17 00:00:00 2001 From: prettydiff Date: Sat, 19 Aug 2023 20:31:58 -0400 Subject: [PATCH] fs: add new option depth to readdir which regulates depth of recursion This commit contains the necessary fixes to code and documentation per manual testing, but I still need to complete test units for test autoamtion. fixes: https://github.com/nodjs/node/issues/49243 --- doc/api/fs.md | 21 +++++++++++ lib/fs.js | 23 +++++++++--- lib/internal/fs/promises.js | 72 +++++++++++++++++++++---------------- 3 files changed, 81 insertions(+), 35 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 114a51c15dc5d66..76543badbe29fd2 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1305,6 +1305,7 @@ changes: * `encoding` {string} **Default:** `'utf8'` * `withFileTypes` {boolean} **Default:** `false` * `recursive` {boolean} **Default:** `false` + * `depth` {number} **Default:** `1` * Returns: {Promise} Fulfills with an array of the names of the files in the directory excluding `'.'` and `'..'`. @@ -1318,6 +1319,12 @@ will be passed as {Buffer} objects. If `options.withFileTypes` is set to `true`, the resolved array will contain {fs.Dirent} objects. +The `options.depth` determines the amount of recursion to apply. A value less +than 1 applies full recursion, value of 1 applies no recursion, and greater +values determine the depth of descendant directies to traverse. If +`options.recursive` is value `true` then `options.depth` defaults to a value +of `0` and otherwise defaults to a value `1`. + ```mjs import { readdir } from 'node:fs/promises'; @@ -3615,6 +3622,7 @@ changes: * `encoding` {string} **Default:** `'utf8'` * `withFileTypes` {boolean} **Default:** `false` * `recursive` {boolean} **Default:** `false` + * `depth` {number} **Default:** `1` * `callback` {Function} * `err` {Error} * `files` {string\[]|Buffer\[]|fs.Dirent\[]} @@ -3633,6 +3641,12 @@ the filenames returned will be passed as {Buffer} objects. If `options.withFileTypes` is set to `true`, the `files` array will contain {fs.Dirent} objects. +The `options.depth` determines the amount of recursion to apply. A value less +than 1 applies full recursion, value of 1 applies no recursion, and greater +values determine the depth of descendant directies to traverse. If +`options.recursive` is value `true` then `options.depth` defaults to a value +of `0` and otherwise defaults to a value `1`. + ### `fs.readFile(path[, options], callback)`