node-treeview
Asynchronous filesystem tree view for node.
Javascript (quick start)
Usage
const TreeView = ; // Using callbackoptions; // Using Promiseoptions; // Using async/await { let tree; try tree = await options; catch error // handle error... // do some stuff...};
Example
const TreeView = ; content: true depth: 2 ;
Here is what the json
output looks like:
The TreeView
lets you listen to item
events.
const TreeView = ; ;
Here is what the txt
output looks like:
file: path/to/dir/file1.txtdir: path/to/dir/subdirfile: path/to/dir/subdir/file2.txtfile: path/to/dir/subdir/logo.pngdone!
Emitted file never have
content
property, emitted dir always havenodes
property equal to an empty array.
The TreeView
lets you process trees in parallel.
const TreeView = ; const treeView = relative: true; Promiseall // Use the same TreeView instance to process different // paths in parallel with the same options treeView treeView;
The TreeView
lets you watch the filesystem.
const TreeView = ; const treeView = ; treeview {/* Item emitted (discovered, added, modified or removed) */}) {/* Initial tree available */}) {/* Item added */}) {/* Item modified */}) {/* Item removed */}) {/* Refreshed tree available (after 'add', 'change' or 'unlink' event) */}) {/* Listen to all events */}); // Start watchingconst watcher = treeview; // Stop watching after 1mn;
Under the hood, the watch
feature is provided by fs.watch
on Mac and Windows, and chokidar
on other platforms.
You should NOT process trees in parallel when you watch the filesystem. Otherwise the
watch
method will not work properly.
TypeScript
Interface overview
// Basic interface of files and directories (used for unreadable resource) // The final output is of type: `TreeNode[]`// and the `TreeView.process` method returns a `Promise<TreeNode[]>`; // List of emitted events;
Options
// Tree sort type
Using typing
; ;; ; promise.then;
Helper
flatten
The flatten
helper lets you get a flat version of the tree.
;; new TreeView.process'path/to/dir'.then;
Or for JavaScript style using require
:
const TreeView = ;const flatten = ;// ...
Here is what the json
output looks like:
clean
The clean
helper lets you clean empty directories from the tree.
;; new TreeView.process'path/to/dir'.then;
Or for JavaScript style using require
:
const TreeView = ;const clean = ;// ...
pretty
The pretty
helper lets you pretty-print the tree.
;; new TreeView.process'path/to/dir'.then;
Here is what the txt
output looks like:
├─ fruits│ ├─ apple.txt│ └─ pears.txt└─ vegetables ├─ bean.txt ├─ potato.txt └─ endive.txt
With the pretty
helper you have full control over how to render the tree.
;; new TreeView.process'path/to/dir'.then;
Here is what the txt
output looks like:
├─ (fruits)│ ├─ apple.txt [51 bytes]│ └─ pears.txt [24 bytes]└─ (vegetables) ├─ bean.txt [13 bytes] ├─ potato.txt [87 bytes] └─ endive.txt [69 bytes]
And for quick rendering, use the predefined renderer
functions.
;;; new TreeView.process'path/to/dir'.then;
Cli
node-treeview Usage: node-treeview <path> [options] Options: --version, -v Show version number [boolean] --help, -h Show help [boolean] --all, -a Include hidden files in output [boolean] --content, -c Add files content to output [boolean] --depth, -d Maximum depth of directories [number] [default: -1] --relative, -r Use relative path [boolean] --include, -i List of directory paths to include in output [array] --exclude, -e List of directory paths to exclude from output [array] --glob, -g Match files based on glob pattern [array] --sort, -s Sort output 0 (Alpha), 1 (FileFirst), 2 (DirFirst) [number] [default: 0] --clean, -n Clean empty directories from output [boolean] --flatten, -f Flatten output [boolean] --pretty, -p Pretty-print output [string] --watch, -w Watch filesystem [boolean] --output, -o Output file path [string] --debug Add debugging information to output [boolean]
Contribute
git clone https://github.com/avine/node-treeview.gitcd ./node-treeviewnpm installnpm run all # npm run clean && npm run build && npm test
License
MIT @ Avine