1
1
#!/usr/bin/env node
2
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
3
'use strict'
3
4
4
5
const docgen = require ( 'react-docgen-typescript' )
5
6
const fs = require ( 'fs' ) . promises
6
7
const path = require ( 'path' )
7
8
const globby = require ( 'globby' )
9
+ const pkg = require ( '../package.json' )
8
10
9
- // These are the filetypes we only care about replacing the version
10
11
const GLOB = [ 'src/components/**/*.tsx' ]
11
12
const GLOBBY_OPTIONS = {
12
13
cwd : path . join ( __dirname , '..' ) ,
@@ -22,20 +23,35 @@ const options = {
22
23
async function createMdx ( filename , name , props ) {
23
24
if ( typeof props === 'undefined' ) return
24
25
25
- // let content = `### ${name}\n\n| Prop name | Description | Type |\n| --- | --- | --- |\n`
26
- let content = `| Prop name | Description | Type |\n | --- | --- | --- |\n`
26
+ let content = `| Property | Description | Type | Default |\n`
27
+ content + = `| --- | --- | --- | --- |\n`
27
28
28
29
for ( const [ key , value ] of Object . entries ( props ) . sort ( ) ) {
29
- if ( value . parent . fileName !== 'react/node_modules/@types/react/index.d.ts' ) {
30
- content += `| ${ value [ 'name' ] } | ${ value [ 'description' ] } | ${ value [ 'type' ] [ 'name' ] } |\n`
31
- console . log ( `${ key } : ${ value } ` )
30
+ if (
31
+ value . parent . fileName !== 'react/node_modules/@types/react/index.d.ts' &&
32
+ ! value [ 'description' ] . includes ( '@ignore' )
33
+ ) {
34
+ const name = value . name || ''
35
+ const description =
36
+ value . description . replaceAll ( '\n' , '<br/>' ) . replaceAll ( ' [docs]' , '' ) || '-'
37
+ const type =
38
+ value . type ? value . type . name . includes ( 'ReactElement' )
39
+ ? 'ReactElement'
40
+ : value . type . name : ''
41
+ const defaultValue = value . defaultValue ? value . defaultValue . value : '-'
42
+
43
+ content += `| **${ name } ** | ${ description } | \`${ type } \` | ${ defaultValue } |\n`
44
+ console . log ( `${ filename } - ${ key } ` )
32
45
}
33
46
}
34
47
35
- await fs . writeFile ( `api/${ filename } .api.mdx` , content , { encoding : 'utf8' } ) . then ( ( ) => {
36
- // Do whatever you want to do.
37
- console . log ( 'Done' )
38
- } )
48
+ await fs
49
+ . writeFile ( `docs/${ pkg . config . version_short } /api/${ filename } .api.mdx` , content , {
50
+ encoding : 'utf8' ,
51
+ } )
52
+ . then ( ( ) => {
53
+ console . log ( `File created: ${ filename } .api.mdx` )
54
+ } )
39
55
}
40
56
41
57
async function main ( ) {
@@ -49,8 +65,6 @@ async function main() {
49
65
const filename = path . basename ( file , '.tsx' )
50
66
createMdx ( filename , props [ 0 ] . displayName , props [ 0 ] . props )
51
67
}
52
- // const props = docgen.parse(file, options)
53
- // if (props) console.log(props)
54
68
} ) ,
55
69
)
56
70
} catch ( error ) {
0 commit comments