Skip to content

Commit

Permalink
Merge branch 'master' into depth-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloLION committed Sep 30, 2023
2 parents 77ddee5 + c2191a1 commit 88768b8
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 282 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13.6.0
18
9 changes: 9 additions & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"pkgFiles": ["package.json", "package-lock.json"],
"git": {
"requireCleanWorkingDir": false
},
"hooks": {
"after:bump": "auto-changelog --hide-credit --package"
}
}
15 changes: 4 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
language: node_js
node_js:
- 10
- 12
- 13
- 14
- 16
- 18
sudo: false
dist: bionic
dist: focal
addons:
apt:
packages:
- graphviz
script:
- npm test
- npm run debug
deploy:
provider: npm
email: "[email protected]"
api_key:
secure: nArcGw6OdS7J1bC+BuXk/2ER7z2Cc+Rjk0oBE/hoTsYo4+ry2LUzWM+nl28om5pF1xtXWwe0fb1rG4f3Ls28iHpH7E5Uz5+RfVfB4VH9w1HL4nBfLW5Ljj9J0ViQzSN1OPJ45B2lwHU8JH7N2c62FDJ5kqLn83WTGdb7Bgyau6o=
on:
tags: true
566 changes: 369 additions & 197 deletions CHANGELOG.md

Large diffs are not rendered by default.

34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

<p align="center">
<img alt="Last version" src="https://img.shields.io/github/tag/pahen/madge.svg?style=flat-square" />
<a href="https://travis-ci.com/pahen/madge">
<a href="https://app.travis-ci.com/github/pahen/madge">
<img alt="Build Status" src="http://img.shields.io/travis/pahen/madge/master.svg?style=flat-square" />
</a>
<a href="https://david-dm.org/pahen/madge">
<img alt="Dependency status" src="http://img.shields.io/david/pahen/madge.svg?style=flat-square" />
</a>
<a href="https://david-dm.org/pahen/madge#info=devDependencies">
<img alg="Dev Dependencies status" src="http://img.shields.io/david/dev/pahen/madge.svg?style=flat-square" />
</a>
<a href="https://www.npmjs.org/package/madge">
<img alg="NPM Status" src="http://img.shields.io/npm/dm/madge.svg?style=flat-square" />
</a>
Expand All @@ -33,7 +27,7 @@

Read the [changelog](CHANGELOG.md) for latest changes.

