Skip to content

Commit c94d1db

Browse files
author
Jacek Kubiak
committed
Modify index.js to support include_paths
1 parent cee6922 commit c94d1db

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

index.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var printIssue = function(fileName, lineNum, matchedString){
2323
"path": fileName,
2424
"lines": {
2525
"begin": lineNum,
26-
"end": lineNum
26+
"end": lineNum
2727
}
2828
}
2929
};
@@ -44,7 +44,7 @@ var findFixmes = function(file){
4444
if (results !== ""){
4545
// Parses grep output
4646
var lines = results.split("\n");
47-
47+
4848
lines.forEach(function(line, index, array){
4949
// grep spits out an extra line that we can ignore
5050
if(index < (array.length-1)){
@@ -65,39 +65,65 @@ var findFixmes = function(file){
6565
})
6666
}
6767

68-
var eligibleFile = function(fp, excludePaths){
69-
return (excludePaths.indexOf(fp.split("/code/")[1]) < 0) &&
70-
!fs.lstatSync(fp).isDirectory() &&
71-
(excludeExtensions.indexOf(path.extname(fp)) < 0)
68+
// Returns an array of unique array values not included in the other provided array
69+
var diff = function(a1, a2) {
70+
var result = [];
71+
72+
for (var i = 0; i < a1.length; i++) {
73+
if (a2.indexOf(a1[i]) === -1) {
74+
result.push(a1[i]);
75+
}
76+
77+
}
78+
return result;
7279
}
7380

74-
// Uses glob to traverse code directory and find files to analyze,
75-
// excluding files passed in with by CLI config
76-
var fileWalk = function(excludePaths){
77-
var analysisFiles = [];
78-
var allFiles = glob.sync("/code/**/**", {});
81+
// Returns all the file paths in the main directory that match the given pattern
82+
var buildFiles = function(paths) {
83+
var files = [];
7984

80-
allFiles.forEach(function(file, i, a){
81-
if(eligibleFile(file, excludePaths)){
82-
analysisFiles.push(file);
83-
}
85+
paths.forEach(function(path, i, a) {
86+
var pattern = "/code/" + path + "**"
87+
files.push.apply(files, glob.sync(pattern, {}));
88+
});
89+
90+
return files;
91+
}
92+
93+
// Filters the directory paths out
94+
var filterFiles = function(files) {
95+
return files.filter(function(file) {
96+
return !fs.lstatSync(file).isDirectory();
8497
});
85-
86-
return analysisFiles;
98+
}
99+
100+
// Returns file paths based on the exclude_paths values in config file
101+
var buildFilesWithExclusions = function(exclusions) {
102+
var allFiles = glob.sync("/code/**/**", {});
103+
var excludedFiles = buildFiles(exclusions);
104+
105+
return diff(allFiles, excludedFiles);
106+
}
107+
108+
// Returns file paths based on the include_paths values in config file
109+
var buildFilesWithInclusions = function(inclusions) {
110+
return buildFiles(inclusions);
87111
}
88112

89113
FixMe.prototype.runEngine = function(){
90-
// Check for existence of config.json, parse exclude paths if it exists
114+
var analysisFiles = []
115+
91116
if (fs.existsSync("/config.json")) {
92117
var engineConfig = JSON.parse(fs.readFileSync("/config.json"));
93-
var excludePaths = engineConfig.exclude_paths;
94-
} else {
95-
var excludePaths = [];
96-
}
97118

98-
// Walk /code/ path and find files to analyze
99-
var analysisFiles = fileWalk(excludePaths);
119+
if (engineConfig.hasOwnProperty("include_paths")) {
120+
analysisFiles = buildFilesWithInclusions(engineConfig.include_paths);
121+
} else if (engineConfig.hasOwnProperty("exclude_paths")) {
122+
analysisFiles = buildFilesWithExclusions(engineConfig.exclude_paths);
123+
}
124+
}
100125

126+
analysisFiles = filterFiles(analysisFiles);
101127
// Execute main loop and find fixmes in valid files
102128
analysisFiles.forEach(function(f, i, a){
103129
findFixmes(f);

0 commit comments

Comments
 (0)