forked from xiaobaili1992/zyd-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateTemplate.js
102 lines (83 loc) · 2.15 KB
/
generateTemplate.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
const fs = require('node:fs');
const path = require('node:path');
const { name } = require('./package.json');
const notNeedPreviewComponent = ['ZydLargeScreenScale'];
const getPath = (curPath) => {
return path.resolve(__dirname, curPath);
};
const getFileContent = (filePath, componentName) => {
let exampleCode = '';
try {
exampleCode = fs.readFileSync(filePath, 'utf8');
exampleCode = exampleCode?.replace(/\.\.\/index(\.js)?/g, name);
} catch (err) {
console.error('读取文件时出错:', err);
exampleCode = `
<template>
<${componentName} />
</template>
<script>
export default {
components: {
${componentName}
}
}
</script>
`;
}
return exampleCode;
};
const getFileName = (dirPath) => {
let componentNames = [];
try {
const files = fs.readdirSync(dirPath);
const vueFiles = files.filter((file) => path.extname(file) === '.vue');
componentNames = vueFiles.map((file) => path.basename(file, '.vue'));
} catch (err) {
console.error('获取文件名出错:', err);
}
return componentNames;
};
const commonTemplate = (componentInfo, componentName, previewCase) => {
const { props, slots, methods, events, expose, functionalTag } = componentInfo;
return `
# ${componentName}
${previewCase}
${props}
${slots}
${methods}
${events}
${expose}
${functionalTag}
`;
};
module.exports = (componentInfo, componentName) => {
const dirPath = getPath('./src/previews/');
const fileNames = getFileName(dirPath);
if (!fileNames?.includes(componentName)) {
return commonTemplate(componentInfo, componentName, '');
}
const filePath = getPath(`./src/previews/${componentName}.vue`);
const exampleCode = getFileContent(filePath, componentName);
const component = notNeedPreviewComponent.includes(componentName)
? '该组件为全屏模式,暂不支持展示'
: `<${componentName} />`;
return commonTemplate(
componentInfo,
componentName,
`
<ClientOnly>
<CodePreview>
<template slot="preview">
${component}
</template>
<template slot="code">
\`\`\`vue
${exampleCode}
\`\`\`
</template>
</CodePreview>
</ClientOnly>
`,
);
};