-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
439 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,113 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
const cyclic = require('./cyclic'); | ||
const CJS = require('./parse/cjs'); | ||
const AMD = require('./parse/amd'); | ||
const ES6 = require('./parse/es6'); | ||
const graph = require('./graph'); | ||
|
||
/** | ||
* Expose factory function. | ||
* @api public | ||
* @param {String|Array|Object} src | ||
* @param {Object} opts | ||
* @return {Madge} | ||
*/ | ||
module.exports = function (src, opts) { | ||
return new Madge(src, opts); | ||
}; | ||
class Madge { | ||
/** | ||
* Class constructor. | ||
* @constructor | ||
* @api public | ||
* @param {String|Array|Object} src | ||
* @param {Object} opts | ||
*/ | ||
constructor(src, opts) { | ||
let tree = []; | ||
|
||
/** | ||
* Class constructor. | ||
* @constructor | ||
* @api public | ||
* @param {String|Array|Object} src | ||
* @param {Object} opts | ||
*/ | ||
function Madge(src, opts) { | ||
let tree = []; | ||
this.opts = opts || {}; | ||
this.opts.format = String(this.opts.format || 'cjs').toLowerCase(); | ||
|
||
this.opts = opts || {}; | ||
this.opts.format = String(this.opts.format || 'cjs').toLowerCase(); | ||
if (typeof src === 'object' && !Array.isArray(src)) { | ||
this.tree = src; | ||
return; | ||
} | ||
|
||
if (typeof src === 'object' && !Array.isArray(src)) { | ||
this.tree = src; | ||
return; | ||
} | ||
if (typeof src === 'string') { | ||
src = [src]; | ||
} | ||
|
||
if (typeof src === 'string') { | ||
src = [src]; | ||
} | ||
if (src && src.length) { | ||
tree = this.parse(src); | ||
} | ||
|
||
if (src && src.length) { | ||
tree = this.parse(src); | ||
this.tree = tree; | ||
} | ||
|
||
this.tree = tree; | ||
} | ||
/** | ||
* Parse the given source folder(s). | ||
* @param {Array|Object} src | ||
* @return {Object} | ||
*/ | ||
parse(src) { | ||
if (this.opts.format === 'cjs') { | ||
return new CJS(src, this.opts, this).tree; | ||
} else if (this.opts.format === 'amd') { | ||
return new AMD(src, this.opts, this).tree; | ||
} else if (this.opts.format === 'es6') { | ||
return new ES6(src, this.opts, this).tree; | ||
} else { | ||
throw new Error('invalid module format "' + this.opts.format + '"'); | ||
} | ||
} | ||
|
||
/** | ||
* Parse the given source folder(s). | ||
* @param {Array|Object} src | ||
* @return {Object} | ||
*/ | ||
Madge.prototype.parse = function (src) { | ||
if (this.opts.format === 'cjs') { | ||
return new CJS(src, this.opts, this).tree; | ||
} else if (this.opts.format === 'amd') { | ||
return new AMD(src, this.opts, this).tree; | ||
} else if (this.opts.format === 'es6') { | ||
return new ES6(src, this.opts, this).tree; | ||
} else { | ||
throw new Error('invalid module format "' + this.opts.format + '"'); | ||
/** | ||
* Return the module dependency graph as an object. | ||
* @api public | ||
* @return {Object} | ||
*/ | ||
obj() { | ||
return this.tree; | ||
} | ||
}; | ||
|
||
/** | ||
* Return the module dependency graph as an object. | ||
* @api public | ||
* @return {Object} | ||
*/ | ||
Madge.prototype.obj = function () { | ||
return this.tree; | ||
}; | ||
/** | ||
* Return the modules that has circular dependencies. | ||
* @api public | ||
* @return {Object} | ||
*/ | ||
circular() { | ||
return cyclic(this.tree); | ||
} | ||
|
||
/** | ||
* Return the modules that has circular dependencies. | ||
* @api public | ||
* @return {Object} | ||
*/ | ||
Madge.prototype.circular = function () { | ||
return cyclic(this.tree); | ||
}; | ||
/** | ||
* Return a list of modules that depends on the given module. | ||
* @api public | ||
* @param {String} id | ||
* @return {Array|Object} | ||
*/ | ||
depends(id) { | ||
return Object.keys(this.tree).filter((module) => { | ||
if (this.tree[module]) { | ||
return this.tree[module].reduce((acc, dependency) => { | ||
if (dependency === id) { | ||
acc = module; | ||
} | ||
return acc; | ||
}, false); | ||
} | ||
}, this); | ||
} | ||
|
||
/** | ||
* Return a list of modules that depends on the given module. | ||
* @api public | ||
* @param {String} id | ||
* @return {Array|Object} | ||
*/ | ||
Madge.prototype.depends = function (id) { | ||
return Object.keys(this.tree).filter((module) => { | ||
if (this.tree[module]) { | ||
return this.tree[module].reduce((acc, dependency) => { | ||
if (dependency === id) { | ||
acc = module; | ||
} | ||
return acc; | ||
}, false); | ||
} | ||
}, this); | ||
}; | ||
/** | ||
* Return the module dependency graph as DOT output. | ||
* @api public | ||
* @return {String} | ||
*/ | ||
dot() { | ||
return graph.dot(this.tree); | ||
} | ||
|
||
/** | ||
* Return the module dependency graph as DOT output. | ||
* @api public | ||
* @return {String} | ||
*/ | ||
Madge.prototype.dot = function () { | ||
return graph.dot(this.tree); | ||
}; | ||
/** | ||
* Return the module dependency graph as a PNG image. | ||
* @api public | ||
* @param {Object} opts | ||
* @param {Function} callback | ||
*/ | ||
image(opts, callback) { | ||
graph.image(this.tree, opts, callback); | ||
} | ||
} | ||
|
||
/** | ||
* Return the module dependency graph as a PNG image. | ||
* @api public | ||
* @param {Object} opts | ||
* @param {Function} callback | ||
*/ | ||
Madge.prototype.image = function (opts, callback) { | ||
graph.image(this.tree, opts, callback); | ||
}; | ||
module.exports = (src, opts) => new Madge(src, opts); |
Oops, something went wrong.