1
- var glob = require ( 'glob' ) ,
2
- spawn = require ( 'child_process' ) . spawn ,
3
- fs = require ( 'fs' ) ,
4
- path = require ( 'path' ) ,
5
- diff = require ( './diff' ) ,
6
- fileBuilder = require ( './file-builder' ) ;
7
-
8
- module . exports = FixMe ;
9
-
10
- function FixMe ( ) { }
11
-
12
- FixMe . prototype . runEngine = function ( ) {
13
- var analysisFiles = [ ] ,
14
- config = {
15
- include_paths : [ "./" ] ,
16
- strings : [ "FIXME" , "TODO" , "HACK" , "XXX" , "BUG" ]
17
- } ,
18
- self = this ;
1
+ var fs = require ( 'fs' ) ;
2
+ var glob = require ( 'glob' ) ;
3
+ var path = require ( 'path' ) ;
4
+ var readline = require ( 'readline' ) ;
5
+ var spawn = require ( 'child_process' ) . spawn ;
6
+
7
+ function FixMe ( ) { }
8
+
9
+ FixMe . prototype . runEngine = function ( ) {
10
+ var config = {
11
+ include_paths : [ './' ] ,
12
+ strings : [ 'FIXME' , 'TODO' , 'HACK' , 'XXX' , 'BUG' ]
13
+ } ;
19
14
20
15
if ( fs . existsSync ( '/config.json' ) ) {
21
16
var userConfig = JSON . parse ( fs . readFileSync ( '/config.json' ) ) ;
@@ -29,67 +24,40 @@ FixMe.prototype.runEngine = function(){
29
24
}
30
25
}
31
26
32
- analysisFiles = fileBuilder . withIncludes ( config . include_paths ) ;
33
- analysisFiles = fileBuilder . filterFiles ( analysisFiles ) ;
34
-
35
- self . find ( analysisFiles , config . strings ) ;
27
+ this . find ( config . include_paths , config . strings ) ;
36
28
}
37
29
38
- FixMe . prototype . find = function ( files , strings ) {
30
+ FixMe . prototype . find = function ( files , strings ) {
39
31
var fixmeStrings = '(' + strings . join ( '|' ) + ')' ;
40
- var grep = spawn ( 'grep' , [ '-nHwoEr' , fixmeStrings ] . concat ( files ) ) ;
41
- var output = "" ;
42
- var self = this ;
43
-
44
- grep . stdout . on ( 'data' , function ( data ) {
45
- output += data . toString ( ) ;
46
- } ) ;
47
-
48
- grep . stdout . on ( 'close' , function ( ) {
49
- if ( output !== "" ) {
50
- // Parses grep output
51
- var lines = output . split ( "\n" ) ;
52
-
53
- lines . forEach ( function ( line , index , array ) {
54
- // grep spits out an extra line that we can ignore
55
- if ( index < ( array . length - 1 ) ) {
56
- // Grep output is colon delimited
57
- var cols = line . split ( ":" ) ;
58
-
59
- // Remove remnants of container paths for external display
60
- var fileName = self . formatPath ( cols [ 0 ] ) ;
61
- var lineNum = cols [ 1 ] ;
62
- var matchedString = cols [ 2 ] ;
63
-
64
- if ( matchedString !== undefined ) {
65
- self . printIssue ( fileName , parseInt ( lineNum ) , matchedString ) ;
32
+ var args = [ '--line-number' , '--with-filename' , '--word-regexp' ,
33
+ '--only-matching' , '--extended-regexp' , '--recursive' ,
34
+ '--binary-files=without-match' , fixmeStrings ] ;
35
+ var grep = spawn ( 'grep' , args . concat ( files ) ) ;
36
+
37
+ readline . createInterface ( { input : grep . stdout } ) . on ( 'line' , function ( line ) {
38
+ var cols = line . split ( ':' ) ;
39
+ var fileName = cols [ 0 ] . replace ( / ^ \/ c o d e \/ / , '' ) ;
40
+ var lineNum = parseInt ( cols [ 1 ] ) ;
41
+ var matchedString = cols [ 2 ] ;
42
+
43
+ if ( matchedString !== undefined ) {
44
+ var issue = JSON . stringify ( {
45
+ 'type' : 'issue' ,
46
+ 'check_name' : matchedString ,
47
+ 'description' : matchedString + ' found' ,
48
+ 'categories' : [ 'Bug Risk' ] ,
49
+ 'location' :{
50
+ 'path' : fileName ,
51
+ 'lines' : {
52
+ 'begin' : lineNum ,
53
+ 'end' : lineNum
66
54
}
67
55
}
68
- } )
69
- }
70
- } ) ;
71
- }
56
+ } ) ;
72
57
73
- FixMe . prototype . printIssue = function ( fileName , lineNum , matchedString ) {
74
- // Prints properly structured Issue data to STDOUT according to Code Climate Engine specification.
75
- var issue = {
76
- "type" : "issue" ,
77
- "check_name" : matchedString ,
78
- "description" : matchedString + " found" ,
79
- "categories" : [ "Bug Risk" ] ,
80
- "location" :{
81
- "path" : fileName ,
82
- "lines" : {
83
- "begin" : lineNum ,
84
- "end" : lineNum
85
- }
58
+ console . log ( issue + '\0' ) ;
86
59
}
87
- } ;
88
-
89
- var issueString = JSON . stringify ( issue ) + "\0" ;
90
- console . log ( issueString ) ;
60
+ } ) ;
91
61
}
92
62
93
- FixMe . prototype . formatPath = function ( path ) {
94
- return path . replace ( / ^ \/ c o d e \/ / , '' ) ;
95
- }
63
+ module . exports = FixMe ;
0 commit comments