forked from vuedoc/parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (62 loc) · 2.26 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
'use strict'
const fs = require('fs')
const cheerio = require('cheerio')
const Parser = require('./lib/parser')
const DEFAULT_ENCODING = 'utf8'
const DEFAULT_IGNORED_VISIBILITIES = ['protected', 'private']
module.exports.parseOptions = (options) => {
if (!options || (!options.filename && !options.filecontent)) {
throw new Error('One of options.filename or options.filecontent is required')
}
options.encoding = options.encoding || DEFAULT_ENCODING
options.ignoredVisibilities = options.ignoredVisibilities || DEFAULT_IGNORED_VISIBILITIES
}
module.exports.parse = (options) => new Promise((resolve) => {
this.parseOptions(options)
if (!options.source) {
if (options.filename) {
options.source = loadSourceFromFileContent(
fs.readFileSync(options.filename, options.encoding))
} else {
options.source = loadSourceFromFileContent(options.filecontent)
}
}
const filterIgnoredVisibilities = (item) => {
return options.ignoredVisibilities.indexOf(item.visibility) === -1
}
const component = {}
const walker = new Parser(options).walk()
walker.features.forEach((feature) => {
switch (feature) {
case 'name':
case 'description':
component[feature] = null
break
default:
component[feature] = []
}
})
walker
.on('name', (name) => (component.name = name))
.on('description', (desc) => (component.description = desc))
.on('keywords', (keywords) => (component.keywords = keywords))
.on('props', (prop) => component.props.push(prop))
.on('data', (data) => component.data.push(data))
.on('computed', (prop) => component.computed.push(prop))
.on('methods', (method) => component.methods.push(method))
.on('slot', (slot) => component.slots.push(slot))
.on('event', (event) => component.events.push(event))
.on('end', () => {
component.props = component.props.filter(filterIgnoredVisibilities)
component.methods = component.methods.filter(filterIgnoredVisibilities)
component.events = component.events.filter(filterIgnoredVisibilities)
resolve(component)
})
})
function loadSourceFromFileContent (filecontent) {
const $ = cheerio.load(filecontent)
return {
template: $('template').html(),
script: $('script').html()
}
}