diff --git a/.eslintrc b/.eslintrc index 9a7a71af..9b7b8352 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,8 @@ { - "extends": "@aptoma/eslint-config/lib/es5", + "extends": "@aptoma/eslint-config", "env": { "node": true, - "mocha": true + "mocha": true, + "es6": true } } \ No newline at end of file diff --git a/README.md b/README.md index 29861e05..72c3f491 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ Get an image representation of the module dependency graph. -V, --version output the version number -f, --format format to parse (amd/cjs/es6) -s, --summary show summary of all dependencies + -L, --list show list of all dependencies -c, --circular show circular dependencies -d, --depends show modules that depends on the given id -x, --exclude a regular expression for excluding modules diff --git a/bin/madge b/bin/madge index d022aa70..8f482bb0 100755 --- a/bin/madge +++ b/bin/madge @@ -5,16 +5,17 @@ /** * Module dependencies */ -var fs = require('fs'), - version = require('../package.json').version, - program = require('commander'), - printResult = require('../lib/print'), - madge = require('../lib/madge'); +const fs = require('fs'); +const version = require('../package.json').version; +const program = require('commander'); +const printResult = require('../lib/print'); +const madge = require('../lib/madge'); program .version(version) .usage('[options] ') .option('-f, --format ', 'format to parse (amd/cjs/es6)', 'cjs') + .option('-L, --list', 'show list of all dependencies (default)') .option('-s, --summary', 'show summary of all dependencies') .option('-c, --circular', 'show circular dependencies') .option('-d, --depends ', 'show modules that depends on the given id') @@ -40,14 +41,14 @@ if (!program.args.length && !program.read && !program.requireConfig) { process.exit(1); } -var src = program.args; +let src = program.args; // Check config file -if (program.config && fs.existsSync(program.config)) { - var configOptions = JSON.parse(fs.readFileSync(program.config, 'utf8')); +if (program.config && fs.existsSync(program.config)) { // eslint-disable-line no-sync + const configOptions = JSON.parse(fs.readFileSync(program.config, 'utf8')); // eslint-disable-line no-sync // Duck punch the program with the new options // Config file take precedence - for (var k in configOptions) { + for (const k in configOptions) { if (configOptions.hasOwnProperty(k)) { program[k] = configOptions[k]; } @@ -56,13 +57,13 @@ if (program.config && fs.existsSync(program.config)) { // Read from standard input if (program.read) { - var buffer = ''; + let buffer = ''; process.stdin.resume(); process.stdin.setEncoding('utf8'); - process.stdin.on('data', function (chunk) { + process.stdin.on('data', (chunk) => { buffer += chunk; }); - process.stdin.on('end', function () { + process.stdin.on('end', () => { src = JSON.parse(buffer); run(); }); @@ -71,8 +72,8 @@ if (program.read) { } function run() { - // Start parsing - var res = madge(src, { + // Start parsing + const res = madge(src, { format: program.format, breakOnError: program.breakOnError, exclude: program.exclude, @@ -80,7 +81,7 @@ function run() { requireConfig: program.requireConfig, mainRequireModule: program.mainRequireModule, paths: program.paths ? program.paths.split(',') : undefined, - extensions: program.extensions.split(',').map(function (str) { return '.' + str; }), + extensions: program.extensions.split(',').map((str) => '.' + str), findNestedDependencies: program.findNestedDependencies }); @@ -90,45 +91,53 @@ function run() { colors: program.colors, output: program.output }); + } // Output circular dependencies - } else if (program.circular) { + if (program.circular) { printResult.circular(res.circular(), { colors: program.colors, output: program.output }); + } // Output module dependencies - } else if (program.depends) { + if (program.depends) { printResult.depends(res.depends(program.depends), { colors: program.colors, output: program.output }); + } // Write image - } else if (program.image) { + if (program.image) { res.image({ colors: program.colors, layout: program.layout, fontFace: program.font, fontSize: program.fontSize, imageColors: program.imageColors - }, function (image) { - fs.writeFile(program.image, image, function (err) { + }, (image) => { + fs.writeFile(program.image, image, (err) => { if (err) { throw err; } }); }); + } // Output DOT - } else if (program.dot) { + if (program.dot) { process.stdout.write(res.dot()); + } + // Output JSON - } else if(program.json) { + if (program.json) { process.stdout.write(JSON.stringify(res.tree) + '\n'); + } + // Output text (default) - } else { + if (program.list || (!program.summary && !program.circular && !program.depends && !program.image && !program.dot && !program.json)) { printResult.list(res.obj(), { colors: program.colors, output: program.output diff --git a/lib/color.js b/lib/color.js deleted file mode 100644 index a5af2277..00000000 --- a/lib/color.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -/** - * Module dependencies. - */ -require('colors'); - -/** - * Return colored string (or not). - * @param {String} str - * @param {String} name - * @param {Boolean} use - * @return {String} - */ -module.exports = function (str, name, use) { - if (!use) { - return str; - } - return String(str)[name]; -}; diff --git a/lib/cyclic.js b/lib/cyclic.js index dbac0906..a4acfcdf 100644 --- a/lib/cyclic.js +++ b/lib/cyclic.js @@ -7,9 +7,9 @@ * @return {Array} */ function getPath(parent, unresolved) { - var parentVisited = false; + let parentVisited = false; - return Object.keys(unresolved).filter(function (module) { + return Object.keys(unresolved).filter((module) => { if (module === parent) { parentVisited = true; } @@ -30,7 +30,7 @@ function resolver(id, modules, circular, resolved, unresolved) { unresolved[id] = true; if (modules[id]) { - modules[id].forEach(function (dependency) { + modules[id].forEach((dependency) => { if (!resolved[dependency]) { if (unresolved[dependency]) { circular.push(getPath(dependency, unresolved)); @@ -51,11 +51,11 @@ function resolver(id, modules, circular, resolved, unresolved) { * @return {Object} */ module.exports = function (modules) { - var circular = []; - var resolved = {}; - var unresolved = {}; + const circular = []; + const resolved = {}; + const unresolved = {}; - Object.keys(modules).forEach(function (id) { + Object.keys(modules).forEach((id) => { resolver(id, modules, circular, resolved, unresolved); }); @@ -64,7 +64,7 @@ module.exports = function (modules) { * Expose the circular dependency array. * @return {Array} */ - getArray: function () { + getArray() { return circular; }, @@ -73,9 +73,9 @@ module.exports = function (modules) { * @param {String} id * @return {Boolean} */ - isCyclic: function (id) { - var cyclic = false; - circular.forEach(function (path) { + isCyclic(id) { + let cyclic = false; + circular.forEach((path) => { if (path.indexOf(id) >= 0) { cyclic = true; } diff --git a/lib/graph.js b/lib/graph.js index 8243319f..3110d57e 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -3,9 +3,9 @@ /** * Module dependencies. */ -var exec = require('child_process').exec; -var cyclic = require('./cyclic'); -var graphviz = require('graphviz'); +const exec = require('child_process').exec; +const cyclic = require('./cyclic'); +const graphviz = require('graphviz'); /** * Set color on a node. @@ -31,7 +31,7 @@ function noDependencyNode(node, color) { * @throws Error */ function checkGraphvizInstalled() { - exec('gvpr -V', function (error, stdout, stderr) { + exec('gvpr -V', (error, stdout, stderr) => { if (error !== null) { throw new Error('Graphviz could not be found. Ensure that "gvpr" is in your $PATH.\n' + error); } @@ -45,18 +45,18 @@ function checkGraphvizInstalled() { */ function createGraphvizOptions(opts) { // Valid attributes: http://www.graphviz.org/doc/info/attrs.html - var G = { + const G = { layout: opts.layout || 'dot', overlap: false, bgcolor: '#ffffff' }; - var N = { + const N = { fontname: opts.fontFace || 'Times-Roman', fontsize: opts.fontSize || 14 }; - var E = {}; + const E = {}; if (opts.colors) { G.bgcolor = opts.imageColors.bgcolor || '#000000'; @@ -80,16 +80,16 @@ function createGraphvizOptions(opts) { * @param {Function} callback */ module.exports.image = function (modules, opts, callback) { - var g = graphviz.digraph('G'); + const g = graphviz.digraph('G'); + const nodes = {}; checkGraphvizInstalled(); opts.imageColors = opts.imageColors || {}; - var nodes = {}; - var cyclicResults = cyclic(modules); + const cyclicResults = cyclic(modules); - Object.keys(modules).forEach(function (id) { + Object.keys(modules).forEach((id) => { nodes[id] = nodes[id] || g.addNode(id); if (opts.colors && modules[id]) { @@ -100,7 +100,7 @@ module.exports.image = function (modules, opts, callback) { } } - modules[id].forEach(function (depId) { + modules[id].forEach((depId) => { nodes[depId] = nodes[depId] || g.addNode(depId); if (opts.colors && !modules[depId]) { noDependencyNode(nodes[depId], opts.imageColors.noDependencies); @@ -118,15 +118,15 @@ module.exports.image = function (modules, opts, callback) { * @return {String} */ module.exports.dot = function (modules) { - var nodes = {}; - var g = graphviz.digraph('G'); + const nodes = {}; + const g = graphviz.digraph('G'); checkGraphvizInstalled(); - Object.keys(modules).forEach(function (id) { + Object.keys(modules).forEach((id) => { nodes[id] = nodes[id] || g.addNode(id); - modules[id].forEach(function (depId) { + modules[id].forEach((depId) => { nodes[depId] = nodes[depId] || g.addNode(depId); g.addEdge(nodes[id], nodes[depId]); }); diff --git a/lib/madge.js b/lib/madge.js index 7e92335e..75efb795 100644 --- a/lib/madge.js +++ b/lib/madge.js @@ -3,11 +3,11 @@ /** * Module dependencies. */ -var cyclic = require('./cyclic'); -var CJS = require('./parse/cjs'); -var AMD = require('./parse/amd'); -var ES6 = require('./parse/es6'); -var graph = require('./graph'); +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. @@ -28,7 +28,7 @@ module.exports = function (src, opts) { * @param {Object} opts */ function Madge(src, opts) { - var tree = []; + let tree = []; this.opts = opts || {}; this.opts.format = String(this.opts.format || 'cjs').toLowerCase(); @@ -91,9 +91,9 @@ Madge.prototype.circular = function () { * @return {Array|Object} */ Madge.prototype.depends = function (id) { - return Object.keys(this.tree).filter(function (module) { + return Object.keys(this.tree).filter((module) => { if (this.tree[module]) { - return this.tree[module].reduce(function (acc, dependency) { + return this.tree[module].reduce((acc, dependency) => { if (dependency === id) { acc = module; } diff --git a/lib/parse/amd.js b/lib/parse/amd.js index 53032537..923753b9 100644 --- a/lib/parse/amd.js +++ b/lib/parse/amd.js @@ -3,12 +3,12 @@ /** * Module dependencies. */ -var fs = require('fs'); -var path = require('path'); -var util = require('util'); -var amdetective = require('amdetective'); -var parse = require('./parse'); -var Base = require('./base'); +const fs = require('fs'); +const path = require('path'); +const util = require('util'); +const amdetective = require('amdetective'); +const parse = require('./parse'); +const Base = require('./base'); /** * Merge the two given trees. @@ -16,12 +16,12 @@ var Base = require('./base'); * @param {Object} b */ function mergeTrees(a, b) { - Object.keys(b).forEach(function (id) { + Object.keys(b).forEach((id) => { if (!a[id]) { a[id] = []; } - b[id].forEach(function (dep) { + b[id].forEach((dep) => { if (a[id].indexOf(dep) < 0) { a[id].push(dep); } @@ -36,7 +36,7 @@ function mergeTrees(a, b) { * @param {String} baseDir (base directory of source files) */ function convertPathsToIds(deps, pathDefs, baseDir) { - var path, pathDeps, i1, len1, i2, len2; + let path, pathDeps, i1, len1, i2, len2; if (baseDir) { baseDir += '/'; @@ -44,7 +44,7 @@ function convertPathsToIds(deps, pathDefs, baseDir) { baseDir = ''; } - Object.keys(pathDefs).forEach(function (id) { + Object.keys(pathDefs).forEach((id) => { path = pathDefs[id]; // if path does not start with / or a protocol: prepend with baseDir @@ -76,7 +76,7 @@ function convertPathsToIds(deps, pathDefs, baseDir) { } // normalize entries within deps-arrays (i.e. replace path-refs with id-refs) - Object.keys(pathDefs).forEach(function (id) { + Object.keys(pathDefs).forEach((id) => { path = baseDir + pathDefs[id]; if (deps[id]) { for (i1 = 0, len1 = deps[id].length; i1 < len1; ++i1) { @@ -97,15 +97,15 @@ function convertPathsToIds(deps, pathDefs, baseDir) { * @return {Object} */ function getShimDepsFromConfig(filename, exclude) { - var deps = {}; - var config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); // eslint-disable-line no-sync - var excludeRegex = exclude ? new RegExp(exclude) : false; - var isIncluded = function (key) { + const deps = {}; + const config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); // eslint-disable-line no-sync + const excludeRegex = exclude ? new RegExp(exclude) : false; + const isIncluded = function (key) { return !(excludeRegex && key.match(excludeRegex)); }; if (config.shim) { - Object.keys(config.shim).filter(isIncluded).forEach(function (key) { + Object.keys(config.shim).filter(isIncluded).forEach((key) => { if (config.shim[key].deps) { deps[key] = config.shim[key].deps.filter(isIncluded); } else { @@ -124,15 +124,15 @@ function getShimDepsFromConfig(filename, exclude) { * @return {Object} */ function getPathsFromConfig(filename, exclude) { - var paths = {}; - var config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); // eslint-disable-line no-sync - var excludeRegex = exclude ? new RegExp(exclude) : false; - var isIncluded = function (key) { + const paths = {}; + const config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); // eslint-disable-line no-sync + const excludeRegex = exclude ? new RegExp(exclude) : false; + const isIncluded = function (key) { return !(excludeRegex && key.match(excludeRegex)); }; if (config.paths) { - Object.keys(config.paths).filter(isIncluded).forEach(function (key) { + Object.keys(config.paths).filter(isIncluded).forEach((key) => { paths[key] = config.paths[key]; }); } @@ -147,7 +147,7 @@ function getPathsFromConfig(filename, exclude) { * @return {String} */ function getBaseUrlFromConfig(filename, srcBaseDir) { - var config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); // eslint-disable-line no-sync + const config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); // eslint-disable-line no-sync return config.baseUrl ? path.relative(srcBaseDir, config.baseUrl) : ''; } @@ -159,11 +159,11 @@ function getBaseUrlFromConfig(filename, srcBaseDir) { * @param {Object} opts * @param {Object} parent */ -var AMD = module.exports = function (src, opts, parent) { +const AMD = module.exports = function (src, opts, parent) { Base.apply(this, arguments); if (opts.requireConfig) { - var baseDir = src.length ? src[0].replace(/\\/g, '/') : ''; + let baseDir = src.length ? src[0].replace(/\\/g, '/') : ''; baseDir = getBaseUrlFromConfig(opts.requireConfig, baseDir); convertPathsToIds(this.tree, getPathsFromConfig(opts.requireConfig, opts.exclude), baseDir); mergeTrees(this.tree, getShimDepsFromConfig(opts.requireConfig, opts.exclude)); @@ -191,31 +191,31 @@ AMD.prototype.normalize = function (filename) { */ AMD.prototype.parseFile = function (filename) { try { - var dependencies = []; - var src = this.getFileSource(filename); - var fileData = {filename: filename, src: src}; + const dependencies = []; + const src = this.getFileSource(filename); + const fileData = {filename: filename, src: src}; this.emit('parseFile', fileData); if (/define|require\s*\(/m.test(fileData.src)) { - amdetective(fileData.src, {findNestedDependencies: this.opts.findNestedDependencies}).map(function (obj) { + amdetective(fileData.src, {findNestedDependencies: this.opts.findNestedDependencies}).map((obj) => { return typeof obj === 'string' ? [obj] : obj.deps; - }).filter(function (deps) { - deps.filter(function (id) { + }).filter((deps) => { + deps.filter((id) => { // Ignore RequireJS IDs and plugins return id !== 'require' && id !== 'exports' && id !== 'module' && !id.match(/\.?\w\!/); - }).map(function (id) { + }).map((id) => { // Only resolve relative module identifiers (if the first term is "." or "..") if (id.charAt(0) !== '.') { return id; } - var depFilename = path.resolve(path.dirname(fileData.filename), id); + const depFilename = path.resolve(path.dirname(fileData.filename), id); if (depFilename) { return this.normalize(depFilename); } - }, this).forEach(function (id) { + }, this).forEach((id) => { if (!this.isExcluded(id) && dependencies.indexOf(id) < 0) { dependencies.push(id); } @@ -239,23 +239,22 @@ AMD.prototype.parseFile = function (filename) { * @param {String} filename */ AMD.prototype.addOptimizedModules = function (filename) { - var self = this; - var anonymousRequire = []; + const anonymousRequire = []; amdetective(this.getFileSource(filename)) - .filter(function (obj) { - var id = obj.name || obj; - return id !== 'require' && id !== 'exports' && id !== 'module' && !id.match(/\.?\w\!/) && !self.isExcluded(id); + .filter((obj) => { + const id = obj.name || obj; + return id !== 'require' && id !== 'exports' && id !== 'module' && !id.match(/\.?\w\!/) && !this.isExcluded(id); }) - .forEach(function (obj) { + .forEach((obj) => { if (typeof obj === 'string') { anonymousRequire.push(obj); return; } - if (!self.isExcluded(obj.name)) { - self.tree[obj.name] = obj.deps.filter(function (id) { - return id !== 'require' && id !== 'exports' && id !== 'module' && !id.match(/\.?\w\!/) && !self.isExcluded(id); + if (!this.isExcluded(obj.name)) { + this.tree[obj.name] = obj.deps.filter((id) => { + return id !== 'require' && id !== 'exports' && id !== 'module' && !id.match(/\.?\w\!/) && !this.isExcluded(id); }); } }); diff --git a/lib/parse/base.js b/lib/parse/base.js index e87a9d39..10d246fe 100644 --- a/lib/parse/base.js +++ b/lib/parse/base.js @@ -3,14 +3,14 @@ /** * Module dependencies */ -var fs = require('fs'); -var path = require('path'); -var util = require('util'); -var resolve = require('resolve'); -var EventEmitter = require('events').EventEmitter; -var commondir = require('commondir'); -var finder = require('walkdir'); -var coffee = require('coffee-script'); +const fs = require('fs'); +const path = require('path'); +const util = require('util'); +const resolve = require('resolve'); +const EventEmitter = require('events').EventEmitter; +const commondir = require('commondir'); +const finder = require('walkdir'); +const coffee = require('coffee-script'); /** * Traversing `src` and fetches all dependencies. @@ -19,7 +19,7 @@ var coffee = require('coffee-script'); * @param {Object} opts * @param {Object} parent */ -var Base = module.exports = function (src, opts, parent) { +const Base = module.exports = function (src, opts, parent) { if (opts.onParseFile) { this.on('parseFile', opts.onParseFile.bind(parent)); } @@ -35,7 +35,7 @@ var Base = module.exports = function (src, opts, parent) { } this.tree = {}; - this.extRegEx = new RegExp('\\.(coffee|jsx|' + this.opts.extensions.map(function (str) { + this.extRegEx = new RegExp('\\.(coffee|jsx|' + this.opts.extensions.map((str) => { return str.substring(1); }).join('|') + ')$', 'g'); this.coffeeExtRegEx = /\.coffee$/; @@ -76,11 +76,12 @@ Base.prototype.resolve = function (dir, id) { * @return {String} */ Base.prototype.getBaseDir = function (src) { - var dir = commondir(src); + const dir = commondir(src); if (!fs.statSync(dir).isDirectory()) { // eslint-disable-line no-sync - dir = path.dirname(dir); + return path.dirname(dir); } + return dir; }; @@ -90,9 +91,7 @@ Base.prototype.getBaseDir = function (src) { * @return {Array} */ Base.prototype.resolveTargets = function (sources) { - return sources.map(function (src) { - return path.resolve(src); - }); + return sources.map((src) => path.resolve(src)); }; /** @@ -118,7 +117,7 @@ Base.prototype.isExcluded = function (id) { * @param {String} filename */ Base.prototype.addModule = function (filename) { - var id = this.normalize(filename); + const id = this.normalize(filename); if (!this.isExcluded(id) && fs.existsSync(filename)) { // eslint-disable-line no-sync this.tree[id] = this.parseFile(filename); @@ -131,11 +130,11 @@ Base.prototype.addModule = function (filename) { * @param {Array} sources */ Base.prototype.readFiles = function (sources) { - sources.forEach(function (src) { + sources.forEach((src) => { if (fs.statSync(src).isDirectory()) { // eslint-disable-line no-sync - finder.sync(src).filter(function (filename) { + finder.sync(src).filter((filename) => { return filename.match(this.extRegEx); - }, this).forEach(function (filename) { + }, this).forEach((filename) => { this.addModule(filename); }, this); } else { @@ -150,7 +149,7 @@ Base.prototype.readFiles = function (sources) { * @return {String} */ Base.prototype.getFileSource = function (filename) { - var src = fs.readFileSync(filename, 'utf8'); // eslint-disable-line no-sync + const src = fs.readFileSync(filename, 'utf8'); // eslint-disable-line no-sync if (filename.match(this.coffeeExtRegEx)) { return coffee.compile(src, { @@ -166,10 +165,8 @@ Base.prototype.getFileSource = function (filename) { * Sort dependencies by name. */ Base.prototype.sortDependencies = function () { - var self = this; - - this.tree = Object.keys(this.tree).sort().reduce(function (acc, id) { - (acc[id] = self.tree[id]).sort(); + this.tree = Object.keys(this.tree).sort().reduce((acc, id) => { + (acc[id] = this.tree[id]).sort(); return acc; }, {}); }; diff --git a/lib/parse/cjs.js b/lib/parse/cjs.js index db1b7966..a26bfd87 100644 --- a/lib/parse/cjs.js +++ b/lib/parse/cjs.js @@ -3,18 +3,18 @@ /** * Module dependencies. */ -var fs = require('fs'); -var path = require('path'); -var util = require('util'); -var detective = require('detective'); -var Base = require('./base'); +const fs = require('fs'); +const path = require('path'); +const util = require('util'); +const detective = require('detective'); +const Base = require('./base'); /** * This class will parse the CommonJS module format. * @see http://nodejs.org/api/modules.html * @constructor */ -var CJS = module.exports = function () { +const CJS = module.exports = function () { Base.apply(this, arguments); }; @@ -45,19 +45,19 @@ CJS.prototype.normalize = function (filename) { CJS.prototype.parseFile = function (filename) { try { if (fs.existsSync(filename)) { // eslint-disable-line no-sync - var dependencies = []; - var src = this.getFileSource(filename); - var fileData = {filename: filename, src: src}; + const dependencies = []; + const src = this.getFileSource(filename); + const fileData = {filename: filename, src: src}; this.emit('parseFile', fileData); if (/require\s*\(/m.test(fileData.src)) { - detective(fileData.src).map(function (id) { - var depFilename = this.resolve(path.dirname(fileData.filename), id); + detective(fileData.src).map((id) => { + const depFilename = this.resolve(path.dirname(fileData.filename), id); if (depFilename) { return this.normalize(depFilename); } - }, this).filter(function (id) { + }, this).filter((id) => { if (!this.isExcluded(id) && dependencies.indexOf(id) < 0) { dependencies.push(id); } diff --git a/lib/parse/es6.js b/lib/parse/es6.js index 4fd63d3a..e2370b5d 100644 --- a/lib/parse/es6.js +++ b/lib/parse/es6.js @@ -3,18 +3,18 @@ /** * Module dependencies. */ -var fs = require('fs'); -var path = require('path'); -var util = require('util'); -var detective = require('detective-es6'); -var Base = require('./base'); +const fs = require('fs'); +const path = require('path'); +const util = require('util'); +const detective = require('detective-es6'); +const Base = require('./base'); /** * This class will parse the ES6 module format. * @see http://nodejs.org/api/modules.html * @constructor */ -var ES6 = module.exports = function () { +const ES6 = module.exports = function () { Base.apply(this, arguments); }; @@ -31,19 +31,19 @@ util.inherits(ES6, Base); ES6.prototype.parseFile = function (filename) { try { if (fs.existsSync(filename)) { // eslint-disable-line no-sync - var dependencies = []; - var src = this.getFileSource(filename); - var fileData = {filename: filename, src: src}; + const dependencies = []; + const src = this.getFileSource(filename); + const fileData = {filename: filename, src: src}; this.emit('parseFile', fileData); if (/import.*from/m.test(fileData.src) || /export.*from/m.test(fileData.src)) { - detective(fileData.src).map(function (id) { - var depFilename = this.resolve(path.dirname(fileData.filename), id); + detective(fileData.src).map((id) => { + const depFilename = this.resolve(path.dirname(fileData.filename), id); if (depFilename) { return this.normalize(depFilename); } - }, this).filter(function (id) { + }, this).filter((id) => { if (!this.isExcluded(id) && dependencies.indexOf(id) < 0) { dependencies.push(id); } diff --git a/lib/print.js b/lib/print.js index 52cb06a1..07c91f68 100644 --- a/lib/print.js +++ b/lib/print.js @@ -3,7 +3,18 @@ /** * Module dependencies. */ -var c = require('./color'); +require('colors'); + +/** + * Return colored string (or not). + * @param {String} str + * @param {String} name + * @param {Boolean} use + * @return {String} + */ +function c(str, name, use) { + return use ? String(str)[name] : str; +} /** * Return the given object as JSON. @@ -27,9 +38,9 @@ module.exports.list = function (modules, opts) { return process.stdout.write(toJSON(modules)); } - Object.keys(modules).forEach(function (id) { + Object.keys(modules).forEach((id) => { console.log(c(id, 'cyan', opts.colors)); - modules[id].forEach(function (depId) { + modules[id].forEach((depId) => { console.log(c(' ' + depId, 'grey', opts.colors)); }, this); }, this); @@ -42,13 +53,13 @@ module.exports.list = function (modules, opts) { * @return {undefined} */ module.exports.summary = function (modules, opts) { - var o = {}; + const o = {}; opts = opts || {}; - Object.keys(modules).sort(function (a, b) { + Object.keys(modules).sort((a, b) => { return modules[b].length - modules[a].length; - }).forEach(function (id) { + }).forEach((id) => { if (opts.output === 'json') { o[id] = modules[id].length; } else { @@ -68,7 +79,8 @@ module.exports.summary = function (modules, opts) { * @return {undefined} */ module.exports.circular = function (circular, opts) { - var arr = circular.getArray(); + const arr = circular.getArray(); + if (opts.output === 'json') { return process.stdout.write(toJSON(arr)); } @@ -76,8 +88,8 @@ module.exports.circular = function (circular, opts) { if (!arr.length) { console.log(c('No circular dependencies found!', 'green', opts.colors)); } else { - arr.forEach(function (path, idx) { - path.forEach(function (module, idx) { + arr.forEach((path, idx) => { + path.forEach((module, idx) => { if (idx) { process.stdout.write(c(' -> ', 'cyan', opts.colors)); } @@ -100,7 +112,7 @@ module.exports.depends = function (modules, opts) { return process.stdout.write(toJSON(modules)); } - modules.forEach(function (id) { + modules.forEach((id) => { console.log(c(id, 'grey', opts.colors)); }); }; diff --git a/package.json b/package.json index a9252d38..6347a617 100644 --- a/package.json +++ b/package.json @@ -20,13 +20,13 @@ "graph" ], "engines": [ - "node >= 0.8.0" + "node >= 4.4.7" ], "scripts": { "test": "npm run lint && npm run mocha && npm run madge", "mocha": "mocha test/*.js", - "lint": "eslint bin lib", - "madge": "bin/madge -c -f cjs ./lib", + "lint": "eslint bin/madge lib", + "madge": "bin/madge -c -L ./lib", "release": "npm test && release-it -n -i patch", "release:minor": "npm run test && release-it -n -i minor", "release:major": "npm run test && release-it -n -i major"