Skip to content

Commit 6d9655e

Browse files
committed
Make "findAllExportedComponentDefinitions" selectable from the CLI (closes #119)
This changes the CLI from a fixed list of resolver names to dynamically looking for a built-in resolver with that name (even though we might not actually add any additional resolvers in the future). It also provides a better error message when a resolver couldn't be found.
1 parent 8a4e83a commit 6d9655e

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

bin/react-docgen.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,26 @@ var excludePatterns = argv.excludePatterns;
7575
var resolver;
7676

7777
if (argv.resolver) {
78-
switch(argv.resolver) {
79-
case 'findAllComponentDefinitions':
80-
resolver = require('../dist/resolver/findAllComponentDefinitions').default;
81-
break;
82-
case 'findExportedComponentDefinition':
83-
resolver = require('../dist/resolver/findExportedComponentDefinition').default;
84-
break;
85-
default: // treat value as module path
86-
resolver = require(path.resolve(process.cwd(), argv.resolver));
78+
try {
79+
// Look for built-in resolver
80+
resolver = require(`../dist/resolver/${argv.resolver}`).default;
81+
} catch(e) {
82+
if (e.code !== 'MODULE_NOT_FOUND') {
83+
throw e;
84+
}
85+
const resolverPath = path.resolve(process.cwd(), argv.resolver);
86+
try {
87+
// Look for local resolver
88+
resolver = require(resolverPath);
89+
} catch (e) {
90+
if (e.code !== 'MODULE_NOT_FOUND') {
91+
throw e;
92+
}
93+
exitWithError(
94+
`Unknown resolver: "${argv.resolver}" is neither a built-in resolver ` +
95+
`nor can it be found locally ("${resolverPath}")`
96+
);
97+
}
8798
}
8899
}
89100

@@ -92,7 +103,7 @@ function parse(source) {
92103
}
93104

94105
function writeError(msg, filePath) {
95-
if (path) {
106+
if (filePath) {
96107
process.stderr.write('Error with path "' + filePath + '": ');
97108
}
98109
process.stderr.write(msg + '\n');

0 commit comments

Comments
 (0)