-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
gen-web-types.js
137 lines (127 loc) · 3.35 KB
/
gen-web-types.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
// I don't like web-types, why can't webstrom just work with typescript?
const fs = require('fs')
const path = require('path')
const { kebabCase } = require('lodash')
exports.genWebTypes = function genWebTypes () {
const components = require('../../lib/components')
const { default: version } = require('../../lib/version')
const elements = []
const scaffold = {
$schema:
'https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json',
framework: 'vue',
name: 'naive-ui',
version,
contributions: {
html: {
// we need to fill tags
elements,
attributes: [],
'js-types-syntax': 'typescript'
}
}
}
const ignoredPropNames = ['theme', 'themeOverrides', 'builtinThemeOverrides']
Object.entries(components).forEach(([exportName, component]) => {
if (exportName[0] !== 'N') return
if (exportName.startsWith('Nx')) return
const { props } = component
const name = /^NH\d$/.test(exportName)
? exportName.replace('NH', 'n-h')
: kebabCase(exportName)
const slots = []
const attributes = []
const events = []
props &&
Object.entries(props).forEach(([propName, prop]) => {
if (propName.startsWith('internal')) return
if (ignoredPropNames.includes(propName)) return
if (propName.startsWith('on') && /[A-Z]/.test(propName[2])) {
// is event
events.push({
name: kebabCase(propName.slice(2)),
description: '-'
})
} else if (prop === null) {
attributes.push({
name: kebabCase(propName),
default: '-',
description: '-',
kind: 'expression'
})
} else {
const attribute = {
name: kebabCase(propName),
default: '-',
description: '-'
}
const type = getType(prop)
if (type !== null) {
attribute.value = {
type,
kind: 'expression'
}
}
// is attribute
attributes.push(attribute)
}
})
elements.push({
name,
slots,
attributes,
'js/events': events
})
})
fs.writeFileSync(
path.resolve(__dirname, '../../web-types.json'),
JSON.stringify(scaffold, 0, 2),
{
encoding: 'utf-8'
}
)
function getType (prop) {
if (typeof prop !== 'object' && typeof prop !== 'function') {
console.error(`invalid prop: ${prop}`)
return null
}
if ('type' in prop) {
return _getType(prop.type)
}
if ('validator' in prop) {
return null
}
return _getType(prop)
}
function _getType (propType) {
if (Array.isArray(propType)) {
const types = propType.map(mapType)
if (types.some((v) => v === null)) return null
return types.join(' | ')
}
return mapType(propType)
}
function mapType (type) {
switch (type) {
case String:
return 'string'
case Number:
return 'number'
case Boolean:
return 'boolean'
case Array:
return 'Array'
case Object:
return 'object'
case Function:
return 'Function'
case Date:
return 'Date'
}
console.error(`unkown type ${type}`)
return null
}
}
if (require.main === module) {
exports.genWebTypes()
}