any-dependency-tree

Build dependencies tree of any artifacts with just single interfaces to implement

Usage no npm install needed!

<script type="module">
  import anyDependencyTree from 'https://cdn.skypack.dev/any-dependency-tree';
</script>

README

Description

CircleCIGreenkeeper badge

Build and visualize dependency tree for any hierarchical structure.
Just implement element => dependencies: element[] interface

Usage

Install the package

npm i any-depepndency-tree

Minimal

Implement EntityDependencyApi

...
const myImp: EntityDependencyApi<MyHierarchyType> = ...; // Mandatory custom implementation

Define starting point of the tree

const myRoot: MyHierarchyType = ...; // this is starting poing of the tree;

Build the complete tree

...
const builder = new DependencyTreeBuilder<MyHierarchyType>(myImp);
const rootNode = await dependencyTreeBuilder.buildDependencyTree(myRoot);
// now the rootTreeNode has the complete tree

Optional

Can visualize the tree using standard JSON.stringify approach

...
const serializingVisitor: = new SerializingVisitor();
const treeString: string = serializingVisitor.visitTree(rootNode);
console.log(treeString);

Or via custom implementation of tree node element visualizer

...
const customSerializer: Serializer<MyHierarchyType> = ... // Optional custom implementation
const serializingVisitor: = new SerializingVisitor(customSerializer);
const treeString: string = serializingVisitor.visitTree(rootTreeNode);
console.log(treeString);

Also supports:

  • Exclusion filter to remove sub-tree
  • Inclusion filter to leave only some parts of the tree
...
const exclusionFilter: Filter<MyHierarchyType> = ... // Optional custom implementation
const inclusionFilter: Filter<MyHierarchyType> = ... // Optional custom implementation
const serializingVisitor: = new SerializingVisitor(customSerializer, inclusionFilter, exclusionFilter);
const filteredTreeString: string = serializingVisitor.visitTree(rootTreeNode);
console.log(filteredTreeString);

Can also convert built tree into the ordered list where children are at the beginning

const orderVisitor: Ordering = new OrderingVisitor(true);
const orderedElements: DependencyTreeNode<any>[] = orderVisitor.visitTree(rootNode);

orderedElements now contains all nodes from the tree including root as the very last element.

Examples

  1. index.test.ts
  2. serializing.test.ts
  3. ordering.test.ts

Contribution

If you are interested in contributing, please take a look at the CONTRIBUTING guide.