forked from TeamWertarbyte/react-props-md-table
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
566 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const addLine = (source, strToAdd = '',) => ( | ||
source + strToAdd + '\n' | ||
); | ||
|
||
module.exports = addLine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.