Skip to content

Commit

Permalink
Use ES6 internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Jul 3, 2016
1 parent c4576e9 commit 6a0e2ca
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 183 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "@aptoma/eslint-config/lib/es5",
"extends": "@aptoma/eslint-config",
"env": {
"node": true,
"mocha": true
"mocha": true,
"es6": true
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Get an image representation of the module dependency graph.
-V, --version output the version number
-f, --format <name> 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 <id> show modules that depends on the given id
-x, --exclude <regex> a regular expression for excluding modules
Expand Down
55 changes: 32 additions & 23 deletions bin/madge
Original file line number Diff line number Diff line change
Expand Up @@ -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] <file|dir ...>')
.option('-f, --format <name>', '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 <id>', 'show modules that depends on the given id')
Expand All @@ -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];
}
Expand All @@ -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();
});
Expand All @@ -71,16 +72,16 @@ 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,
optimized: program.optimized,
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
});

Expand All @@ -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
Expand Down
20 changes: 0 additions & 20 deletions lib/color.js

This file was deleted.

22 changes: 11 additions & 11 deletions lib/cyclic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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));
Expand All @@ -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);
});

Expand All @@ -64,7 +64,7 @@ module.exports = function (modules) {
* Expose the circular dependency array.
* @return {Array}
*/
getArray: function () {
getArray() {
return circular;
},

Expand All @@ -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;
}
Expand Down
32 changes: 16 additions & 16 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand All @@ -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';
Expand All @@ -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]) {
Expand All @@ -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);
Expand All @@ -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]);
});
Expand Down
16 changes: 8 additions & 8 deletions lib/madge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 6a0e2ca

Please sign in to comment.