Skip to content

lwe8/mhaehko

mhaehko

A static analysis tool designed to examine TypeScript and JavaScript projects and produce dependency graphs,using TypeScript APIs.

Key Features

  • Dependency Graph Generation: Analyze TypeScript/JavaScript projects to map file dependencies.
  • Circular Dependency Detection: Implements depth-first search (DFS) algorithms to identify cycles in the dependency graph, helping developers locate problematic import patterns.
  • Topological Sorting: Files are ordered according to their dependency relationships, enabling build systems and bundlers to process files in the correct sequence.
  • Leaf & Mutual Dependencies: Find files with no local imports and mutual (two-way) dependencies.
  • NPM & Node Built-in Detection: List external and built-in module usage.

Use

Installation

Install mhaehko as a development dependency in your project:

npm i -D mhaehko

Basic Usage

ES Modules

import mhaehko from "mhaehko";

const entry = "src/index.ts";
const analysis = await mhaehko(entry);

// Get topologically sorted files
const sorted = analysis.sort();

// Check for circular dependencies
const circular = analysis.circular();

// Find mutual dependencies
const mutual = analysis.mutual();

CommonJS

const mhaehko = require("mhaehko");

const entry = "src/index.js";
mhaehko(entry).then((analysis) => {
  console.log("Circular dependencies:", analysis.circular());
  console.log("NPM dependencies:", analysis.npm());
});

API

mhaehko(entry:string)

Mhaehko Interface Overview

The mhaehko interface defines 12 methods that provide access to different aspects of the dependency analysis.

Method Categories

Structural Analysis Methods

These methods provide insights into the structural properties of the dependency graph, including ordering, cycles, and relationship patterns.

Method Return Type Purpose
sort() string[] Topological sort of dependencies
circular() CircularDependency[] Detected circular dependencies
mutual() string[][] Bidirectional dependency pairs
leaf() string[] Files with no local imports
chain() Record<string, string[]> Complete dependency chains
entryToLeaf() string[][] Paths from entry to leaf files

Query Methods

These methods provide access to the raw dependency data and allow querying specific relationships.

Method Return Type Purpose
deps() Record<string, string[]> Raw dependency graph
dependents(file:string) string[] Files that depend on given file
npm() string[] External NPM dependencies
node() string[] Node.js built-in modules

Visualization and Metadata Methods

These methods provide formatted output and analysis metadata.

Method Return Type Purpose
textGraph() string Text representation of graph
warn() string[] Analysis warnings

Contributing

Contributions are welcome for bug fixes, features, documentation, and code quality improvements.

See detail in CONTRIBUTING.md

License

Apache-2.0 © Pho Thin Mg

About

Analyze a TypeScript/JavaScript project's dependencies and generates a dependency graph

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors