@abi-software/simulationvuer 3.0.12 → 3.0.14

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.
@@ -5,133 +5,130 @@
5
5
  * To watch components changes for Vitepress on Dev Mode
6
6
  */
7
7
 
8
- import fs from 'fs'
9
- import path from 'path'
10
- import chokidar from 'chokidar'
11
- import { parseSource } from 'vue-docgen-api'
12
- import { Render } from '@vuese/markdown-render'
8
+ import { Render } from "@vuese/markdown-render";
13
9
 
14
- const watchMode = process.argv.find((argv) => argv === 'watch')
10
+ import chokidar from "chokidar";
11
+ import fs from "node:fs";
12
+ import path from "node:path";
13
+ import { parseSource } from "vue-docgen-api";
15
14
 
16
- const componentsDir = 'src/components'
17
- const components = ['SimulationVuer.vue']
18
- const outputDir = 'docs/components'
15
+ const watchMode = process.argv.find((argv) => argv === "watch");
19
16
 
20
- function generateMarkdown(file) {
21
- const fileWithPath = `${componentsDir}/${file}`
22
- const fileContent = fs.readFileSync(fileWithPath, 'utf-8')
17
+ const componentsDir = "src/components";
18
+ const components = ["SimulationVuer.vue"];
19
+ const outputDir = "docs/components";
20
+
21
+ async function generateMarkdown(file) {
22
+ const fileWithPath = `${componentsDir}/${file}`;
23
+ const fileContent = fs.readFileSync(fileWithPath, "utf-8");
23
24
 
24
25
  try {
25
- // const parserResult = parser(fileContent)
26
- const parserResult = parseSource(fileContent, fileWithPath)
27
- parserResult.then((result) => {
28
- const {
29
- displayName: name,
30
- description: desc,
31
- props,
32
- events,
33
- methods
34
- } = result
35
-
36
- // transform props to vuese styles
37
- const parseResult = {
38
- name: name,
39
- componentDesc: {
40
- default: [desc]
41
- },
42
- props: transformData(props),
43
- events: transformData(events),
44
- methods: transformData(methods),
45
- }
46
- parseResult.name = 'SimulationVuer' // Because there has another name prop in component
47
- const r = new Render(parseResult)
48
- const renderResult = r.render()
49
- const markdownResult = r.renderMarkdown()
50
- const markdownContent = markdownResult.content
51
- const componentName = path.basename(fileWithPath, '.vue')
52
-
53
- if (!fs.existsSync(outputDir)) {
54
- fs.mkdirSync(outputDir)
55
- }
26
+ const result = await parseSource(fileContent, fileWithPath);
27
+ const {
28
+ displayName: name,
29
+ description: desc,
30
+ props,
31
+ events,
32
+ methods,
33
+ } = result;
34
+
35
+ // transform props to vuese styles
36
+ const parseResult = {
37
+ name: name,
38
+ componentDesc: {
39
+ default: [desc],
40
+ },
41
+ props: transformData(props),
42
+ events: transformData(events),
43
+ methods: transformData(methods),
44
+ };
45
+ parseResult.name = "SimulationVuer"; // Because there has another name prop in component
46
+ const r = new Render(parseResult);
47
+ const markdownResult = r.renderMarkdown();
48
+ const markdownContent = markdownResult.content;
49
+ const componentName = path.basename(fileWithPath, ".vue");
50
+
51
+ if (!fs.existsSync(outputDir)) {
52
+ fs.mkdirSync(outputDir);
53
+ }
54
+
55
+ await fs.promises.writeFile(
56
+ `${outputDir}/${componentName}.md`,
57
+ markdownContent,
58
+ );
56
59
 
57
- fs.writeFile(`${outputDir}/${componentName}.md`, markdownContent, (err) => {
58
- if (err) {
59
- console.error(`Error writing markdown file for ${componentName}`, err)
60
- } else {
61
- console.log(`Markdown file for ${componentName} is generated!`)
62
- }
63
- })
64
- })
65
- } catch(e) {
66
- console.error(e)
60
+ console.log(`Markdown file for ${componentName} is generated!`);
61
+ } catch (e) {
62
+ // log parse or write errors so the developer knows something went wrong
63
+ console.error(`Failed to generate markdown for ${file}:`, e);
67
64
  }
68
65
  }
69
66
 
70
67
  function transformData(data = []) {
71
68
  function capitalise(str) {
72
- return str.charAt(0).toUpperCase() + str.slice(1)
69
+ return str.charAt(0).toUpperCase() + str.slice(1);
73
70
  }
74
71
 
75
72
  data.forEach((prop) => {
76
- prop.name = prop.name
77
-
78
73
  if (prop.description) {
79
- prop.describe = [prop.description.replaceAll('\n', ' ')]
74
+ prop.describe = [prop.description.replaceAll("\n", " ")];
80
75
  }
81
76
 
82
77
  if (prop.type) {
83
78
  // Handle multiple types separated by '|'
84
79
  // Convert to array to avoid markdown table issues
85
- if (prop.type.name.indexOf('|') !== -1) {
86
- prop.type = prop.type.name.split('|').map((item) => capitalise(item.trim()))
80
+ if (prop.type.name.indexOf("|") !== -1) {
81
+ prop.type = prop.type.name
82
+ .split("|")
83
+ .map((item) => capitalise(item.trim()));
87
84
  } else {
88
- prop.type = capitalise(prop.type.name)
85
+ prop.type = capitalise(prop.type.name);
89
86
  }
90
87
  }
91
88
 
92
89
  if (prop.defaultValue) {
93
- prop.default = prop.defaultValue.value.replaceAll('\n', ' ')
90
+ prop.default = prop.defaultValue.value.replaceAll("\n", " ");
94
91
  }
95
92
 
96
93
  // events
97
94
  if (prop.properties) {
98
- prop.argumentsDesc = []
95
+ prop.argumentsDesc = [];
99
96
  prop.properties.forEach((param) => {
100
- const argName = param.name || param.description
101
- prop.argumentsDesc.push(argName)
102
- })
97
+ const argName = param.name || param.description;
98
+ prop.argumentsDesc.push(argName);
99
+ });
103
100
  }
104
101
 
105
102
  // methods
106
103
  if (prop.params) {
107
- prop.argumentsDesc = []
104
+ prop.argumentsDesc = [];
108
105
  prop.params.forEach((param) => {
109
- const argName = param.description || param.name
110
- prop.argumentsDesc.push(argName)
111
- })
106
+ const argName = param.description || param.name;
107
+ prop.argumentsDesc.push(argName);
108
+ });
112
109
  }
113
- })
110
+ });
114
111
  if (!data.length) {
115
- return null
112
+ return null;
116
113
  }
117
- return data
114
+ return data;
118
115
  }
119
116
 
120
117
  // To generate markdown files - one time
121
118
  components.forEach((component) => {
122
- console.log(`Write markdown file for ${component} on first load.`)
123
- generateMarkdown(component)
124
- })
119
+ console.log(`Write markdown file for ${component} on first load.`);
120
+ generateMarkdown(component);
121
+ });
125
122
 
126
123
  // To watch component changes and generate markdown files
127
124
  if (watchMode) {
128
125
  const watcher = chokidar.watch(components, {
129
126
  cwd: componentsDir,
130
127
  ignoreInitial: true,
131
- })
128
+ });
132
129
 
133
- watcher.on('change', (file) => {
134
- console.log(`The component ${file} has changed!`)
135
- generateMarkdown(file)
136
- })
130
+ watcher.on("change", (file) => {
131
+ console.log(`The component ${file} has changed!`);
132
+ generateMarkdown(file);
133
+ });
137
134
  }