Recursively parses a directory and returns the content as an object
npm install @fliegwerk/dir-tree
or
yarn add @fliegwerk/dir-tree
const dirTree = require('@fliegwerk/dir-tree');
const {join} = require('path');
const tree = await dirTree(
join(__dirname, 'template')
);
console.log(tree.type); // 'file' | 'directory' | 'link' | 'unreadable'
console.log(tree);
dirTree(path)
returns a Promise
that resolves to a TreeElement
object.
A TreeElement
object can be one of four types:
FileElement
(element.type === 'file'
) - a simple file in the file systemDirectoryElement
(element.type === 'directory'
) - a directory containingchildren: TreeElement[]
LinkElement
(element.type === 'link'
) - a type representing a symlink in the file system. Contains adestination: string
that is the link's destination pathUnreadableElement
(element.type === 'unreadable'
) - a type representing an unreadable file in the file system. Contains anerror: unknown
that is the error that got thrown while trying to read the file.
Apart from these additional properties, all TreeElement
objects have the following properties:
type: 'file' | 'directory' | 'link' | 'unreadable'
- the element typepath: string
- the full, absolute path to the elementext: string
- the file name extension (including the period), if it exists. Empty string (''
) if no file extension exists.name: string
- the name of the file (including the extension)
No additional options exist in this library. We want to focus on doing one job well (and rock-solid). That's why we don' t provide a lot of options around our core functionality.