forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvue2Code.js
More file actions
183 lines (158 loc) · 4.72 KB
/
vue2Code.js
File metadata and controls
183 lines (158 loc) · 4.72 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const { join } = require('path');
const { existsSync, mkdirSync } = require('fs');
const { transferDir, transferFile } = require('./file');
const {logSuccess} =require('./log')
/**
* 生成 vue crud 模板代码
*
* @param {*} fromFile
* @param {*} toFile
* @param {*} options
* @returns Promise<boolean>
*/
const replaceArgsToFile = (fromFile, toFile, options) => {
if (!options) return Promise.resolve();
const {
title, // 标题
containerType = 'Modal', // 交互方式
isNeedQuery, // 是否需要筛选
isNeedAdd,
isNeedEdit,
isNeedDelete,
isNeedBatchDelete,
} = options;
return transferFile(fromFile, toFile, data => {
const apiConfig = { list: '/api/json/list' };
const batchToolbar = [];
const rowToolbar = [];
if (isNeedAdd) {
apiConfig.add = '/api/json/add';
batchToolbar.push(`{
label: '添加',
type: 'primary',
toolbarType: ICrudToolbarTypeEnum.Add,
request: (row) =>
axios(apiConfig.add, { method: 'post', data: row }).then(() => {
ElMessage.success('添加成功');
}),
}`);
}
if (isNeedEdit) {
apiConfig.edit = '/api/json/edit';
rowToolbar.push(`{
label: '编辑',
type: 'link',
toolbarType: ICrudToolbarTypeEnum.Edit,
request: (row) =>
axios(apiConfig.edit, { method: 'post', data: row }).then(() => {
ElMessage.success('编辑成功');
}),
}`);
}
if (isNeedDelete) {
apiConfig.delete = '/api/json/delete';
batchToolbar.push(`{
label: '删除',
type: 'link',
toolbarType: ICrudToolbarTypeEnum.Delete,
request: (row) =>
axios(apiConfig.delete, { method: 'post', data: row }).then(() => {
ElMessage.success('删除成功');
}),
}`);
}
if (isNeedBatchDelete) {
apiConfig.batchDelete = '/api/json/delete';
batchToolbar.push(`{
label: '批量删除',
type: 'dashed',
toolbarType: ICrudToolbarTypeEnum.DeleteBatch,
request: (row) =>
axios(apiConfig.delete, { method: 'post', data: row }).then(() => {
ElMessage.success('删除成功');
}),
}`);
}
// 移除筛选
!isNeedQuery && data.replace(/isFilter: true,/g, 'isFilter: false,');
// replace content
const replaceData = `import { ElMessage } from 'element-plus';\n${data
.replace(/\/\/ @ts-ignore/g, '')
.replace(
/\} from '..\/components\/index';/g,
`, ICrudToolbarTypeEnum, ICurdContainerTypeEnum } from '../components/index';\n`,
)
.replace(/\{crud.title\}/g, title)
.replace(/'\{crud.apiConfig\}'/g, JSON.stringify(apiConfig))
.replace(
/'\{crud.containerType\}'/g,
`ICurdContainerTypeEnum.${containerType}`,
)
.replace(/'\{crud.batchToolbar\}'/g, `[${batchToolbar.join(',')}]`)
.replace(/'\{crud.rowToolbar\}'/g, `[${rowToolbar.join(',')}]`)}`;
return replaceData;
});
};
/**
* 生成 vue 模板代码
*
* @param {*} fromPath
* @param {*} toPath
* @param {*} options
*/
const generateVueCode = (fromPath, toPath, options) => {
const { isTs, file, model,component } = options;
// 基础 crud 模板
component.forEach(item=>{
const baseCrudTemplatePath = `components/${item}/config`;
const templateSuffix = isTs ? 'ts' : 'ts';
const templateFromPath = join(
fromPath,
`${baseCrudTemplatePath}.${templateSuffix}`,
);
const templateToPath = join(toPath, model,'components',item);
if (!existsSync(templateFromPath)) return;
// 模块不存在则新建
!existsSync(templateToPath) && mkdirSync(templateToPath);
// replace args
replaceArgsToFile(
templateFromPath,
join(templateToPath, `config.${templateSuffix}`),
options,
).then(isSuccess => {});
})
};
/**
* 初始化 Vue crud 基础组件
* 用作生成基础组件到项目基架中
*
* @param {*} fromPath
* @param {*} toPath
*/
const initVueBase = (fromPath, toPath,options) => {
const { model,component } = options;
const baseModelPath = join(toPath, model);
!existsSync(baseModelPath) && mkdirSync(baseModelPath);
// 基础组件
const baseComponentPath = 'components';
let templateFiles = ['Form','Table','ToolBar']
// ignore file
const ignoreFiles = [
'node_modules',
'.DS_Store',
'dist',
'yarn.lock',
'.prettierrc',
'README.md',
'package-lock.json',
'package.json',
'tsconfig.json',
].concat(templateFiles.filter(item=>!component.includes(item)));
const baseFromPath = join(fromPath, baseComponentPath);
const baseToPath = join(toPath, model,baseComponentPath);
transferDir(baseFromPath, baseToPath, ignoreFiles);
};
module.exports = {
initVueBase,
generateVueCode,
};