Skip to content

Commit

Permalink
Added ability to handle multiple exports in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
rianbotha committed Apr 24, 2019
1 parent 27ed3fa commit 78da9bf
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ props-table path/to/SomeComponent.js
```

## Credits
Forked from https://github.com/TeamWertarbyte/react-props-md-table
Inspired by https://github.com/TeamWertarbyte/react-props-md-table
50 changes: 33 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,42 @@ const reactDocs = require('react-docgen');
const fs = require('fs');
const path = require('path');

const formatType = (type) => (
type.name === 'union'
? type.value.map(formatType).join(' || ')
: type.name
);

const nlToBr = (str, addQuotes = false) => (
str.replace(/\r\n|\r|\n/gi, addQuotes ? '`<br>`' : '<br>')
);

const filename = process.argv[process.argv.length - 1];
const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8');

const component = reactDocs.parse(content);
console.log('| Property | PropType | Required | Default | Description |');
console.log('|----------|----------|----------|---------|-------------|');
try {
const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8');
const components = reactDocs.parse(content, reactDocs.resolver.findAllComponentDefinitions);

for (const name of Object.keys(component.props)) {
const { type, required, description, defaultValue } = component.props[name];
console.log(`| ${name} | \`${formatType(type)}\` | ${required ? 'yes' : ''} | ${defaultValue != null ? `\`${nlToBr(defaultValue.value, true)}\`` : ''} | ${nlToBr(description)} |`);
}
components.forEach((component) => {

function formatType (type) {
if (type.name === 'union') {
return type.value.map(formatType).join(' || ');
} else {
return type.name;
}
}
const title = components.length > 1 ? `## ${component.displayName || ''} Properties` : '## Properties';
console.log(title);
console.log('');

if (component.props) {
console.log('| Property | PropType | Required | Default | Description |');
console.log('|----------|----------|----------|---------|-------------|');

for (const name of Object.keys(component.props)) {
const { type, required, description, defaultValue } = component.props[name];
console.log(`| ${name} | \`${formatType(type)}\` | ${required ? 'yes' : ''} | ${defaultValue != null ? `\`${nlToBr(defaultValue.value, true)}\`` : ''} | ${nlToBr(description)} |`);
}
} else {
console.log('This component has no properties');
}

function nlToBr(str, addQuotes = false) {
return str.replace(/\r\n|\r|\n/gi, addQuotes ? '`<br>`' : '<br>');
console.log('');
});
} catch (e) {
console.warn(`Can't extract props data from file ${filename}: ${e.message}`);
}
138 changes: 69 additions & 69 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@rianbotha/react-props-md-table",
"version": "1.0.2",
"description": "A markdown table generator for React component props forked from TeamWerterbyte.",
"version": "2.0.0",
"description": "A markdown table generator for React component props.",
"main": "index.js",
"bin": {
"props-table": "index.js"
Expand All @@ -21,6 +21,6 @@
"license": "MIT",
"homepage": "https://github.com/rianbotha/react-props-md-table#readme",
"dependencies": {
"react-docgen": "^4.0.0"
"react-docgen": "^4.1.0"
}
}

0 comments on commit 78da9bf

Please sign in to comment.