99 *
1010 */
1111
12- /*eslint no-process-exit: 0*/
13-
1412var argv = require ( 'nomnom' )
1513 . script ( 'react-docgen' )
1614 . help (
@@ -73,6 +71,7 @@ var extensions = new RegExp('\\.(?:' + argv.extension.join('|') + ')$');
7371var ignoreDir = argv . ignoreDir ;
7472var excludePatterns = argv . excludePatterns ;
7573var resolver ;
74+ var errorMessage ;
7675
7776if ( argv . resolver ) {
7877 try {
@@ -90,7 +89,8 @@ if (argv.resolver) {
9089 if ( e . code !== 'MODULE_NOT_FOUND' ) {
9190 throw e ;
9291 }
93- exitWithError (
92+ // Will exit with this error message
93+ errorMessage = (
9494 `Unknown resolver: "${ argv . resolver } " is neither a built-in resolver ` +
9595 `nor can it be found locally ("${ resolverPath } ")`
9696 ) ;
@@ -112,12 +112,7 @@ function writeError(msg, filePath) {
112112 }
113113}
114114
115- function exitWithError ( error ) {
116- writeError ( error ) ;
117- process . exit ( 1 ) ;
118- }
119-
120- function exitWithResult ( result ) {
115+ function writeResult ( result ) {
121116 result = argv . pretty ?
122117 JSON . stringify ( result , null , 2 ) :
123118 JSON . stringify ( result ) ;
@@ -126,7 +121,6 @@ function exitWithResult(result) {
126121 } else {
127122 process . stdout . write ( result + '\n' ) ;
128123 }
129- process . exit ( 0 ) ;
130124}
131125
132126function traverseDir ( filePath , result , done ) {
@@ -139,7 +133,7 @@ function traverseDir(filePath, result, done) {
139133 } ,
140134 function ( error , content , filename , next ) {
141135 if ( error ) {
142- exitWithError ( error ) ;
136+ throw error ;
143137 }
144138 try {
145139 result [ filename ] = parse ( content ) ;
@@ -150,17 +144,23 @@ function traverseDir(filePath, result, done) {
150144 } ,
151145 function ( error ) {
152146 if ( error ) {
153- writeError ( error ) ;
147+ throw error ;
154148 }
155149 done ( ) ;
156150 }
157151 ) ;
158152}
159153
160154/**
161- * 1. No files passed, consume input stream
155+ * 1. An error occurred, so exit
162156 */
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+ */
164164 var source = '' ;
165165 process . stdin . setEncoding ( 'utf8' ) ;
166166 process . stdin . resume ( ) ;
@@ -173,14 +173,14 @@ if (paths.length === 0) {
173173 } ) ;
174174 process . stdin . on ( 'end' , function ( ) {
175175 try {
176- exitWithResult ( parse ( source ) ) ;
176+ writeResult ( parse ( source ) ) ;
177177 } catch ( error ) {
178178 writeError ( error ) ;
179179 }
180180 } ) ;
181181} else {
182182 /**
183- * 2 . Paths are passed.
183+ * 3 . Paths are passed
184184 */
185185 var result = Object . create ( null ) ;
186186 async . eachSeries ( paths , function ( filePath , done ) {
@@ -191,7 +191,12 @@ if (paths.length === 0) {
191191 return ;
192192 }
193193 if ( stats . isDirectory ( ) ) {
194- traverseDir ( filePath , result , done ) ;
194+ try {
195+ traverseDir ( filePath , result , done ) ;
196+ } catch ( error ) {
197+ writeError ( error ) ;
198+ done ( ) ;
199+ }
195200 }
196201 else {
197202 try {
@@ -208,14 +213,13 @@ if (paths.length === 0) {
208213 var resultsPaths = Object . keys ( result ) ;
209214 if ( resultsPaths . length === 0 ) {
210215 // 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?
214218 fs . stat ( paths [ 0 ] , function ( error , stats ) {
215- exitWithResult ( stats . isDirectory ( ) ? result : result [ resultsPaths [ 0 ] ] ) ;
219+ writeResult ( stats . isDirectory ( ) ? result : result [ resultsPaths [ 0 ] ] ) ;
216220 } ) ;
217221 } else {
218- exitWithResult ( result ) ;
222+ writeResult ( result ) ;
219223 }
220224 } ) ;
221225}
0 commit comments