(path, config) => new Madge(path, config)
n/a
dot = function (modules, config) {
const nodes = {};
const g = graphviz.digraph('G');
return checkGraphvizInstalled(config)
.then(() => {
Object.keys(modules).forEach((id) => {
nodes[id] = nodes[id] || g.addNode(id);
modules[id].forEach((depId) => {
nodes[depId] = nodes[depId] || g.addNode(depId);
g.addEdge(nodes[id], nodes[depId]);
});
});
return g.to_dot();
});
}...
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.depends());
});
```
#### .dot()
> Returns a `Promise` resolved with a DOT representation of the module dependency graph.
```javascript
const madge = require('madge');
madge('path/to/app.js')
...image = function (modules, circular, imagePath, config) {
const options = createGraphvizOptions(config);
options.type = path.extname(imagePath).replace('.', '') || 'png';
return checkGraphvizInstalled(config)
.then(() => {
return createGraph(modules, circular, config, options)
.then((image) => fs.writeFile(imagePath, image))
.then(() => path.resolve(imagePath));
});
}...
madge('path/to/app.js')
.then((res) => res.dot())
.then((output) => {
console.log(output);
});
```
#### .image(imagePath: string)
> Write the graph as an image to the given image path. The [image format](http://www.graphviz.org/content/output-formats) to
use is determined from the file extension. Returns a `Promise` resolved with a full path to the written image.
```javascript
const madge = require('madge');
madge('path/to/app.js')
...circular = function (spinner, res, circular, opts) {
if (opts.json) {
return printJSON(circular);
}
const cyclicCount = Object.keys(circular).length;
if (!circular.length) {
spinner.succeed(chalk.bold('No circular dependency found!'));
} else {
spinner.fail(chalk.red.bold(`Found ${pluralize('circular dependency', cyclicCount, true)}!\n`));
circular.forEach((path, idx) => {
process.stdout.write(chalk.dim(idx + 1 + ') '));
path.forEach((module, idx) => {
if (idx) {
process.stdout.write(chalk.dim(' > '));
}
process.stdout.write(chalk.cyan.bold(module));
});
process.stdout.write('\n');
});
}
}...
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.warnings());
});
```
#### .circular()
> Returns an `Array` with all modules that has circular dependencies.
```javascript
const madge = require('madge');
madge('path/to/app.js').then((res) => {
...depends = function (depends, opts) {
if (opts.json) {
return printJSON(depends);
}
depends.forEach((id) => {
console.log(chalk.cyan.bold(id));
});
}...
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.circular());
});
```
#### .depends()
> Returns an `Array` with all modules that depends on a given module.
```javascript
const madge = require('madge');
madge('path/to/app.js').then((res) => {
...getResultSummary = function (res, startTime) {
const warningCount = res.warnings().skipped.length;
const fileCount = Object.keys(res.obj()).length;
console.log('Processed %s %s %s %s\n',
chalk.bold(fileCount),
pluralize('file', fileCount),
chalk.dim(`(${prettyMs(Date.now() - startTime)})`),
warningCount ? '(' + chalk.yellow.bold(pluralize('warning', warningCount, true)) + ')' : ''
);
}...
}
return madge(src, config);
})
.then((res) => {
if (!program.json && !program.dot) {
spinner.stop();
output.getResultSummary(res, startTime);
}
if (program.summary) {
output.summary(res.obj(), {
json: program.json
});
...list = function (modules, opts) {
opts = opts || {};
if (opts.json) {
return printJSON(modules);
}
Object.keys(modules).forEach((id) => {
console.log(chalk.cyan.bold(id));
modules[id].forEach((depId) => {
console.log(chalk.grey(` ${depId}`));
});
});
}...
if (program.dot) {
return res.dot().then((output) => {
process.stdout.write(output);
return res;
});
}
output.list(res.obj(), {
json: program.json
});
return res;
})
.then((res) => {
if (program.warning && !program.json) {
...summary = function (modules, opts) {
const o = {};
opts = opts || {};
Object.keys(modules).sort((a, b) => {
return modules[b].length - modules[a].length;
}).forEach((id) => {
if (opts.json) {
o[id] = modules[id].length;
} else {
console.log('%s %s', chalk.cyan.bold(modules[id].length), chalk.grey(id));
}
});
if (opts.json) {
return printJSON(o);
}
}...
.then((res) => {
if (!program.json && !program.dot) {
spinner.stop();
output.getResultSummary(res, startTime);
}
if (program.summary) {
output.summary(res.obj(), {
json: program.json
});
return res;
}
if (program.depends) {
...warnings = function (res) {
const skipped = res.warnings().skipped;
if (skipped.length) {
console.log(chalk.yellow.bold(`\n✖ Skipped ${pluralize('file', skipped.length, true)}\n`));
skipped.forEach((file) => {
console.log(chalk.yellow(file));
});
}
}...
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.obj());
});
```
#### .warnings()
> Returns an `Object` of warnings.
```javascript
const madge = require('madge');
madge('path/to/app.js').then((res) => {
...