Skip to content

Parses a directory and returns the content as tree

License

Notifications You must be signed in to change notification settings

fliegwerk/dir-tree

Repository files navigation

@fliegwerk/dir-tree

NPM npm (scoped) node-current (scoped) Libraries.io dependency status for latest release GitHub Repo stars

Recursively parses a directory and returns the content as an object

Installation

npm install @fliegwerk/dir-tree

or

yarn add @fliegwerk/dir-tree

Usage

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 system
  • DirectoryElement (element.type === 'directory') - a directory containing children: TreeElement[]
  • LinkElement (element.type === 'link') - a type representing a symlink in the file system. Contains a destination: string that is the link's destination path
  • UnreadableElement (element.type === 'unreadable') - a type representing an unreadable file in the file system. Contains an error: 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 type
  • path: string - the full, absolute path to the element
  • ext: 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)

Additional options

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.

About

Parses a directory and returns the content as tree

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •