Skip to content

Commit ac92076

Browse files
committed
Fix reading from stdin
1 parent bc68a8b commit ac92076

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

bin/react-docgen.js

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var fs = require('fs');
5353
var parser = require('../dist/main.js');
5454

5555
var output = argv.out;
56-
var paths = argv.path;
56+
var paths = argv.path || [];
5757
var extensions = new RegExp('\\.(?:' + argv.extension.join('|') + ')$');
5858
var ignoreDir = argv.ignoreDir;
5959

@@ -81,25 +81,6 @@ function exitWithResult(result) {
8181
process.exit(0);
8282
}
8383

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-
10384
function traverseDir(path, result, done) {
10485
dir.readFiles(
10586
path,
@@ -128,41 +109,64 @@ function traverseDir(path, result, done) {
128109
}
129110

130111
/**
131-
* 2. Paths are passed.
112+
* 1. No files passed, consume input stream
132113
*/
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);
143130
}
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) {
148140
writeError(error, path);
149-
}
150-
finally {
151141
done();
142+
return;
143+
}
144+
if (stats.isDirectory()) {
145+
traverseDir(path, result, done);
152146
}
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);
153170
}
154171
});
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

Comments
 (0)