dtre
v2.0.0
Published
Programmatically print a simple directory tree in plain text. It's just an easy way for me to get the format I like.
Downloads
269
Readme
dtre: Directory Tree
/ˈditriː/
Programmatically print a simple directory tree in plain text or JSON. It's just an easy way for me to get the format I like.
Contents
Features
- Recursive Tree Generation: Prints directory structure recursively.
- Exclusion Support: Exclude files or folders using regex patterns.
- Custom Styles: Supports
default
andbacktick
formatting styles. - Output Options:
- Prints raw text output to
--stdout
- Writes to a file
- JSON
- Overview JSON Output: Includes file content and metadata.
- Prints raw text output to
- Configuration Files:
- Supports local (
dtre.json
) and - global (
~/.config/dtre.json
) configuration.
- Supports local (
- CLI Options: Override configuration with command-line arguments.
Module exports a function printDirectoryTree(dir, style, outputPath, excludes, jsonOutput = false, overview = false)
where:
dir
is the directory for which you want the tree.style
is the branch formatting style (e.g.,'default'
or'backtick'
).outputPath
is the file path for saving output, ornull
to print to the console.excludes
is a regex pattern string for files/folders to exclude, ornull
for no exclusions.jsonOutput
(optional) outputs the directory tree in JSON format iftrue
.overview
(optional) includes file metadata and content in JSON output iftrue
.
Example:
import { printDirectoryTree } from "./index.js";
printDirectoryTree('test', 'backtick', 'dist/directory_tree.json', null, true, true);
This generates a JSON file ./dist/directory_tree.json
with content like:
{
"name": "test",
"type": "directory",
"children": [
{
"name": "a1.txt",
"type": "file",
"extension": "txt",
"content": "Sample content of a1.txt"
}
]
}
Import the global npm package:
$ npm install --global dtre
Command-line interface supports various options:
# Print the tree for a directory
$ dtre --directoryPath="assets"
$ dtre -d assets
# Specify the style of output
$ dtre --style="default"
$ dtre -s backtick
# Write the output to a file
$ dtre --outputPath="dir.txt"
$ dtre -o dir.txt
# Exclude files or folders matching a regex
$ dtre --excludes="^[_\.]"
$ dtre -e "^[_\.]"
# Use a global configuration file
$ dtre --global
$ dtre -g
# Output JSON with file content and metadata (overview mode)
$ dtre --overview
$ dtre -w
Place a dtre.json
file in the current working directory. Example:
{
"directoryPath": "./src",
"style": "backtick",
"outputPath": "./tree-output.txt",
"excludes": "node_modules",
"jsonOutput": false,
"overview": false
}
Create a global configuration file in ~/.config/dtre.json
. Example:
{
"directoryPath": "./",
"style": "default",
"outputPath": null,
"excludes": null,
"jsonOutput": false,
"overview": false
}
Important: To use the global configuration, run with the --global
or -g
flag.
LICENSE
MIT © Sam Liebl 2024.