Skip to content

Commit fd21411

Browse files
authored
Merge pull request #80 from realityking/es2015
Make use of ES2015 features
2 parents fd80706 + b0efa59 commit fd21411

File tree

4 files changed

+64
-56
lines changed

4 files changed

+64
-56
lines changed

bin/cli.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
'use strict';
44

5-
var dependencyTree = require('../');
6-
var program = require('commander');
5+
const dependencyTree = require('../');
6+
const program = require('commander');
77

88
program
99
.version(require('../package.json').version)
@@ -14,9 +14,9 @@ program
1414
.option('--list-form', 'output the list form of the tree (one element per line)')
1515
.parse(process.argv);
1616

17-
var tree;
17+
let tree;
1818

19-
var options = {
19+
const options = {
2020
filename: program.args[0],
2121
root: program.directory,
2222
config: program.requireConfig,

index.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
var precinct = require('precinct');
2-
var path = require('path');
3-
var fs = require('fs');
4-
var cabinet = require('filing-cabinet');
5-
var debug = require('debug')('tree');
6-
var Config = require('./lib/Config');
1+
'use strict';
2+
3+
const precinct = require('precinct');
4+
const path = require('path');
5+
const fs = require('fs');
6+
const cabinet = require('filing-cabinet');
7+
const debug = require('debug')('tree');
8+
const Config = require('./lib/Config');
79

810
/**
911
* Recursively find all dependencies (avoiding circular) traversing the entire dependency tree
@@ -22,20 +24,20 @@ var Config = require('./lib/Config');
2224
* @return {Object}
2325
*/
2426
module.exports = function(options) {
25-
var config = new Config(options);
27+
const config = new Config(options);
2628

2729
if (!fs.existsSync(config.filename)) {
2830
debug('file ' + config.filename + ' does not exist');
2931
return config.isListForm ? [] : {};
3032
}
3133

32-
var results = traverse(config);
34+
const results = traverse(config);
3335
debug('traversal complete', results);
3436

3537
dedupeNonExistent(config.nonExistent);
3638
debug('deduped list of nonExistent partials: ', config.nonExistent);
3739

38-
var tree;
40+
let tree;
3941
if (config.isListForm) {
4042
debug('list form of results requested');
4143

@@ -80,8 +82,8 @@ module.exports.toList = function(options) {
8082
* @return {Array}
8183
*/
8284
module.exports._getDependencies = function(config) {
83-
var dependencies;
84-
var precinctOptions = config.detectiveConfig;
85+
let dependencies;
86+
const precinctOptions = config.detectiveConfig;
8587
precinctOptions.includeCore = false;
8688

8789
try {
@@ -95,12 +97,12 @@ module.exports._getDependencies = function(config) {
9597
return [];
9698
}
9799

98-
var resolvedDependencies = [];
100+
const resolvedDependencies = [];
99101

100-
for (var i = 0, l = dependencies.length; i < l; i++) {
101-
var dep = dependencies[i];
102+
for (let i = 0, l = dependencies.length; i < l; i++) {
103+
const dep = dependencies[i];
102104

103-
var result = cabinet({
105+
const result = cabinet({
104106
partial: dep,
105107
filename: config.filename,
106108
directory: config.directory,
@@ -116,7 +118,7 @@ module.exports._getDependencies = function(config) {
116118
continue;
117119
}
118120

119-
var exists = fs.existsSync(result);
121+
const exists = fs.existsSync(result);
120122

121123
if (!exists) {
122124
config.nonExistent.push(dep);
@@ -135,7 +137,7 @@ module.exports._getDependencies = function(config) {
135137
* @return {Object|String[]}
136138
*/
137139
function traverse(config) {
138-
var subTree = config.isListForm ? [] : {};
140+
let subTree = config.isListForm ? [] : {};
139141

140142
debug('traversing ' + config.filename);
141143

@@ -144,7 +146,7 @@ function traverse(config) {
144146
return config.visited[config.filename];
145147
}
146148

147-
var dependencies = module.exports._getDependencies(config);
149+
let dependencies = module.exports._getDependencies(config);
148150

149151
debug('cabinet-resolved all dependencies: ', dependencies);
150152
// Prevents cycles by eagerly marking the current file as read
@@ -160,9 +162,9 @@ function traverse(config) {
160162
debug('filtered number of dependencies: ' + dependencies.length);
161163
}
162164

163-
for (var i = 0, l = dependencies.length; i < l; i++) {
164-
var d = dependencies[i];
165-
var localConfig = config.clone();
165+
for (let i = 0, l = dependencies.length; i < l; i++) {
166+
const d = dependencies[i];
167+
const localConfig = config.clone();
166168
localConfig.filename = d;
167169

168170
if (localConfig.isListForm) {
@@ -192,13 +194,13 @@ function traverse(config) {
192194
* @return {String[]}
193195
*/
194196
function removeDups(list) {
195-
var cache = {};
196-
var unique = [];
197+
const cache = new Set();
198+
const unique = [];
197199

198200
list.forEach(function(item) {
199-
if (!cache[item]) {
201+
if (!cache.has(item)) {
200202
unique.push(item);
201-
cache[item] = true;
203+
cache.add(item);
202204
}
203205
});
204206

@@ -207,10 +209,10 @@ function removeDups(list) {
207209

208210
// Mutate the list input to do a dereferenced modification of the user-supplied list
209211
function dedupeNonExistent(nonExistent) {
210-
var deduped = removeDups(nonExistent);
212+
const deduped = removeDups(nonExistent);
211213
nonExistent.length = deduped.length;
212214

213-
for (var i = 0, l = deduped.length; i < l; i++) {
215+
for (let i = 0, l = deduped.length; i < l; i++) {
214216
nonExistent[i] = deduped[i];
215217
}
216218
}

lib/Config.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
var path = require('path');
2-
var debug = require('debug')('tree');
1+
'use strict';
32

4-
function Config(options) {
5-
this.filename = options.filename;
6-
this.directory = options.directory || options.root;
7-
this.visited = options.visited || {};
8-
this.nonExistent = options.nonExistent || [];
9-
this.isListForm = options.isListForm;
10-
this.requireConfig = options.config || options.requireConfig;
11-
this.webpackConfig = options.webpackConfig;
12-
this.nodeModulesConfig = options.nodeModulesConfig;
13-
this.detectiveConfig = options.detective || options.detectiveConfig || {};
3+
const path = require('path');
4+
const debug = require('debug')('tree');
145

15-
this.filter = options.filter;
6+
class Config {
7+
constructor(options) {
8+
this.filename = options.filename;
9+
this.directory = options.directory || options.root;
10+
this.visited = options.visited || {};
11+
this.nonExistent = options.nonExistent || [];
12+
this.isListForm = options.isListForm;
13+
this.requireConfig = options.config || options.requireConfig;
14+
this.webpackConfig = options.webpackConfig;
15+
this.nodeModulesConfig = options.nodeModulesConfig;
16+
this.detectiveConfig = options.detective || options.detectiveConfig || {};
1617

17-
if (!this.filename) { throw new Error('filename not given'); }
18-
if (!this.directory) { throw new Error('directory not given'); }
19-
if (this.filter && typeof this.filter !== 'function') { throw new Error('filter must be a function'); }
18+
this.filter = options.filter;
2019

21-
debug('given filename: ' + this.filename);
20+
if (!this.filename) { throw new Error('filename not given'); }
21+
if (!this.directory) { throw new Error('directory not given'); }
22+
if (this.filter && typeof this.filter !== 'function') { throw new Error('filter must be a function'); }
2223

23-
this.filename = path.resolve(process.cwd(), this.filename);
24+
debug('given filename: ' + this.filename);
2425

25-
debug('resolved filename: ' + this.filename);
26-
debug('visited: ', this.visited);
27-
}
26+
this.filename = path.resolve(process.cwd(), this.filename);
27+
28+
debug('resolved filename: ' + this.filename);
29+
debug('visited: ', this.visited);
30+
}
2831

29-
Config.prototype.clone = function() {
30-
return new Config(this);
31-
};
32+
clone () {
33+
return new Config(this);
34+
}
35+
}
3236

3337
module.exports = Config;

webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
'use strict';
2+
13
module.exports = {
24
entry: './index.js',
35
resolve: {
46
alias: {
57
F: './node_modules/filing-cabinet'
68
}
79
}
8-
};
10+
};

0 commit comments

Comments
 (0)