A TypeScript utility to generate filesystem subtrees with configurable hooks, enabling developers to customize directory and file handling.
This project is the modern successor to compound-callback-subtree.
npm install subtree-hooks
or with Yarn:
yarn add subtree-hooks
import { SubtreeHooks, Node } from './subtree-hooks';
interface CustomNode extends Node<CustomNode> {
size: number;
}
new SubtreeHooks<CustomNode>({
onDirStats: (data, cb) => {
data.node.size = 0;
cb();
},
onFileStats: (data, cb) => {
data.node.size = data.stats.size;
if (data.dirNode) data.dirNode.size += data.node.size;
cb();
},
onSubTree: (data, next, abort) => {
if (data.file === '.git' || data.file === 'node_modules') abort();
else next();
},
}).fromPath('./', (error, tree) => {
if (error) console.error('Error', error);
else console.log(JSON.stringify(tree))
});
>> {
"size":87899,
"files":{
"dist":{
"size":13905,
"files":{
"index.d.ts":{"size":1541},
"index.js":{"size":5908},
"types.d.ts":{"size":6445},
"types.js":{"size":11}
}
},
...etc
}}
(See the full API reference in the documentation link below.)
Full API docs are available here: 👉 https://berendkemper.github.io/subtree-hooks/
Clone the repo and install dependencies:
git clone git@github.com:BerendKemper/subtree-hooks.git
cd subtree-hooks
npm install
Build the project:
npm run build
Generate docs:
npm run docs
Issues and pull requests are welcome! Please open an issue if you find a bug or want to suggest a feature.
ISC © Berend Kemper