> I've worked with Madge on my free time for the last couple of years and it's been a great experience. It started as an experiment but turned out to be a very useful tool for many developers. I have many ideas for the project and it would definitely be easier to dedicate more time to it with some [financial support](#donations) 🙏
> I've worked with Madge on my free time for the last couple of years and it's been a great experience. It started as an experiment but turned out to be a very useful tool for many developers. I have many ideas for the project and it would definitely be easier to dedicate more time to it with some [financial support](#donations-%EF%B8%8F) 🙏
>
> Regardless of your contribution, thanks for your support!
Expand Down Expand Up @@ -243,7 +237,7 @@ Property | Type | Default | Description
`graphVizOptions` | Object | false | Custom Graphviz [options](https://graphviz.gitlab.io/_pages/doc/info/attrs.html)
`graphVizPath` | String | null | Custom Graphviz path
`detectiveOptions` | Object | false | Custom `detective` options for [dependency-tree](https://github.com/dependents/node-dependency-tree) and [precinct](https://github.com/dependents/node-precinct#usage)
`dependencyFilter` | Function | false | Function called with a dependency filepath (exclude substree by returning false)
`dependencyFilter` | Function | false | Function called with a dependency filepath (exclude subtrees by returning false)

You can use configuration file either in `.madgerc` in your project or home folder or directly in `package.json`. Look [here](https://github.com/dominictarr/rc#standards) for alternative locations for the file.

Expand Down Expand Up @@ -380,6 +374,12 @@ npm install
npm test
```

# Creating a release

```sh
npm run release
```

# FAQ

## Missing dependencies?
Expand Down Expand Up @@ -444,6 +444,20 @@ Put this in your madge config.
}
```

## How to ignore dynamic imports in Typescript?

Put this in your madge config.

```json
{
"detectiveOptions": {
"ts": {
"skipAsyncImports": true
}
}
}
```

## Mixing TypesScript and Javascript imports?

Ensure you have this in your `.tsconfig` file.
Expand Down Expand Up @@ -497,7 +511,7 @@ This project exists thanks to all the people who contribute.

## Donations ❤️

Thanks to the awesome people below for making donations! 🙏[[Donate](https://paypal.me/pahen)]
Thanks to the awesome people below for making donations! 🙏[Donate](https://paypal.me/pahen)

<p>
<a href="https://github.com/BeroBurny" target="_blank">
Expand Down
13 changes: 8 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ program
.option('--orphans', 'show modules that no one is depending on')
.option('--leaves', 'show modules that have no dependencies')
.option('--dot', 'show graph using the DOT language')
.option('--rankdir <direction>', 'set the direction of the graph layout')
.option('--extensions <list>', 'comma separated string of valid file extensions')
.option('--require-config <file>', 'path to RequireJS config')
.option('--webpack-config <file>', 'path to webpack config')
Expand Down Expand Up @@ -75,7 +76,7 @@ const spinner = ora({
text: 'Finding files',
color: 'white',
interval: 100000,
isEnabled: program.spinner
isEnabled: program.spinner === 'false' ? false : null
});

let exitCode = 0;
Expand Down Expand Up @@ -144,6 +145,10 @@ if (!program.color) {
config.edgeColor = '#757575';
}

if (program.rankdir) {
config.rankdir = program.rankdir;
}

function dependencyFilter() {
let prevFile;

Expand All @@ -153,10 +158,8 @@ function dependencyFilter() {
const dir = path.dirname(relPath) + '/';
const file = path.basename(relPath);

if (program.spinner) {
spinner.text = chalk.grey(dir) + chalk.cyan(file);
spinner.render();
}
spinner.text = chalk.grey(dir) + chalk.cyan(file);

prevFile = traversedFilePath;
}
};
Expand Down
11 changes: 11 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const path_ = require('path');
const tree = require('./tree');
const cyclic = require('./cyclic');
const graph = require('./graph');
Expand Down Expand Up @@ -47,6 +48,16 @@ class Madge {
}

this.config = Object.assign({}, defaultConfig, config);
if (typeof this.config.tsConfig === 'string') {
const ts = require('typescript');
const tsParsedConfig = ts.readJsonConfigFile(this.config.tsConfig, ts.sys.readFile);
const obj = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path_.dirname(config.tsConfig));
this.config.tsConfig = {
...obj.raw,
compilerOptions: obj.options
};
log('using tsconfig %o', this.config.tsConfig);
}

if (typeof path === 'object' && !Array.isArray(path)) {
this.tree = path;
Expand Down
85 changes: 42 additions & 43 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const path = require('path');
const {promisify} = require('util');
const graphviz = require('graphviz');

const gv = require('ts-graphviz');
const adapter = require('ts-graphviz/adapter');
const toArray = require('stream-to-array');
const exec = promisify(require('child_process').execFile);
const writeFile = promisify(require('fs').writeFile);

Expand All @@ -13,8 +14,8 @@ const writeFile = promisify(require('fs').writeFile);
* @param {String} color
*/
function setNodeColor(node, color) {
node.set('color', color);
node.set('fontcolor', color);
node.attributes.set('color', color);
node.attributes.set('fontcolor', color);
}

/**
Expand Down Expand Up @@ -45,28 +46,31 @@ function createGraphvizOptions(config) {
const graphVizOptions = config.graphVizOptions || {};

return {
// Graph
G: Object.assign({
overlap: false,
pad: 0.3,
rankdir: config.rankdir,
layout: config.layout,
bgcolor: config.backgroundColor
}, graphVizOptions.G),
// Edge
E: Object.assign({
color: config.edgeColor
}, graphVizOptions.E),
// Node
N: Object.assign({
fontname: config.fontName,
fontsize: config.fontSize,
color: config.nodeColor,
shape: config.nodeShape,
style: config.nodeStyle,
height: 0,
fontcolor: config.nodeColor
}, graphVizOptions.N)
dotCommand: config.graphVizPath ? config.graphVizPath : null,
attributes: {
// Graph
graph: Object.assign({
overlap: false,
pad: 0.3,
rankdir: config.rankdir,
layout: config.layout,
bgcolor: config.backgroundColor
}, graphVizOptions.G),
// Edge
edge: Object.assign({
color: config.edgeColor
}, graphVizOptions.E),
// Node
node: Object.assign({
fontname: config.fontName,
fontsize: config.fontSize,
color: config.nodeColor,
shape: config.nodeShape,
style: config.nodeStyle,
height: 0,
fontcolor: config.nodeColor
}, graphVizOptions.N)
}
};
}

Expand All @@ -79,16 +83,12 @@ function createGraphvizOptions(config) {
* @return {Promise}
*/
function createGraph(modules, circular, config, options) {
const g = graphviz.digraph('G');
const g = gv.digraph('G');
const nodes = {};
const cyclicModules = circular.reduce((a, b) => a.concat(b), []);

if (config.graphVizPath) {
g.setGraphVizPath(config.graphVizPath);
}

Object.keys(modules).forEach((id) => {
nodes[id] = nodes[id] || g.addNode(id);
nodes[id] = nodes[id] || g.createNode(id);

if (!modules[id].length) {
setNodeColor(nodes[id], config.noDependencyColor);
Expand All @@ -97,21 +97,20 @@ function createGraph(modules, circular, config, options) {
}

modules[id].forEach((depId) => {
nodes[depId] = nodes[depId] || g.addNode(depId);
nodes[depId] = nodes[depId] || g.createNode(depId);

if (!modules[depId]) {
setNodeColor(nodes[depId], config.noDependencyColor);
}

g.addEdge(nodes[id], nodes[depId]);
});
});

return new Promise((resolve, reject) => {
g.output(options, resolve, (code, out, err) => {
reject(new Error(err));
g.createEdge([nodes[id], nodes[depId]]);
});
});
const dot = gv.toDot(g);
return adapter
.toStream(dot, options)
.then(toArray)
.then(Buffer.concat);
}

/**
Expand All @@ -124,7 +123,7 @@ function createGraph(modules, circular, config, options) {
module.exports.svg = function (modules, circular, config) {
const options = createGraphvizOptions(config);

options.type = 'svg';
options.format = 'svg';

return checkGraphvizInstalled(config)
.then(() => createGraph(modules, circular, config, options));
Expand All @@ -141,7 +140,7 @@ module.exports.svg = function (modules, circular, config) {
module.exports.image = function (modules, circular, imagePath, config) {
const options = createGraphvizOptions(config);

options.type = path.extname(imagePath).replace('.', '') || 'png';
options.format = path.extname(imagePath).replace('.', '') || 'png';

return checkGraphvizInstalled(config)
.then(() => {
Expand All @@ -161,7 +160,7 @@ module.exports.image = function (modules, circular, imagePath, config) {
module.exports.dot = function (modules, circular, config) {
const options = createGraphvizOptions(config);

options.type = 'dot';
options.format = 'dot';

return checkGraphvizInstalled(config)
.then(() => createGraph(modules, circular, config, options))
Expand Down

0 comments on commit 88768b8

Please sign in to comment.