Skip to content

Commit

Permalink
Update the readme.md directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rianbotha committed Apr 28, 2019
1 parent 78da9bf commit dec1583
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 45 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ npm i -g @rianbotha/react-props-md-table
```

## Usage
Run the following command in the terminal to output a markdown PropTable.
Run the following command in the terminal to replace the props table in `readme.md` in the same directory.
```
props-table path/to/SomeComponent.js
```

## Credits
Inspired by https://github.com/TeamWertarbyte/react-props-md-table
The props table in the file needs to be wrapped between `<!-- props-table-start -->` and `<!-- props-table-end -->` for the replacement to work.

### Options

`--toConsole` will write the props table to the terminal instead of the file.
`--force` will write the props table at the end of the readme if no table wrapped by comments is found. The readme.md will be created if it doesn't exist.
`--help` or `-h` display usage options
`--version` or `-v` display version information
5 changes: 5 additions & 0 deletions add-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const addLine = (source, strToAdd = '',) => (
source + strToAdd + '\n'
);

module.exports = addLine;
43 changes: 43 additions & 0 deletions build-props-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const reactDocs = require('react-docgen');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const formatType = require('./format-type');
const nlToBr = require('./nl-to-br');
const addLine = require('./add-line');

const buildPropsTable = (filename) => {
try {
const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8');
const components = reactDocs.parse(content, reactDocs.resolver.findAllComponentDefinitions);

let propsTable = '';

components.forEach((component) => {
const title = components.length > 1 ? `## ${component.displayName || ''} Properties` : '## Properties';
propsTable = addLine(propsTable, title);
propsTable = addLine(propsTable);

if (component.props) {
propsTable = addLine(propsTable, '| Property | PropType | Required | Default | Description |');
propsTable = addLine(propsTable, '|----------|----------|----------|---------|-------------|');

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

propsTable = addLine(propsTable);
});

return propsTable;
} catch (e) {
console.error(chalk.red(`Can't extract props data from file ${filename}: ${e.message}`));
return null;
}
}

module.exports = buildPropsTable;
7 changes: 7 additions & 0 deletions format-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const formatType = (type) => (
type.name === 'union'
? type.value.map(formatType).join(' || ')
: type.name
);

module.exports = formatType;
66 changes: 26 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
#!/usr/bin/env node
const reactDocs = require('react-docgen');
const fs = require('fs');
const path = require('path');
const buildPropsTable = require('./build-props-table');
const writeReadme = require('./write-readme');
const argv = require('yargs')
.usage('Usage: $0 <filename> [--toConsole]')
.boolean(['toConsole', 'force'])
.describe('toConsole', 'Outputs props table to command line instead of readme')
.describe('force', 'Add props table at end of file if no table is found to be replaced. A new readme.md will be created if it does not exist.')
.demandCommand(1)
.alias('h', 'help')
.alias('v', 'version')
.argv

const filename = argv._[0];
const toConsole = argv.toConsole;
const force = argv.force;
const readme = path.dirname(filename) + '/readme.md';
const regex = /<!-- props-table-start -->[\s\S]*<!-- props-table-end -->/m;

const propsTable = buildPropsTable(filename);

if (toConsole) {
console.log(propsTable);
process.exit(1);
}

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];

try {
const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8');
const components = reactDocs.parse(content, reactDocs.resolver.findAllComponentDefinitions);

components.forEach((component) => {

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');
}

console.log('');
});
} catch (e) {
console.warn(`Can't extract props data from file ${filename}: ${e.message}`);
if (propsTable) {
writeReadme(readme, propsTable, regex, force);
}
5 changes: 5 additions & 0 deletions nl-to-br.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const nlToBr = (str, addQuotes = false) => (
str.replace(/\r\n|\r|\n/gi, addQuotes ? '`<br>`' : '<br>')
);

module.exports = nlToBr;
Loading

0 comments on commit dec1583

Please sign in to comment.