fs-directory-tree
List the contents of a directory recursively (asynchronously).
A function that takes an options object as an argument, containing the target directory, how many directories deep to scan, and a function to filter results by; It will then return a promise that will resolve to a tree-like object structure representation of the target directory.
Usage
Options
const options = {
path: "/home/username",
depth: 1,
filter: item => item.stats.isDirectory() || /\.md$/.test(item.path)
};
const tree = await fsDirectoryTree(options);
fsDirectoryTree(options).then(tree => tree);
Yields
{
name: 'username',
path: '/home/username',
type: 'directory',
stats: {},
contents: [
{
name: 'Pictures',
path: '/home/username/Pictures',
type: 'directory',
stats: {},
},
{
name: 'World domination plans.md',
path: '/home/username/World domination plans.md',
type: 'file',
stats: {},
extension: '.md',
},
],
};
Parameters
-
options :
object
-
path :
string
Absolute path of the target directory.
-
depth :
number
How many directories deep to list:
- 0 : do not list.
- 1 : target directory only (default)
- 2+ : arbitrary number of directories deep.
-
filter :
function ({ name, path, stats})
A function that gets called for every item in the tree, whose return value determine whether or not said item is kept or removed from the tree. (This runs before directory contents are scanned, meaning that large directories can be skipped from being evaluated for performance).
This function is passed an object containing the base name, path, and fs.Stats for each path that is evaluated, this way you may filter out paths by testing the path against a regular expression, or determining whether the path is a file or directory by calling
item.stats.isFile()
oritem.stats.isDirectory()
.
-
Returns
-
tree :
object
-
name :
string
-
path :
string
-
type :
string
-
stats :
fs.Stats object
-
extension :
string
-
contents :
array
-
name :