This repository was archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathcomponent-metadata.ts
229 lines (187 loc) · 7.56 KB
/
component-metadata.ts
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
const fs = require('fs-extra')
const glob = require('glob')
const md5File = require('md5-file')
import * as docgen from 'react-docgen-typescript'
const chokidar = require('chokidar')
const { info, warn } = require('signale')
const camelCase = require('lodash.camelcase')
const getMetadata = require('./get-metadata')
const { icons } = require('@auth0/cosmos/atoms/icon/icons.json')
const colors = require('@auth0/cosmos/tokens/colors')
/* CLI param for watch mode */
const watch = process.argv.includes('-w') || process.argv.includes('--watch')
const debug = process.argv.includes('-d') || process.argv.includes('--debug')
let warning = 0
/* Cache file metadata in memory for comparison on subsequent recompiles */
const jsCache = []
const mdCache = []
/* Ensure meta directory exists */
fs.ensureDirSync('core/components/meta')
/* Get list of ts and md files from atoms and molecules */
const javascriptFiles = glob.sync('core/components/+(atoms|molecules|layouts)/**/*.ts*')
let markdownFiles = glob.sync('core/components/+(atoms|molecules|layouts)/**/*.md')
const run = (event: string, srcPath?: string) => {
let warning = 0
if (event === 'ready') {
info('Generating metadata')
} else {
info(`Updating metadata for ${event === 'change' ? 'changed' : 'unlinked'} path ${srcPath}`)
}
javascriptFiles
/* Get all files on first run or only those matching the srcPath from change/unlink */
.filter(path => !srcPath || path.includes(srcPath.replace('.md', '')))
/* Ignore stories and type defs */
.filter(path => !path.includes('story.tsx') || !path.includes('.d.ts'))
.forEach(path => {
try {
/* ignore secondary files */
const directoryName = path.split('/').splice(-2, 1)[0]
const componentFileName = directoryName.replace('_', '') + '.tsx'
/* if component file does not exist, move on */
if (!path.includes(componentFileName)) return
/* get documentation file path */
const documentationPath = path.replace('.tsx', '.md')
const cmpFileHash = md5File.sync(path)
const docFileHash = fs.existsSync(documentationPath)
? md5File.sync(documentationPath)
: null
let cacheEntry = jsCache.find(f => f.name === componentFileName)
/* If component file and corresponding doc file are unchanged, move on */
if (cacheEntry && cacheEntry.hash === cmpFileHash && cacheEntry.docHash === docFileHash) {
return
}
if (!cacheEntry) {
cacheEntry = {
name: componentFileName
}
jsCache.push(cacheEntry)
}
cacheEntry.hash = cmpFileHash
cacheEntry.docHash = docFileHash
/* parse the component code to get metadata */
const data: any = docgen.parse(path, {
componentNameResolver: (exp, source) => source.text.match(/export default (\w+)/)[1]
})[0]
if (debug) console.log({ data })
/* make modifications to prop types to improve documentation */
if (data.props) {
Object.keys(data.props).forEach(propName => {
const prop = data.props[propName]
/* remove redundant quotes from default value of type string */
if (prop.defaultValue) {
if (typeof prop.defaultValue.value === 'string') {
prop.defaultValue.value = prop.defaultValue.value.replace(/^'(.*)'$/, '$1')
}
}
if (prop.type.name === 'enum' && prop.type.value === '__ICONNAMES__') {
/* create an array of all the icons with an empty string as first element */
prop.type.value = [{ value: '' }].concat(Object.keys(icons).map(value => ({ value })))
}
if (prop.type.name === 'enum' && prop.type.value === '__COLORS__') {
/* create an array of all the base colors */
prop.type.value = Object.keys(colors.base).map(value => ({ value }))
}
/* remove redundant quotes from enum values in prop types */
if (prop.type.name === 'enum' && Array.isArray(prop.type.value)) {
prop.type.value.forEach(element => {
element.value = element.value.replace(/^'(.*)'$/, '$1')
})
}
/* add required field for custom shapes */
if (prop.type.name === 'custom' && prop.type.raw.includes('.isRequired')) {
prop.required = true
prop.type.raw = prop.type.raw.replace('.isRequired', '')
}
})
}
/* add filepath to metadata */
data.filepath = path
/* add documentation if exists */
if (fs.existsSync(documentationPath)) {
data.documentation = fs.readFileSync(documentationPath, 'utf8')
/* remove from markdown files list (useful later) */
markdownFiles = markdownFiles.filter(path => path !== documentationPath)
/* pull meta from docs */
data.meta = getMetadata(documentationPath)
} else if (debug) {
warn('documentation not found for ' + path)
} else {
warning++
}
/* add lazy hint for documentation */
data.implemented = true
data.internal = path.includes('_')
cacheEntry.data = data
} catch (err) {
/* warn if there was a problem with getting metadata */
if (debug) warn(`Could not parse metadata for ${path}: ${err.stack || err}`)
else warning++
}
})
info('metadata done')
/* Add documentation files that are not implemented yet */
markdownFiles
.filter(path => !srcPath || path.includes(srcPath))
.forEach(path => {
const data: any = {}
/* infer display name from path */
data.displayName = camelCase(
path
.split('/')
.pop()
.replace('.md', '')
)
const currentHash = md5File.sync(path)
let cacheEntry = mdCache.find(f => f.data.displayName === data.displayName)
if (cacheEntry && cacheEntry.hash === currentHash) {
return
}
if (!cacheEntry) {
cacheEntry = { data }
mdCache.push(cacheEntry)
}
cacheEntry.hash = currentHash
/* attach content of documentation file */
data.documentation = fs.readFileSync(path, 'utf8')
/* attach temporary filepath */
data.filepath = path
/* add lazy hint for documentation */
data.implemented = false
})
/*
Write the file in docs folder
TODO: Rethink tooling for docs which works across packages
*/
const metadata = [...jsCache, ...mdCache].map(entry => entry.data)
fs.writeFileSync(
'core/components/meta/metadata.json',
JSON.stringify({ metadata }, null, 2),
'utf8'
)
// Write a version of the Changelog to a place where we can access it later.
// TODO: Consider parsing the Markdown and storing this in a more structured format
// so we can display it more intelligently in the docs?
info('Generating changelog')
const changelog = fs.readFileSync('changelog.md', 'utf8')
fs.writeFileSync(
'core/components/meta/changelog.json',
JSON.stringify({ changelog }, null, 2),
'utf8'
)
if (event === 'ready' && warning) {
warn(`${warning} components could use some docs love, run in --debug mode for more info`)
}
}
/* watch mode 👀 */
if (watch) {
console.log('running in watch mode')
chokidar
.watch('core/components', {
ignored: ['node_modules', 'core/components/meta']
})
.on('ready', () => run('ready'))
.on('change', path => run('change', path))
.on('unlink', path => run('unlink', path))
} else {
run('ready')
}