@@ -53,7 +53,7 @@ var fs = require('fs');
53
53
var parser = require ( '../dist/main.js' ) ;
54
54
55
55
var output = argv . out ;
56
- var paths = argv . path ;
56
+ var paths = argv . path || [ ] ;
57
57
var extensions = new RegExp ( '\\.(?:' + argv . extension . join ( '|' ) + ')$' ) ;
58
58
var ignoreDir = argv . ignoreDir ;
59
59
@@ -81,25 +81,6 @@ function exitWithResult(result) {
81
81
process . exit ( 0 ) ;
82
82
}
83
83
84
- /**
85
- * 1. No files passed, consume input stream
86
- */
87
- if ( paths . length === 0 ) {
88
- var source = '' ;
89
- process . stdin . setEncoding ( 'utf8' ) ;
90
- process . stdin . resume ( ) ;
91
- var timer = setTimeout ( function ( ) {
92
- process . stderr . write ( 'Still waiting for std input...' ) ;
93
- } , 5000 ) ;
94
- process . stdin . on ( 'data' , function ( chunk ) {
95
- clearTimeout ( timer ) ;
96
- source += chunk ;
97
- } ) ;
98
- process . stdin . on ( 'end' , function ( ) {
99
- exitWithResult ( parser . parse ( source ) ) ;
100
- } ) ;
101
- }
102
-
103
84
function traverseDir ( path , result , done ) {
104
85
dir . readFiles (
105
86
path ,
@@ -128,41 +109,64 @@ function traverseDir(path, result, done) {
128
109
}
129
110
130
111
/**
131
- * 2. Paths are passed.
112
+ * 1. No files passed, consume input stream
132
113
*/
133
- var result = Object . create ( null ) ;
134
- async . eachSeries ( paths , function ( path , done ) {
135
- fs . stat ( path , function ( error , stats ) {
136
- if ( error ) {
137
- writeError ( error , path ) ;
138
- done ( ) ;
139
- return ;
140
- }
141
- if ( stats . isDirectory ( ) ) {
142
- traverseDir ( path , result , done ) ;
114
+ if ( paths . length === 0 ) {
115
+ var source = '' ;
116
+ process . stdin . setEncoding ( 'utf8' ) ;
117
+ process . stdin . resume ( ) ;
118
+ var timer = setTimeout ( function ( ) {
119
+ process . stderr . write ( 'Still waiting for std input...' ) ;
120
+ } , 5000 ) ;
121
+ process . stdin . on ( 'data' , function ( chunk ) {
122
+ clearTimeout ( timer ) ;
123
+ source += chunk ;
124
+ } ) ;
125
+ process . stdin . on ( 'end' , function ( ) {
126
+ try {
127
+ exitWithResult ( parser . parse ( source ) ) ;
128
+ } catch ( error ) {
129
+ writeError ( error ) ;
143
130
}
144
- else {
145
- try {
146
- result [ path ] = parser . parse ( fs . readFileSync ( path ) ) ;
147
- } catch ( error ) {
131
+ } ) ;
132
+ } else {
133
+ /**
134
+ * 2. Paths are passed.
135
+ */
136
+ var result = Object . create ( null ) ;
137
+ async . eachSeries ( paths , function ( path , done ) {
138
+ fs . stat ( path , function ( error , stats ) {
139
+ if ( error ) {
148
140
writeError ( error , path ) ;
149
- }
150
- finally {
151
141
done ( ) ;
142
+ return ;
143
+ }
144
+ if ( stats . isDirectory ( ) ) {
145
+ traverseDir ( path , result , done ) ;
152
146
}
147
+ else {
148
+ try {
149
+ result [ path ] = parser . parse ( fs . readFileSync ( path ) ) ;
150
+ } catch ( error ) {
151
+ writeError ( error , path ) ;
152
+ }
153
+ finally {
154
+ done ( ) ;
155
+ }
156
+ }
157
+ } ) ;
158
+ } , function ( ) {
159
+ var resultsPaths = Object . keys ( result ) ;
160
+ if ( resultsPaths . length === 0 ) {
161
+ // we must have gotten an error
162
+ process . exit ( 1 ) ;
163
+ }
164
+ if ( paths . length === 1 ) { // a single path?
165
+ fs . stat ( paths [ 0 ] , function ( error , stats ) {
166
+ exitWithResult ( stats . isDirectory ( ) ? result : result [ resultsPaths [ 0 ] ] ) ;
167
+ } ) ;
168
+ } else {
169
+ exitWithResult ( result ) ;
153
170
}
154
171
} ) ;
155
- } , function ( ) {
156
- var resultsPaths = Object . keys ( result ) ;
157
- if ( resultsPaths . length === 0 ) {
158
- // we must have gotten an error
159
- process . exit ( 1 ) ;
160
- }
161
- if ( paths . length === 1 ) { // a single path?
162
- fs . stat ( paths [ 0 ] , function ( error , stats ) {
163
- exitWithResult ( stats . isDirectory ( ) ? result : result [ resultsPaths [ 0 ] ] ) ;
164
- } ) ;
165
- } else {
166
- exitWithResult ( result ) ;
167
- }
168
- } ) ;
172
+ }
0 commit comments