Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option —-stdin for piping predefined tree #103

Merged
merged 1 commit into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
test
.madgerc
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## v1.2.0 (Sep 1, 2016)
* Added option `--stdin` to be used for piping a predefined tree.

## v1.1.0 (Aug 23, 2016)
* Support for setting custom GraphViz options with config `graphVizOptions`.

Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ $ apt-get install graphviz

# API

## madge(path: string|array, config: object)
## madge(path: string|array|object, config: object)

> `path` is a single file or directory to read (or an array of files/directories).
> `path` is a single file or directory, or an array of files/directories to read. A predefined tree can also be passed in as an object.

> `config` is optional and should be [configuration](#configuration) to be used.
> `config` is optional and should be the [configuration](#configuration) to use.

> Returns a `Promise` resolved with the Madge instance object.

Expand Down Expand Up @@ -235,6 +235,12 @@ $ madge --image graph.svg path/src/app.js
$ madge --dot path/src/app.js > graph.gv
```

> Using pipe to transform tree (this example will uppercase all paths)

```sh
$ madge --json path/src/app.js | tr '[a-z]' '[A-Z]' | madge --stdin
```

# Debugging

> To enable debugging output if you encounter problems, run madge with the `--debug` option then throw the result in a gist when creating issues on GitHub.
Expand Down
100 changes: 62 additions & 38 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ program
.option('--require-config <file>', 'path to RequireJS config')
.option('--webpack-config <file>', 'path to webpack config')
.option('--no-color', 'disable color in output and image', false)
.option('--stdin', 'read predefined tree from STDIN', false)
.option('--debug', 'turn on debug output', false)
.parse(process.argv);

if (!program.args.length) {
if (!program.args.length && !program.stdin) {
console.log(program.helpInformation());
process.exit(1);
}
Expand Down Expand Up @@ -82,52 +83,75 @@ if (!program.color) {
config.edgeColor = '#757575';
}

madge(program.args, config)
.then((res) => {
if (program.summary) {
return output.summary(res.obj(), {
json: program.json
new Promise((resolve, reject) => {
if (program.stdin) {
let buffer = '';

process.stdin
.resume()
.setEncoding('utf8')
.on('data', (chunk) => {
buffer += chunk;
})
.on('end', () => {
try {
resolve(JSON.parse(buffer));
} catch (e) {
reject(e);
}
});
}

if (program.depends) {
return output.depends(res.depends(program.depends), {
json: program.json
});
}
} else {
resolve(program.args);
}
})
.then((src) => {
return madge(src, config);
})
.then((res) => {
if (program.summary) {
return output.summary(res.obj(), {
json: program.json
});
}

if (program.circular) {
const circular = res.circular();
if (program.depends) {
return output.depends(res.depends(program.depends), {
json: program.json
});
}

output.circular(circular, {
json: program.json
});
if (program.circular) {
const circular = res.circular();

if (circular.length) {
process.exit(1);
}
output.circular(circular, {
json: program.json
});

return;
if (circular.length) {
process.exit(1);
}

if (program.image) {
return res.image(program.image).then((imagePath) => {
console.log('Image created at %s', imagePath);
});
}
return;
}

if (program.dot) {
return res.dot().then((output) => {
process.stdout.write(output);
});
}
if (program.image) {
return res.image(program.image).then((imagePath) => {
console.log('Image created at %s', imagePath);
});
}

return output.list(res.obj(), {
json: program.json
if (program.dot) {
return res.dot().then((output) => {
process.stdout.write(output);
});
})
.catch((err) => {
output.error(err);
}

process.exit(1);
return output.list(res.obj(), {
json: program.json
});
})
.catch((err) => {
output.error(err);

process.exit(1);
});
13 changes: 10 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const tree = require('./tree');
const cyclic = require('./cyclic');
const graph = require('./graph');
const log = require('./log');

const defaultConfig = {
baseDir: null,
Expand All @@ -29,7 +30,7 @@ class Madge {
* Class constructor.
* @constructor
* @api public
* @param {String|Array} path
* @param {String|Array|Object} path
* @param {Object} config
* @return {Promise}
*/
Expand All @@ -38,12 +39,18 @@ class Madge {
throw new Error('path argument not provided');
}

this.config = Object.assign({}, defaultConfig, config);

if (typeof path === 'object' && !Array.isArray(path)) {
this.tree = path;
log('using predefined tree %o', path);
return Promise.resolve(this);
}

if (typeof path === 'string') {
path = [path];
}

this.config = Object.assign({}, defaultConfig, config);

return tree(path, this.config).then((tree) => {
this.tree = tree;
return this;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"commander": "^2.9.0",
"commondir": "^1.0.1",
"debug": "^2.2.0",
"dependency-tree": "^5.5.0",
"dependency-tree": "^5.5.1",
"graphviz": "^0.0.8",
"mz": "^2.4.0",
"rc": "^1.1.6",
Expand Down
17 changes: 17 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ describe('API', () => {
}).catch(done);
});

it('takes a predefined tree', (done) => {
madge({
a: ['b', 'c', 'd'],
b: ['c'],
c: [],
d: ['a']
}).then((res) => {
res.obj().should.eql({
a: ['b', 'c', 'd'],
b: ['c'],
c: [],
d: ['a']
});
done();
}).catch(done);
});

it('can exclude modules using RegExp', (done) => {
madge(__dirname + '/commonjs/a.js', {
excludeRegExp: ['^b$']
Expand Down