forked from rebassjs/rebass
/
generate-examples.js
executable file
·292 lines (258 loc) · 7.62 KB
/
generate-examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const fs = require('fs')
const path = require('path')
const systemDocs = require('system-docs')
const RebassNative = require('./dist')
const keys = Object.keys(RebassNative)
const examples = fs.readdirSync(
path.join(__dirname, './examples/jsx')
)
.reduce((a, b) => Object.assign({}, a, {
[path.basename(b, path.extname(b))]: path.join(__dirname, './examples/jsx', b)
}), {})
const template = ({
components = []
}) => `
# Components
<details>
<summary>Table of Contents</summary>
<ul>
${components.map(({ name }) => `<li><a href='#${name.toLowerCase()}'>${name}</a></li>`).join('\n')}
</ul>
</details>
${components
.map(comp => {
if (!comp.example) console.log(`no example for:`, comp.name)
return comp
})
.map(({ name, props, example, extension }) => (
`## ${name}
${extension ? `Extends [${extension}](#${extension.toLowerCase()})\n` : ''}
${'```.jsx\n' + example + '\n```\n\n'}
${props.length ? '### Props\n' : ''}
${props.map(prop => (
`- \`${prop.name}\``
)).join('\n')}
`)).join('\n')}
`.replace(/\n\n+/g, '\n\n')
const propBlacklist = {
m: true,
mt: true,
mr: true,
mb: true,
ml: true,
mx: true,
my: true,
p: true,
pt: true,
pr: true,
pb: true,
pl: true,
px: true,
py: true,
color: true,
bg: true,
w: true,
f: true
}
const moduleBlacklist = {
createColors: true,
invertTheme: true
}
const components = keys
.filter(key => !moduleBlacklist[key])
.map(key => {
const Component = RebassNative[key]
const {
propTypes = {},
defaultProps = {}
} = Component || {}
let extension
if (defaultProps && defaultProps.is) {
extension = typeof defaultProps.is === 'function'
? defaultProps.is.displayName
: null
}
let example
if (examples[key]) {
example = fs.readFileSync(examples[key], 'utf8')
}
const props = Object.keys(propTypes)
.filter(key => !propBlacklist[key])
.map(key => ({
name: key
}))
return {
name: key,
extension,
Component,
props,
example
}
})
.filter(c => typeof c.Component === 'function')
const content = template({ components })
fs.writeFileSync(path.join(__dirname, 'docs', 'components', 'list.md'), content)
const propsTableColumns = [
'prop',
'default',
'theme key'
// 'style type'
]
const createMarkdownPropsTable = docs => {
const { propTypes = {}, defaultProps = {} } = docs
const keys = Object.keys(propTypes)
if (!keys.length) return ''
const head = [
propsTableColumns.join(' | '),
propsTableColumns.map(n => '---').join('|')
].join('\n')
const rows = keys.map(key => {
const prop = propTypes[key] || {}
return [
key,
defaultProps[key] || '',
prop.themeKey || 'N/A'
// prop.styleType || 'N/A'
].join(' | ')
})
return [ head, ...rows ].join('\n')
}
const createJsxPropsTable = docs => {
const { propTypes = {}, defaultProps = {} } = docs
const keys = Object.keys(propTypes)
if (!keys.length) return ''
const Column = `RebassNative.Column flexGrow={1} width={1/${propsTableColumns.length}}`
const head = `
${propsTableColumns.map(col => `<${Column}>
<RebassNative.Lead>${col}</RebassNative.Lead>
</RebassNative.Column>`).join('\n')}
`
const rows = keys.map(key => {
const prop = propTypes[key] || {}
return `
${[
key,
defaultProps[key] || '',
prop.themeKey || 'N/A'
// prop.styleType || 'N/A'
].map(col => `<${Column}>${col}</RebassNative.Column>`).join('\n')}
`
})
return [
'<RebassNative.Row flexWrap="wrap" justifyContent="space-between" px={4}>',
head,
...rows,
'</RebassNative.Row>'
].join('\n')
}
const getName = component => component.displayName ||
component.name ||
component
const getMarkdownExtensions = ({ extensions = [] }, originalName) => {
if (!extensions.length) return ''
const names = extensions.map(getName)
const links = names.map(name => `[${name}](${name === originalName
? `http://jxnblk.com/rebass/components/${name}`
: `/components/${name}`})`)
return 'Extends: ' + links.join(' > ')
}
const getJsxExtensions = ({ extensions = [] }, originalName) => {
if (!extensions.length) return ''
const names = extensions.map(getName)
const links = names
.map(name => `<RebassNative.ButtonOutline onPress={() => {
${name === originalName
? `// open http://jxnblk.com/rebass/components/${name}`
: `this.props.navigation.navigate('${name}Screen')`}
}}>${name}</RebassNative.ButtonOutline>`)
return links.join('<RebassNative.Text> → </RebassNative.Text>')
}
// build individual component files
Object.keys(RebassNative)
.filter(name => name[0].toUpperCase() === name[0])
.forEach(name => {
if (name === 'index') return
const docs = systemDocs(RebassNative[name])
const example = examples[name] ? fs.readFileSync(examples[name], 'utf8')
: (docs.extensions || []).map(getName).some(ext => ext === 'Text')
? `<${name} children="Example"/>` : `<${name}/>`
const table = createMarkdownPropsTable(docs)
const extensions = getMarkdownExtensions(docs, name)
const content = [
'# ' + name,
'```.jsx\n' + example + '\n```\n\n',
extensions,
table
]
.filter(Boolean)
.join('\n\n')
const file = path.join(__dirname, 'docs', 'components', name + '.md')
fs.writeFileSync(file, content)
fs.writeFileSync(path.join(__dirname, 'examples', 'app', 'screens', name + 'Screen.js'), `
import React from 'react';
import {ScrollView} from 'react-native'
import * as RebassNative from 'rebass-native'
export class ${name}Screen extends React.Component {
static navigationOptions = {
title: '${name}',
};
render () {
return (
<ScrollView>
<RebassNative.Subhead mx={4} my={2}>Demo</RebassNative.Subhead>
<RebassNative.Banner bg="white">
${example.replace(/<\//g, '</RebassNative.').replace(/(<)([^/])/g, '<RebassNative.$2')}
</RebassNative.Banner>
<RebassNative.Subhead mx={4} my={2}>Code</RebassNative.Subhead>
<RebassNative.Code bg="black" color="lime" p={4}>{\`${example}\`}</RebassNative.Code>
<RebassNative.Subhead mx={4} my={2}>Inheritance</RebassNative.Subhead>
<RebassNative.Flex px={4} flexDirection="row">${getJsxExtensions(docs, name)}</RebassNative.Flex>
<RebassNative.Subhead mx={4} my={2}>Props</RebassNative.Subhead>
${createJsxPropsTable(docs)}
</ScrollView>
)
}
}
`)
})
console.log('Generated docs/components/list.md')
fs.writeFileSync(path.join(__dirname, 'examples', 'app', 'RootStack.js'), `
import { createStackNavigator } from 'react-navigation'
import {IndexScreen} from './IndexScreen.js'
${components
.map(getName)
.map(name => `import {${name}Screen} from './screens/${name}Screen.js'`)
.join('\n')}
export const RootStack = createStackNavigator(
{
IndexScreen,
${components.map(getName).map(name => `${name}Screen`).join(',\n ')}
},
{
initialRouteName: 'IndexScreen'
}
)
`)
fs.writeFileSync(path.join(__dirname, 'examples', 'app', 'IndexScreen.js'), `
import React, { Component } from 'react'
import {
Flex,
ButtonTransparent
} from 'rebass-native'
import {ScrollView} from 'react-native'
export class IndexScreen extends Component {
static navigationOptions = {
title: 'Rebass Native',
};
render () {
return (<ScrollView>
${components.map(getName).map(name => `
<ButtonTransparent
textProps={{color: 'black', textAlign: 'left'}}
onPress={() => this.props.navigation.navigate('${name}Screen')}
>${name}</ButtonTransparent>
`).join('\n')}
</ScrollView>)
}
}
`)