forked from quasarframework/quasar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.web-types.js
168 lines (161 loc) · 5.7 KB
/
build.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
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
const
path = require('path'),
fs = require('fs'),
{ logError, writeFile, kebabCase } = require('./build.utils')
function resolveType ({ type, values }) {
// TODO transform Object with "values" and arrays Objects with values
if (Array.isArray(type)) {
return type.map(type => resolveType({ type })).join('|')
}
if (type === 'String' && values) {
return values.map(v => v === null ? 'null' : `'${v}'`).join('|')
}
if (['Any', 'String', 'Boolean', 'Number', 'Object'].includes(type)) {
return type.toLowerCase()
}
if (type === 'Array') {
return 'any[]'
}
return type
}
function getDescription (propApi) {
return propApi.examples
? propApi.desc + '\n\nExamples:\n' + propApi.examples.join('\n')
: propApi.desc
}
module.exports.generate = function (data) {
try {
const webtypes = JSON.stringify({
$schema: '',
framework: 'vue',
name: 'quasar',
version: process.env.VERSION || require('../package.json').version,
contributions: {
html: {
'types-syntax': 'typescript',
tags: data.components.map(({ api: { events, props, scopedSlots, slots, meta }, name }) => {
let slotTypes = []
if (slots) {
Object.entries(slots).forEach(([name, slotApi]) => {
slotTypes.push({
name,
description: getDescription(slotApi),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
})
})
}
if (scopedSlots) {
Object.entries(scopedSlots).forEach(([name, slotApi]) => {
slotTypes.push({
name,
'vue-properties': slotApi.scope && Object.entries(slotApi.scope).map(([name, api]) => ({
name,
type: resolveType(api),
description: getDescription(api),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
})),
description: getDescription(slotApi),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
})
})
}
const result = {
name,
source: {
module: 'quasar',
symbol: name
},
attributes: props && Object.entries(props).map(([name, propApi]) => {
let result = {
name,
value: {
kind: 'expression',
type: resolveType(propApi)
},
description: getDescription(propApi),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
}
if (propApi.required) {
result.required = true
}
if (propApi.default) {
result.default = JSON.stringify(propApi.default)
}
if (propApi.type === 'Boolean') {
// Deprecated but used for compatibility with WebStorm 2019.2.
result.type = 'boolean'
}
return result
}),
events: events && Object.entries(events).map(([name, eventApi]) => ({
name,
arguments: eventApi.params && Object.entries(eventApi.params).map(([paramName, paramApi]) => ({
name: paramName,
type: resolveType(paramApi),
description: getDescription(paramApi),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
})),
description: getDescription(eventApi),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
})),
slots: slotTypes,
description: `${name} - Quasar component`,
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
}
if (props && props.value && ((events && events.input) || props.value.category === 'model')) {
result['vue-model'] = {
prop: 'value',
event: 'input'
}
}
Object.entries(result).forEach(([key, v]) => {
if (!v) {
delete result[key]
}
})
return result
}),
attributes: data.directives.map(({ name, api: { modifiers, value, meta } }) => {
let valueType = value.type
let result = {
name: 'v-' + kebabCase(name),
source: {
module: 'quasar',
symbol: name
},
required: false, // Directive is never required
description: `${name} - Quasar directive`,
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
}
if (modifiers) {
result['vue-modifiers'] = Object.entries(modifiers).map(([name, api]) => ({
name,
description: getDescription(api),
'doc-url': meta.docsUrl || 'https://v1.quasar.dev'
}))
}
if (valueType !== 'Boolean') {
result.value = {
kind: 'expression',
type: resolveType(value)
}
}
return result
})
}
}
}, null, 2)
let webTypesPath = path.resolve(__dirname, '../dist/web-types')
if (!fs.existsSync(webTypesPath)) {
fs.mkdirSync(webTypesPath)
}
writeFile(path.resolve(webTypesPath, 'web-types.json'), webtypes)
}
catch (err) {
logError(`build.web-types.js: something went wrong...`)
console.log()
console.error(err)
console.log()
process.exit(1)
}
}