Skip to content

Commit 5b190f2

Browse files
committed
Add directory pattern skipping command-line option
1 parent 753de20 commit 5b190f2

File tree

5 files changed

+75
-33
lines changed

5 files changed

+75
-33
lines changed

bin/jscr.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ argvParser.parse(process.argv)
1414

1515
.then(argvParser.getSpec)
1616
.then(function(specs){
17+
1718
var path = specs.targetedTree;
18-
return crawlComplexity(path, specs);
19+
var skipped = specs.skippedDirectory;
20+
21+
return crawlComplexity(path, skipped);
22+
1923
})
2024
.then(function(data){
2125

src/argv-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ program
1919
});
2020

2121
program
22-
.option('-e, --exclude [folder]', 'exclude folders', function(folder){
23-
options.skippedDirectories = folder.split(',');
22+
.option('-s, --skip <folder>', 'skip a folder', function(folder){
23+
options.skippedDirectory = folder;
2424
});
2525

2626
module.exports.parse = function(argv){

src/crawl-complexity.js

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
*
1515
* returns a Promise
1616
* rejected promise if not .js
17+
*
1718
* fulfilled promise returns the passed file reference (path)
1819
*/
19-
function checkFileExtension(fileRef){
20+
function isJavascriptFile(fileRef){
2021

2122
var resolver = Promise.defer();
2223

@@ -29,6 +30,40 @@
2930
return resolver.promise;
3031
}
3132

33+
34+
35+
/**
36+
* Checks for file not to be skipped
37+
*
38+
* returns a Promise
39+
* rejected promise if 'fileRef' matches 'skipped' String value
40+
* fulfilled promise if 'skipped' is falsy or empty
41+
*
42+
* fulfilled promise returns the passed file reference (path)
43+
*/
44+
45+
function isNotSkipped(fileRef, skipped){
46+
47+
var
48+
skippedRegExp = new RegExp(skipped, 'g')
49+
;
50+
51+
if(typeof skipped === 'string') {
52+
53+
if( fileRef.match(skippedRegExp) ) {
54+
throw new Error(fileRef + ' is skipped');
55+
} else {
56+
return fileRef;
57+
}
58+
59+
} else {
60+
61+
return fileRef;
62+
63+
}
64+
65+
}
66+
3267
/**
3368
* Reads JS file
3469
*
@@ -90,7 +125,7 @@
90125
*
91126
* returns a Function
92127
*/
93-
function populateReportList(errorsList, reportList){
128+
function populateReportList(errorsList, reportList, skipped){
94129

95130
/**
96131
* Populates error and report stack objects
@@ -103,7 +138,11 @@
103138

104139
var fileRef = require('path').normalize(root + "/" + fileStats.name);
105140

106-
checkFileExtension(fileRef)
141+
Promise.resolve(fileRef)
142+
.then(isJavascriptFile, next)
143+
.then(function(fileRef){
144+
return isNotSkipped(fileRef, skipped);
145+
})
107146
.caught(next) // fire 'next' if extension is not correct
108147
.then(readJSFile)
109148
.then(buildFileReport)
@@ -123,49 +162,25 @@
123162

124163
}
125164

126-
/**
127-
*
128-
*/
129-
130-
function formatOptions(rawOptions){
131-
132-
var formattedOptions = {
133-
followLinks: false
134-
};
135-
136-
if(rawOptions){
137-
138-
if(
139-
rawOptions.skippedDirectories
140-
&& rawOptions.skippedDirectories.length > 0
141-
) {
142-
formattedOptions.filters = rawOptions.skippedDirectories;
143-
}
144-
145-
}
146-
147-
return formattedOptions;
148-
}
149-
150165
/**
151166
* builds a complexity report of .js files
152167
* found in a file tree from a given path
153168
*
154169
* returns a Promise
155170
* rejects promise if any runtime error occurs
156171
*/
157-
function crawlComplexity(path){
172+
function crawlComplexity(path, skippedFolder){
158173

159174
var
160175
reportList = [],
161176
errorsList = null,
162177
resolver = Promise.defer()
163178
;
164179

165-
var walker = walk.walk(path);
180+
var walker = walk.walk(path || './');
166181

167182
walker
168-
.on("file", populateReportList(errorsList, reportList))
183+
.on("file", populateReportList(errorsList, reportList, skippedFolder))
169184
.on("error", function(){ resolver.reject('runtime error'); })
170185
.on("end", function(){
171186
resolver.resolve({

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var chai = require('chai'),
1212
treePathNotExisting = '/doesntexist',
1313
crawler = require('../src/crawl-complexity'),
1414
expectedComplexRes = require('./complex-tree-results'),
15+
expectedSkipRes = require('./skip-results'),
1516
expectedEmptyRes = { report : [], errors : null };
1617

1718
chai.use(chaiAsPromised);
@@ -54,4 +55,14 @@ describe('the complexity crawler promise', function () {
5455

5556
});
5657

58+
it('should return the attended complex result when a skip folder path is passed', function(done){
59+
60+
this.timeout(10000);
61+
62+
expect(crawler(treePathComplex, '/tree/complex/jquery'))
63+
.to.eventually.deep.equal(expectedSkipRes)
64+
.to.be.fulfilled.and.notify(done);
65+
66+
});
67+
5768
});

test/skip-results.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"report":
3+
[ { "path": "test/tree/complex/empty.js",
4+
"escapedPath": "test/tree/complex/empty.js",
5+
"complexity": 1,
6+
"lineNumber": 0,
7+
"maintainability": 0,
8+
"halstead": { "length": 0, "vocabulary": 0, "difficulty": 0, "bugs": 0 }
9+
}
10+
],
11+
"errors": null
12+
}

0 commit comments

Comments
 (0)