-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
index.js
executable file
Β·461 lines (385 loc) Β· 12.2 KB
/
index.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const Vue = require('vue')
const Vuetify = require('vuetify')
const fs = require('fs')
const map = require('./helpers/map')
const deepmerge = require('./helpers/merge')
const pkg = require('../package.json')
const camelizeRE = /-(\w)/g
const camelize = str => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
}
const capitalize = str => {
return str.charAt(0).toUpperCase() + str.slice(1)
}
const hyphenateRE = /\B([A-Z])/g
function hyphenate (str) {
return str.replace(hyphenateRE, '-$1').toLowerCase()
}
Vue.use(Vuetify)
function parseFunctionParams (func) {
const groups = /function\s_.*\((.*)\)\s\{.*/i.exec(func)
if (groups && groups.length > 1) return `(${groups[1]}) => {}`
else return 'null'
}
function getPropType (type) {
if (Array.isArray(type)) {
return type.map(t => getPropType(t))
}
if (!type) return 'any'
return type.name.toLowerCase()
}
function getPropDefault (def, type) {
if (def === '' ||
(def == null && type !== 'boolean' && type !== 'function')
) {
return 'undefined'
} else if (typeof (def) === 'function' && type !== 'function') {
def = def.call({})
}
if (type === 'boolean') {
return def ? 'true' : 'false'
}
if (type === 'string') {
return def ? `'${def}'` : def
}
if (type === 'function') {
return parseFunctionParams(def)
}
return def
}
function getPropSource (name, mixins) {
const source = null
for (let i = 0; i < mixins.length; i++) {
let mixin = mixins[i]
if (mixin.name !== 'VueComponent') mixin = Vue.extend(mixin)
if (mixin.options.name) {
const source = Object.keys(mixin.options.props || {}).find(p => p === name) && mixin.options.name
const found = getPropSource(name, [mixin.super].concat(mixin.options.extends).concat(mixin.options.mixins).filter(m => !!m)) || source
if (found) return hyphenate(found)
}
}
return source
}
function genProp (name, prop, mixins, cmp) {
const type = getPropType(prop.type)
const source = getPropSource(name, mixins) || cmp
return {
name,
type,
default: getPropDefault(prop.default, type),
source,
}
}
function parseComponent (component) {
return {
props: parseProps(component),
mixins: parseMixins(component),
}
}
function parseProps (component, array = [], mixin = false) {
const options = component.options
const mixins = [component.super].concat(options.extends).concat(options.mixins).filter(m => !!m)
const props = options.props || {}
Object.keys(props).forEach(key => {
const generated = genProp(key, props[key], mixins, component.options.name)
array.push(generated)
})
return array.sort((a, b) => a.name > b.name)
}
function parseMixins (component) {
if (!component.options.mixins) return []
let mixins = []
for (let i = 0; i < component.options.mixins.length; i++) {
let mixin = component.options.mixins[i]
if (mixin.name !== 'VueComponent') mixin = Vue.extend(mixin)
if (mixin.options.name) {
mixins.push(mixin.options.name)
if (mixin.options.mixins) {
mixins = mixins.concat(parseMixins(mixin))
}
}
}
return mixins.sort((a, b) => a > b)
}
function processVariableFile (path) {
if (fs.existsSync(path)) {
const varFile = fs.readFileSync(path, 'utf8')
const vars = varFile.split(/;[\n]*/g)
const varValues = []
for (const [ind, varString] of vars.entries()) {
const varArr = varString.split(':')
if (varArr.length >= 2 && varArr[0].charAt(0) === '$') {
const varName = varArr.shift().trim()
let varDefault = (vars[ind + 1].charAt(0) === '@')
? vars[ind + 1]
: varArr.join(':')
varDefault = `${varDefault.trim()};`
const lastIndex = varValues.findIndex(item => item.name === varName)
if (lastIndex > -1) {
varValues[lastIndex].default = varDefault
} else {
varValues.push({
name: varName,
default: varDefault,
})
}
}
}
return varValues
}
}
function parseVariables () {
const variables = {}
const varPaths = [
{ path: './../vuetify/src/styles/settings/_variables.scss', tag: 'globals' },
{ path: './../vuetify/src/styles/settings/_light.scss', tag: 'light' },
{ path: './../vuetify/src/styles/settings/_dark.scss', tag: 'dark' },
]
// component dir
const rootDir = './../vuetify/src/components'
const comps = fs.readFileSync(`${rootDir}/index.ts`, 'utf8')
const folders = comps
.trim()
.replace(/export\s\*\sfrom\s'.\//g, '')
.split(`'\n`)
for (const folder of folders) {
varPaths.push({ path: `${rootDir}/${folder}/_variables.scss`, tag: hyphenate(folder) })
}
// process all variable paths
for (const varPath of varPaths) {
variables[varPath.tag] = processVariableFile(varPath.path)
}
return variables
}
const components = {}
const directives = {}
const installedComponents = Vue.options._base.options.components
const installedDirectives = Vue.options._base.options.directives
const componentNameRegex = /^(?:V[A-Z]|v-[a-z])/
const excludes = ['VMessages', 'VLabel']
for (const name in installedComponents) {
if (!componentNameRegex.test(name)) continue
if (excludes.includes(name)) continue
let component = installedComponents[name]
if (component.options.$_wrapperFor) {
component = component.options.$_wrapperFor
}
const kebabName = hyphenate(name)
let options = parseComponent(component)
if (map[kebabName]) {
options = deepmerge(options, map[kebabName])
}
components[kebabName] = options
}
for (const key of ['Mutate', 'Intersect', 'Ripple', 'Resize', 'Scroll', 'Touch', 'ClickOutside']) {
if (!installedDirectives[key]) continue
const lowerCaseVersion = hyphenate(key).toLowerCase()
const vKey = `v-${lowerCaseVersion}`
const directive = map[vKey]
directive.type = getPropDefault(directive.default, directive.type)
directives[vKey] = directive
}
function writeApiFile (obj, file) {
const stream = fs.createWriteStream(file)
const comment = `/*
* THIS FILE HAS BEEN AUTOMATICALLY GENERATED USING THE API-GENERATOR TOOL.
*
* CHANGES MADE TO THIS FILE WILL BE LOST!
*/
`
stream.once('open', () => {
stream.write(comment)
stream.write('module.exports = ')
stream.write(JSON.stringify(obj, null, 2))
stream.write('\n')
stream.end()
})
}
function writeJsonFile (obj, file) {
const stream = fs.createWriteStream(file)
stream.once('open', () => {
stream.write(JSON.stringify(obj, null, 2))
stream.end()
})
}
function writePlainFile (content, file) {
const stream = fs.createWriteStream(file)
stream.once('open', () => {
stream.write(content)
stream.end()
})
}
const tags = Object.keys(components).reduce((t, k) => {
t[k] = {
attributes: components[k].props.map(p => p.name.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`)).sort(),
description: '',
}
return t
}, {})
const attributes = Object.keys(components).reduce((attrs, k) => {
const tmp = components[k].props.reduce((a, prop) => {
let type = prop.type
if (!type) type = ''
else if (Array.isArray(type)) type = type.map(t => t.toLowerCase()).join('|')
else type = type.toLowerCase()
const name = prop.name.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`)
a[`${k}/${name}`] = {
type,
description: '',
}
return a
}, {})
return Object.assign(attrs, tmp)
}, {})
const fakeComponents = ts => {
const imports = [
`import Vue from 'vue'`,
]
if (ts) imports.push(`import { PropValidator } from 'vue/types/options'`)
const inspection = ts ? '' : `// noinspection JSUnresolvedFunction\n`
return `${imports.join('\n')}\n\n` + Object.keys(components).map(component => {
const propType = type => {
if (type === 'any' || typeof type === 'undefined') return ts ? 'null as any as PropValidator<any>' : 'null'
if (Array.isArray(type)) return `[${type.map(s => s.charAt(0).toUpperCase() + s.slice(1)).join(',')}]`
return type.charAt(0).toUpperCase() + type.slice(1)
}
const quoteProp = name => name.match(/-/) ? `'${name}'` : name
const componentProps = components[component].props
componentProps.sort((a, b) => {
if (a.name < b.name) return -1
return a.name === b.name ? 0 : 1
})
let props = componentProps.map(prop => ` ${quoteProp(prop.name)}: ${propType(prop.type)}`).join(',\n')
if (props) props = `\n props: {\n${props}\n }\n`
return `${inspection}Vue.component('${component}', {${props}})`
}).join('\n')
}
const variables = parseVariables()
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist', 0o755)
}
writeJsonFile(tags, 'dist/tags.json')
writeJsonFile(attributes, 'dist/attributes.json')
writeJsonFile(variables, 'dist/variables.json')
writePlainFile(fakeComponents(false), 'dist/fakeComponents.js')
writePlainFile(fakeComponents(true), 'dist/fakeComponents.ts')
// Create web-types.json to provide autocomplete in JetBrains IDEs
const webTypes = {
$schema: 'https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json',
framework: 'vue',
name: 'vuetify',
version: pkg.version,
contributions: {
html: {
'types-syntax': 'typescript',
tags: [],
attributes: [],
},
},
}
components['$vuetify'] = map['$vuetify']
components['internationalization'] = map['internationalization']
writeApiFile({ ...components, ...directives }, 'dist/api.js')
delete components['$vuetify']
delete components['internationalization']
Object.keys(components).forEach(function (key) {
const name = capitalize(camelize(key))
const attributes = mapArray(components[key].props, transformAttribute)
const events = mapArray(components[key].events, transformEvent)
const slots = mapArray(components[key].slots, transformSlot)
const tag = {
name,
source: { module: './src/components/index.ts', symbol: name },
attributes,
events,
slots,
}
webTypes.contributions.html.tags.push(tag)
function mapArray (arr, mapper) {
return arr !== undefined ? arr.map(mapper) : undefined
}
function transformAttribute (attr) {
attr = copyObject(attr)
delete attr['source']
if (attr['type']) {
attr['value'] = {
kind: 'expression',
type: attr['type'],
}
if (attr['type'] !== 'boolean') {
delete attr['type']
}
}
if (attr['default'] !== undefined) {
attr['default'] = JSON.stringify(attr['default'])
}
delete attr['example']
return attr
}
function transformEvent (event) {
event = copyObject(event)
if (event['value'] !== undefined) {
const type = event['value']
event.arguments = [{
name: 'argument',
type: typeof type === 'string' ? type : JSON.stringify(type),
}]
}
delete event['value']
delete event['source']
return event
}
function transformSlot (slot) {
slot = copyObject(slot)
if (slot['props'] !== undefined) {
const props = []
Object.keys(slot['props']).forEach(function (name) {
const type = slot['props'][name]
props.push({
name,
type: typeof type === 'string' ? type : JSON.stringify(type),
})
})
slot['vue-properties'] = props
}
delete slot['props']
delete slot['source']
return slot
}
function copyObject (obj) {
const result = {}
if (typeof obj === 'string') {
result['name'] = obj
} else {
Object.keys(obj).forEach(function (name) {
result[name] = obj[name]
})
}
return result
}
})
Object.keys(directives).forEach(function (key) {
const name = key
const directive = directives[key]
const modifiers = []
let valueType
let defaultValue
for (const option of directive.options || []) {
if (option.name.indexOf('modifiers.') === 0) {
modifiers.push({
name: option.name.substr('modifiers.'.length),
})
} else if (option.name === 'value') {
valueType = option.type
defaultValue = option.default
}
}
webTypes.contributions.html.attributes.push({
name,
source: { module: './src/directives/index.ts', symbol: capitalize(name.substr(2)) },
default: defaultValue,
value: valueType ? { kind: 'expression', type: valueType } : undefined,
'vue-modifiers': modifiers.length > 0 ? modifiers : undefined,
})
})
writeJsonFile(webTypes, 'dist/web-types.json')