forked from ABI-Software/flatmapvuer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vuese-generator.js
65 lines (55 loc) · 1.77 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
/**
* 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 { Render } from '@vuese/markdown-render'
const watchMode = process.argv.find((argv) => argv === 'watch')
const componentsDir = 'src/components'
const components = ['FlatmapVuer.vue', 'MultiFlatmapVuer.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 r = new Render(parserResult)
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)
})
}