-
Notifications
You must be signed in to change notification settings - Fork 7
/
vuese-generator.js
124 lines (107 loc) · 3.13 KB
/
vuese-generator.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
/**
* Vuese Generator
*
* To generate markdown files from Vue components
* To watch components changes for Vitepress on Dev Mode
*/
import fs from 'fs'
import path from 'path'
import chokidar from 'chokidar'
// import { parser } from '@vuese/parser'
import { parseSource } from 'vue-docgen-api'
import { Render } from '@vuese/markdown-render'
const watchMode = process.argv.find((argv) => argv === 'watch')
const componentsDir = 'src/components'
const components = ['ScaffoldVuer.vue']
const outputDir = 'docs/components'
function generateMarkdown(file) {
const fileWithPath = `${componentsDir}/${file}`
const fileContent = fs.readFileSync(fileWithPath, 'utf-8')
try {
// const parserResult = parser(fileContent)
const parserResult = parseSource(fileContent, fileWithPath)
parserResult.then((result) => {
const {
displayName: name,
description: desc,
props,
events,
methods
} = result
// transform props to vuese styles
const parseResult = {
name: name,
componentDesc: {
default: [desc]
},
props: transformData(props),
events: transformData(events),
methods: transformData(methods),
}
const r = new Render(parseResult)
const renderResult = r.render()
const markdownResult = r.renderMarkdown()
const markdownContent = markdownResult.content
const componentName = path.basename(fileWithPath, '.vue')
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir)
}
fs.writeFile(`${outputDir}/${componentName}.md`, markdownContent, (err) => {
if (err) {
console.error(`Error writing markdown file for ${componentName}`, err)
} else {
console.log(`Markdown file for ${componentName} is generated!`)
}
})
})
} catch(e) {
console.error(e)
}
}
function transformData(data = []) {
data.forEach((prop) => {
prop.name = prop.name
if (prop.description) {
prop.describe = [prop.description.replaceAll('\n', ' ')]
}
if (prop.type) {
prop.type = prop.type.name
}
if (prop.defaultValue) {
prop.default = prop.defaultValue.value.replaceAll('\n', ' ')
}
// events
if (prop.properties) {
prop.argumentsDesc = []
prop.properties.forEach((param) => {
const argName = param.name || param.description
prop.argumentsDesc.push(argName)
})
}
// methods
if (prop.params) {
prop.argumentsDesc = []
prop.params.forEach((param) => {
const argName = param.description || param.name
prop.argumentsDesc.push(argName)
})
}
})
return data
}
// To generate markdown files - one time
components.forEach((component) => {
console.log(`Write markdown file for ${component} on first load.`)
generateMarkdown(component)
})
// To watch component changes and generate markdown files
if (watchMode) {
const watcher = chokidar.watch(components, {
cwd: componentsDir,
ignoreInitial: true,
})
watcher.on('change', (file) => {
console.log(`The component ${file} has changed!`)
generateMarkdown(file)
})
}