9
9
*
10
10
*/
11
11
12
- /*eslint no-process-exit: 0*/
13
-
14
12
var argv = require ( 'nomnom' )
15
13
. script ( 'react-docgen' )
16
14
. help (
@@ -73,6 +71,7 @@ var extensions = new RegExp('\\.(?:' + argv.extension.join('|') + ')$');
73
71
var ignoreDir = argv . ignoreDir ;
74
72
var excludePatterns = argv . excludePatterns ;
75
73
var resolver ;
74
+ var errorMessage ;
76
75
77
76
if ( argv . resolver ) {
78
77
try {
@@ -90,7 +89,8 @@ if (argv.resolver) {
90
89
if ( e . code !== 'MODULE_NOT_FOUND' ) {
91
90
throw e ;
92
91
}
93
- exitWithError (
92
+ // Will exit with this error message
93
+ errorMessage = (
94
94
`Unknown resolver: "${ argv . resolver } " is neither a built-in resolver ` +
95
95
`nor can it be found locally ("${ resolverPath } ")`
96
96
) ;
@@ -112,12 +112,7 @@ function writeError(msg, filePath) {
112
112
}
113
113
}
114
114
115
- function exitWithError ( error ) {
116
- writeError ( error ) ;
117
- process . exit ( 1 ) ;
118
- }
119
-
120
- function exitWithResult ( result ) {
115
+ function writeResult ( result ) {
121
116
result = argv . pretty ?
122
117
JSON . stringify ( result , null , 2 ) :
123
118
JSON . stringify ( result ) ;
@@ -126,7 +121,6 @@ function exitWithResult(result) {
126
121
} else {
127
122
process . stdout . write ( result + '\n' ) ;
128
123
}
129
- process . exit ( 0 ) ;
130
124
}
131
125
132
126
function traverseDir ( filePath , result , done ) {
@@ -139,7 +133,7 @@ function traverseDir(filePath, result, done) {
139
133
} ,
140
134
function ( error , content , filename , next ) {
141
135
if ( error ) {
142
- exitWithError ( error ) ;
136
+ throw error ;
143
137
}
144
138
try {
145
139
result [ filename ] = parse ( content ) ;
@@ -150,17 +144,23 @@ function traverseDir(filePath, result, done) {
150
144
} ,
151
145
function ( error ) {
152
146
if ( error ) {
153
- writeError ( error ) ;
147
+ throw error ;
154
148
}
155
149
done ( ) ;
156
150
}
157
151
) ;
158
152
}
159
153
160
154
/**
161
- * 1. No files passed, consume input stream
155
+ * 1. An error occurred, so exit
162
156
*/
163
- if ( paths . length === 0 ) {
157
+ if ( errorMessage ) {
158
+ writeError ( errorMessage ) ;
159
+ process . exitCode = 1 ;
160
+ } else if ( paths . length === 0 ) {
161
+ /**
162
+ * 2. No files passed, consume input stream
163
+ */
164
164
var source = '' ;
165
165
process . stdin . setEncoding ( 'utf8' ) ;
166
166
process . stdin . resume ( ) ;
@@ -173,14 +173,14 @@ if (paths.length === 0) {
173
173
} ) ;
174
174
process . stdin . on ( 'end' , function ( ) {
175
175
try {
176
- exitWithResult ( parse ( source ) ) ;
176
+ writeResult ( parse ( source ) ) ;
177
177
} catch ( error ) {
178
178
writeError ( error ) ;
179
179
}
180
180
} ) ;
181
181
} else {
182
182
/**
183
- * 2 . Paths are passed.
183
+ * 3 . Paths are passed
184
184
*/
185
185
var result = Object . create ( null ) ;
186
186
async . eachSeries ( paths , function ( filePath , done ) {
@@ -191,7 +191,12 @@ if (paths.length === 0) {
191
191
return ;
192
192
}
193
193
if ( stats . isDirectory ( ) ) {
194
- traverseDir ( filePath , result , done ) ;
194
+ try {
195
+ traverseDir ( filePath , result , done ) ;
196
+ } catch ( error ) {
197
+ writeError ( error ) ;
198
+ done ( ) ;
199
+ }
195
200
}
196
201
else {
197
202
try {
@@ -208,14 +213,13 @@ if (paths.length === 0) {
208
213
var resultsPaths = Object . keys ( result ) ;
209
214
if ( resultsPaths . length === 0 ) {
210
215
// we must have gotten an error
211
- process . exit ( 1 ) ;
212
- }
213
- if ( paths . length === 1 ) { // a single path?
216
+ process . exitCode = 1 ;
217
+ } else if ( paths . length === 1 ) { // a single path?
214
218
fs . stat ( paths [ 0 ] , function ( error , stats ) {
215
- exitWithResult ( stats . isDirectory ( ) ? result : result [ resultsPaths [ 0 ] ] ) ;
219
+ writeResult ( stats . isDirectory ( ) ? result : result [ resultsPaths [ 0 ] ] ) ;
216
220
} ) ;
217
221
} else {
218
- exitWithResult ( result ) ;
222
+ writeResult ( result ) ;
219
223
}
220
224
} ) ;
221
225
}
0 commit comments