Skip to content

Commit

Permalink
Support passing in dependencies directly via -f json -d file.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Radcliffe committed Apr 15, 2015
1 parent f709a0f commit 64befee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
18 changes: 13 additions & 5 deletions bin/dependo
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@
var Version = require('../lib/version');
var Commander = require('commander');
var Dependo = require('../lib/dependo');
var fs = require('fs');

Commander
.version(Version)
.usage('[options] <file|dir ...>')
.option('-f, --format <name>', 'format to parse (amd/cjs/es6)', 'amd')
.option('-f, --format <name>', 'format to parse (amd/cjs/es6/json)', 'amd')
.option('-x, --exclude <regex>', 'a regular expression for excluding modules')
.option('-d, --deps <filename>', 'when used with \'-f json\', a file to load deps from in {p1:[\'c1\',\'c2\']} format')
.parse(process.argv);

if (!Commander.args.length) {
console.log(Commander.helpInformation());
process.exit(1);

if (Commander.format !== "json" && !Commander.args.length) {
console.log(Commander.helpInformation());
process.exit(1);
}

var src = Commander.args[0];

var directDeps = Commander.format==='json' &&
fs.existsSync(Commander.deps) &&
JSON.parse(fs.readFileSync(Commander.deps));

var dependo = new Dependo(src, {
format: Commander.format,
exclude: Commander.exclude
exclude: Commander.exclude,
directDeps: directDeps
});

var html = dependo.generateHtml();
Expand Down
10 changes: 7 additions & 3 deletions lib/dependo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ var madge = require('madge');
var sha1 = require('sha-1');

function Dependo(targetPath, options) {

this.config = options || {};
this.config.format = String(options.format || 'amd').toLowerCase();
this.config.exclude = options.exclude || null;
this.identification = sha1(targetPath + JSON.stringify(this.config)) || ~~(Math.random()*999999999);

this.dependencies = madge(targetPath, this.config).tree;
if (this.config.format==='json') {
this.dependencies = this.config.directDeps;
} else {
this.dependencies = madge(targetPath, this.config).tree;
}

if (this.config.transform && typeof (this.config.transform) == 'function') {
this.dependencies = this.config.transform(this.dependencies);
Expand All @@ -22,4 +26,4 @@ Dependo.prototype.generateHtml = function () {
return require('./html').output(this.dependencies, this.identification);
};

module.exports = Dependo;
module.exports = Dependo;
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Options

```JavaScript
{
'format': The module format to expect, 'cjs', 'amd', or 'es6'. AMD (amd) is the default format.
'format': The module format to expect, 'cjs', 'amd', 'es6', or 'json'. AMD (amd) is the default format. If 'json', use the -d/--deps option to provide a JSON file
'optimized': Boolean, True if the parser should read modules from a optimized file (r.js). Defaults to false.
'exclude': String from which a regex will be constructed for excluding files from the scan.
'mainRequireModule': Name of the module if parsing an optimized file (r.js), where the main file used require() instead of define. Defaults to ''.
Expand Down Expand Up @@ -84,7 +84,8 @@ CLI

-h, --help output usage information
-V, --version output the version number
-f, --format <name> format to parse (amd/cjs)
-f, --format <name> format to parse (amd/cjs/es6/json)
-d, --deps <file> the file from which to read a JSON set of dependencies (only for -f json)
-x, --exclude <regex> a regular expression for excluding modules

### Generate HTML report of all module dependencies (AMD), and save it to /example/report.html
Expand All @@ -93,7 +94,7 @@ CLI

Grunt
-----
I also wrote a grunt-task that can be found in this seperate repository: https://github.com/auchenberg/grunt-dependo
I also wrote a grunt-task that can be found in this separate repository: https://github.com/auchenberg/grunt-dependo

Roadmap
-------
Expand All @@ -104,7 +105,7 @@ dependo is still very much in progress, so here is the todo-list:

Thanks to
-----------
This project would'nt have been possible without the great work on [node-madge](https://github.com/pahen/node-madge/) by Patrik Henningson, or wonderful [D3.js](http://d3js.org/) library.
This project wouldn't have been possible without the great work on [node-madge](https://github.com/pahen/node-madge/) by Patrik Henningson, or wonderful [D3.js](http://d3js.org/) library.


Inspiration
Expand Down

0 comments on commit 64befee

Please sign in to comment.