@abi-software/simulationvuer 0.7.0-vue-3-alpha.5 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Vuese Generator
3
+ *
4
+ * To generate markdown files from Vue components
5
+ * To watch components changes for Vitepress on Dev Mode
6
+ */
7
+
8
+ import fs from 'fs'
9
+ import path from 'path'
10
+ import chokidar from 'chokidar'
11
+ // import { parser } from '@vuese/parser'
12
+ import { parseSource } from 'vue-docgen-api'
13
+ import { Render } from '@vuese/markdown-render'
14
+
15
+ const watchMode = process.argv.find((argv) => argv === 'watch')
16
+
17
+ const componentsDir = 'src/components'
18
+ const components = ['SimulationVuer.vue']
19
+ const outputDir = 'docs/components'
20
+
21
+ function generateMarkdown(file) {
22
+ const fileWithPath = `${componentsDir}/${file}`
23
+ const fileContent = fs.readFileSync(fileWithPath, 'utf-8')
24
+
25
+ try {
26
+ // const parserResult = parser(fileContent)
27
+ const parserResult = parseSource(fileContent, fileWithPath)
28
+ parserResult.then((result) => {
29
+ const {
30
+ displayName: name,
31
+ description: desc,
32
+ props,
33
+ events,
34
+ methods
35
+ } = result
36
+
37
+ // transform props to vuese styles
38
+ const parseResult = {
39
+ name: name,
40
+ componentDesc: {
41
+ default: [desc]
42
+ },
43
+ props: transformData(props),
44
+ events: transformData(events),
45
+ methods: transformData(methods),
46
+ }
47
+ parseResult.name = 'SimulationVuer' // Because there has another name prop in component
48
+ const r = new Render(parseResult)
49
+ const renderResult = r.render()
50
+ const markdownResult = r.renderMarkdown()
51
+ const markdownContent = markdownResult.content
52
+ const componentName = path.basename(fileWithPath, '.vue')
53
+
54
+ if (!fs.existsSync(outputDir)) {
55
+ fs.mkdirSync(outputDir)
56
+ }
57
+
58
+ fs.writeFile(`${outputDir}/${componentName}.md`, markdownContent, (err) => {
59
+ if (err) {
60
+ console.error(`Error writing markdown file for ${componentName}`, err)
61
+ } else {
62
+ console.log(`Markdown file for ${componentName} is generated!`)
63
+ }
64
+ })
65
+ })
66
+ } catch(e) {
67
+ console.error(e)
68
+ }
69
+ }
70
+
71
+ function transformData(data = []) {
72
+ data.forEach((prop) => {
73
+ prop.name = prop.name
74
+
75
+ if (prop.description) {
76
+ prop.describe = [prop.description.replaceAll('\n', ' ')]
77
+ }
78
+
79
+ if (prop.type) {
80
+ prop.type = prop.type.name
81
+ }
82
+
83
+ if (prop.defaultValue) {
84
+ prop.default = prop.defaultValue.value.replaceAll('\n', ' ')
85
+ }
86
+
87
+ // events
88
+ if (prop.properties) {
89
+ prop.argumentsDesc = []
90
+ prop.properties.forEach((param) => {
91
+ const argName = param.name || param.description
92
+ prop.argumentsDesc.push(argName)
93
+ })
94
+ }
95
+
96
+ // methods
97
+ if (prop.params) {
98
+ prop.argumentsDesc = []
99
+ prop.params.forEach((param) => {
100
+ const argName = param.description || param.name
101
+ prop.argumentsDesc.push(argName)
102
+ })
103
+ }
104
+ })
105
+ if (!data.length) {
106
+ return null
107
+ }
108
+ return data
109
+ }
110
+
111
+ // To generate markdown files - one time
112
+ components.forEach((component) => {
113
+ console.log(`Write markdown file for ${component} on first load.`)
114
+ generateMarkdown(component)
115
+ })
116
+
117
+ // To watch component changes and generate markdown files
118
+ if (watchMode) {
119
+ const watcher = chokidar.watch(components, {
120
+ cwd: componentsDir,
121
+ ignoreInitial: true,
122
+ })
123
+
124
+ watcher.on('change', (file) => {
125
+ console.log(`The component ${file} has changed!`)
126
+ generateMarkdown(file)
127
+ })
128
+ }