This repository was archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-vetur-helpers.js
executable file
·137 lines (126 loc) · 3.72 KB
/
generate-vetur-helpers.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
#!/usr/bin/env node
const { resolve } = require("path")
const { readFileSync, existsSync, writeFile } = require("fs")
const { kebabCase, noop } = require("lodash")
const EOL = require("os").EOL
const BV = require("bootstrap-vue/dist/bootstrap-vue")
const cache = Object.create(null)
cache.tags = {}
cache.attributes = {}
const PropTypeMap = new Map([
[String, { description: "String value.", type: "string" }],
[Boolean, { description: "Boolean value.", type: "boolean" }],
[Number, { description: "Number value.", type: "number" }],
[Array, { description: "Array value.", type: "array" }],
[Object, { description: "Object value.", type: "object" }],
[Function, { description: "Function value.", type: "function" }],
[RegExp, { description: "RegExp value.", type: "RegExp" }],
[Date, { description: "Date value.", type: "Date" }]
])
class JsonSet extends Set {
toJSON() {
return Array.from(this)
}
}
function $writeFile(path, data, options) {
return new Promise((resolve, reject) => {
writeFile(path, data, options, err => {
err ? reject(err) : resolve()
})
})
}
function reduceTags({ tagName, metaDoc }) {
return function reducerFn(meta, prop) {
meta.attributes.add(kebabCase(prop))
meta.description = metaDoc.description || `Bootstrap-Vue component: <${tagName}>`
if (metaDoc.components) {
metaDoc.components.map(name => meta.subtags.add(kebabCase(name)))
}
if (metaDoc.events) {
metaDoc.events.map(e => meta.attributes.add(e.event))
}
return meta
}
}
function reduceAttrs({ props, tagName, metaDoc }) {
return function reducerFn(meta, prop) {
const kebabCaseProp = kebabCase(prop)
const entry = {}
// Namespace component props by component.
const nsKey = `${tagName}/${kebabCaseProp}`
const value = props[prop].type
if (Array.isArray(value)) {
const types = value.map(val => {
const v = PropTypeMap.get(val)
if (!PropTypeMap.has(val)) {
console.error(nsKey, v)
} else {
return PropTypeMap.get(val).type
}
})
const type = types.join("|")
let description = "One of "
if (types.length == 2) {
description += `${types[0]} or ${types[1]}.`
} else {
for (let i = 0; i < types.length; i++) {
if (i < types.length - 1) {
description += `${types[i]}, `
} else {
description += `or ${types[i]}.`
}
}
}
entry[nsKey] = {
description,
type
}
} else {
entry[nsKey] = PropTypeMap.get(value)
}
return Object.assign({}, meta, entry)
}
}
const Vue = (function() {
const v = Object.create(null)
v.directive = noop
v.use = plugin => plugin.install(v)
v.component = (name, def) => {
const tagName = kebabCase(name)
const metapath = resolve(
require.resolve("bootstrap-vue"),
"/docs/components/",
tagName
.split("-")
.filter(s => s !== "b")
.join("-"),
"meta.json"
)
let metaDoc = {}
if (existsSync(metapath)) {
metaDoc = JSON.parse(readFileSync(metapath))
}
cache.tags[tagName] = Object.keys(def.props || {}).reduce(reduceTags({ tagName, metaDoc }), {
attributes: new JsonSet(),
subtags: new JsonSet(),
description: ""
})
Object.assign(
cache.attributes,
Object.keys(def.props || {}).reduce(reduceAttrs({ tagName, props: def.props, metaDoc }), cache.attributes)
)
}
return v
})()
function cacheLibMeta() {
BV.install(Vue)
}
function main() {
cacheLibMeta()
for (const basename in cache) {
$writeFile(resolve(__dirname, "..", `${basename}.json`), JSON.stringify(cache[basename], null, 2) + EOL).catch(
console.error
)
}
}
main()