@charlietango/react-docs
Advanced tools
Comparing version
@@ -5,3 +5,3 @@ // | ||
@@ -8,0 +8,0 @@ |
@@ -16,2 +16,7 @@ // | ||
// Regex used for detection | ||
const typeRegex = /type\s+?(\w+)\s+?=([\s\S]+?\n\|?})/; | ||
const defaultPropsRegex = /defaultProps\s+?=[\s\S]+?\n}/; | ||
const descriptionRegex = /\n\/\*\*[\s\S]+?\*\//; | ||
@@ -99,6 +104,2 @@ | ||
function parseViewModel(component ) { | ||
const typeRegex = /type\s+?(\w+)\s+?=([\s\S]+?\n\|?})/; | ||
const defaultPropsRegex = /defaultProps\s+?=[\s\S]+?\n}/; | ||
const descriptionRegex = /\n\/\*\*[\s\S]+?\*\//; | ||
const types = typeRegex.exec(component.src); | ||
@@ -110,3 +111,5 @@ const defaultProps = defaultPropsRegex.exec(component.src); | ||
throw new Error( | ||
`Failed to types for ${component.name} - ${chalk.grey(component.file)}` | ||
`Failed to find model for ${component.name} - ${chalk.grey( | ||
component.file | ||
)}` | ||
); | ||
@@ -113,0 +116,0 @@ } |
{ | ||
"name": "@charlietango/react-docs", | ||
"description": "Generate docs using react-docgen, and output to Markdown, json or CSharp ViewModels.", | ||
"version": "1.1.11", | ||
"version": "1.1.12", | ||
"main": "lib/index.js", | ||
@@ -31,3 +31,3 @@ "bin": { | ||
"dependencies": { | ||
"@charlietango/react-docs-net": "^1.1.11", | ||
"@charlietango/react-docs-net": "^1.1.12", | ||
"chalk": "^2.3.1", | ||
@@ -34,0 +34,0 @@ "del": "^3.0.0", |
# React Docs | ||
Generate docs from React components and flow type view model files. | ||
## Usage | ||
Add the dependency | ||
```sh | ||
yarn add @charlietango/react-doc --dev | ||
``` | ||
Generate JSON files with react-docgen, and process them: | ||
```js | ||
const docs = require('@charlietango/react-docs'); | ||
docs('src/models/**/*.json', { | ||
dest: 'dist/models', // Add dest to write to files | ||
outputMarkdown: true, | ||
}); | ||
``` | ||
or calling the `bin` | ||
```sh | ||
$ react-doc src/{components,types}/**/*.js --dest dist/models --md | ||
``` | ||
#### Warning for CSharp models | ||
Generating CSharp models is only supported for Flow files. | ||
This is a basic rewrite of the React props to C#/.NET. No validation is done on the actual files. | ||
Not all Flow features are supported, since there's not a simple way to convert them to C#. | ||
* Flow `number` is converted to `int`. Use `@type {TYPE}` in comment tag for the prop, to change the number type. | ||
### Config | ||
| Name | Type | Default | Description | | ||
| --------- | ------- | ------- | --------------------------------------------------- | | ||
| dest | string | | Write all output files to this path | | ||
| namespace | string | | Namespace to use when generating CSharp view models | | ||
| verbose | boolean | false | Output extra logging | | ||
| quiet | boolean | false | Don't output anything to log | | ||
| Name | Type | Default | Description | | ||
| -------------- | ----------------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------- | | ||
| dest | string | | Write all output files to this path | | ||
| componentsDir | string / Array<string> | ['components', 'modules'] | Paths that contains directories with react components. Used for filtering out non components | | ||
| filter | (file: string) => boolean / boolean | true | Custom filter method to use instead of the default. Set to `false` to skip filtering. | | ||
| globOptions | Object | | Options to to use for the globbing method | | ||
| outputMarkdown | boolean / string | false | If `true`, will output Markdown to `dest`. If a string, the value will be used as the output path | | ||
| outputJson | boolean / string | false | If `true`, will output JSON to `dest`. If a string, the value will be used as the output path | | ||
| outputCSharp | boolean / string | false | If `true`, will output CSharp to `dest`. If a string, the value will be used as the output path | | ||
| namespace | string | | Namespace to use when generating CSharp view models | | ||
| verbose | boolean | false | Output extra logging | | ||
| quiet | boolean | false | Don't output anything to log | | ||
| throwOnWarning | boolean | false | Throw an error if warnings are logged | | ||
@@ -18,1 +60,24 @@ ### JSDoc flags | ||
* `@type` - Set a specific C# type for this prop - Like `decimal` | ||
## Example input files | ||
In addition to any React style files that `react-docgen` can handle, there is a fallback that will attempt to extract a single Flowtype from a file, and create a model from it. | ||
This is done by using the type as the `props` for a React Component. | ||
#### `ImageModel.js` | ||
```js | ||
// @flow | ||
/** | ||
* Description of the model | ||
**/ | ||
export type ImageModel = { | ||
/** Image src URL */ | ||
src: string, | ||
/** Original image height */ | ||
height?: number, | ||
/** Original image width */ | ||
width?: number, | ||
}; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22518
17.65%304
1%83
361.11%