Tree structure utils for terminal printing and file traversing.
$ npm install term-tree
Create and print filenames from array.
import { createTreeFromFiles, renderTree } from 'term-tree'
const files = ['package/src/foo/bar.ts', /* ... */];
const tree = createTreeFromFiles(files);
const graph = renderTree(source);
console.log(graph);
package
├── src
│ ├── foo
│ │ ├── bar.ts
│ │ └── baz.ts
│ └── shared
│ ├── context.ts
│ └── vendors/ansi.ts
├── tests/foo
│ ├── bar.ts
│ └── baz.ts
└── package.json
Walk directory to read files:
import fs from 'fs/promises';
import { walkDirectory } from 'term-tree';
const files = {};
for await (const [name, stats] of walkDirectory('./')) {
if (stats.isFile) {
files[name] = await fs.readFile(name, 'utf8');
}
}
$ npm install -g term-tree
$ term-tree --size
/Users/Asuka109/repositories/term-tree (82.21 kB)
├── README.md (5.52 kB)
├── bin.js (132 B)
├── package.json (762 B)
├── pnpm-lock.yaml (48.53 kB)
├── src (9.85 kB)
│ ├── cli.ts (3.32 kB)
│ ├── fs.ts (1.25 kB)
│ ├── index.ts (122 B)
│ ├── node.ts (2.5 kB)
│ ├── render.ts (1.96 kB)
│ ├── types.ts (202 B)
│ └── utils.ts (213 B)
├── tests (5.4 kB)
│ ├── fs.test.ts (1.44 kB)
│ ├── node.test.ts (3.73 kB)
│ └── tsconfig.json (82 B)
├── tsconfig.json (11.33 kB)
└── vitest.config.ts (174 B)
$ term-tree --help
Usage: term-tree [options] [dir]
Tree structure utils for terminal printing and file traversing.
Arguments:
dir directory to list (default: "./")
Options:
-V, --version output the version number
-d, --depth [depth] depth of the tree to search (default: 5)
-s, --size show file size
-S, --symbols <symbols> symbols of tree (default: "ansi")
-a, --absolute print absolute path
--combine combine files and directories (default: true)
--no-combine
-l, --follow-symlink follow symlink
-D, --include-dots include dot files
-M, --include-node-modules include node_modules
-r, --review print and review the options
-j, --json output as json
-h, --help display help for command
Create an array from async generator.
-
generator
AsyncGenerator<T>
Visit the tree node and its children in DFS.
-
source
TreeNode -
handler
function (src: TreeNode, parent: TreeNode): void -
parent
TreeNode?
Create a tree from a list of files.
-
files
any -
options
CreateTreeFromFilesOptions (optional, default{}
)
The symbols to use for rendering the tree.
Type: (TreeSymbols | "ansi"
| "ascii"
)
Render the tree node into terminal graph.
-
source
TreeNode -
options
RenderTreeOptions (optional, default{}
)
Generate and walk the directory tree.
-
dir
string -
options
WalkDirectoryOptions?
Returns AsyncGenerator<[string, fs.Stats]>