subtree-hooks
    Preparing search index...

    Class SubtreeHooks<CustomNode>

    Walks a directory subtree and generates a tree structure of the files/folders. Features custom type hinting, example below.

    import { SubtreeHooks, Node } from './subtree-hooks';
    interface CustomNode extends Node<CustomNode> {
    size: number;
    }
    const tree = 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();
    },
    });
    tree.fromPath('./', (error, tree) => {
    if (error) console.error('Error', error);
    else console.log(JSON.stringify(tree));
    });

    Type Parameters

    • CustomNode extends Node<CustomNode>

      declare a Custom Tree interface that be hinted as tree type.

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    onDirStats:
        | ((data: OnStatsData, callback: () => void) => unknown)
        | ((nodeData: Node, callback?: (...data: unknown[]) => void) => void)

    Type Declaration

    • (data: OnStatsData, callback: () => void) => unknown
        • (data: OnStatsData, callback: () => void): unknown
        • Called for each directory after its fs.Stats have been retrieved.

          Parameters

          • data: OnStatsData

            Metadata about the directory, including paths, stats, and current tree node.

          • callback: () => void

            Must be called to continue traversal of the subtree.

          Returns unknown

    • (nodeData: Node, callback?: (...data: unknown[]) => void) => void
    onFileStats:
        | ((nodeData: Node, callback?: (...data: unknown[]) => void) => void)
        | ((data: OnStatsData, callback: () => void) => unknown)

    Type Declaration

    • (nodeData: Node, callback?: (...data: unknown[]) => void) => void
    • (data: OnStatsData, callback: () => void) => unknown
        • (data: OnStatsData, callback: () => void): unknown
        • Called for each file after its fs.Stats have been retrieved.

          Parameters

          • data: OnStatsData

            Metadata about the file, including paths, stats, and current tree node.

          • callback: () => void

            Must be called to continue processing.

          Returns unknown

    onSubTree:
        | ((nodeData: Node, callback?: (...data: unknown[]) => void) => void)
        | ((data: SubFileData, next: () => void, abort: () => void) => unknown)

    Type Declaration

    • (nodeData: Node, callback?: (...data: unknown[]) => void) => void
    • (data: SubFileData, next: () => void, abort: () => void) => unknown
        • (data: SubFileData, next: () => void, abort: () => void): unknown
        • Called when a directory's subtree is ready to be processed. Allows control over whether the subtree is traversed.

          Parameters

          • data: SubFileData

            Metadata about the directory, including paths, and owner directory tree node.

          • next: () => void

            Call this to continue processing the subtree.

          • abort: () => void

            Optionally call this to skip processing this subtree.

          Returns unknown

    Methods

    • Parameters

      • path: string
      • callback: ReturnTree<CustomNode> = console.log

      Returns void