generated from ABI-Software/ScaffoldVuer-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vuese-generator.js
94 lines (82 loc) · 2.59 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
/**
* Vuese Generator
*
* To generate markdown files from Vue components
* To watch components changes for Vitepress on Dev Mode
*
* Update the components array.
*/
const COMPONENTS = [
'VueComponentTemplate'
]
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 outputDir = 'docs/components'
const components = COMPONENTS.map((component) => component + '.vue')
function generateMarkdown(file) {
const fileWithPath = `${componentsDir}/${file}`
const fileContent = fs.readFileSync(fileWithPath, 'utf-8')
try {
// vuese parser doesn't work with composition api
// const parserResult = parser(fileContent)
const parserResult = parseSource(fileContent, fileWithPath)
parserResult.then((result) => {
const name = result.displayName
const desc = result.description
const props = result.props
// transform props to vuese styles
props.forEach((prop) => {
prop.name = prop.name
prop.type = prop.type.name
prop.describe = [prop.description]
prop.default = prop.defaultValue.value.replaceAll('\n', '')
})
const parseResult = {
name: name,
componentDesc: {
default: [desc]
},
props: props,
}
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)
}
}
// 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)
})
}