forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact2Code.js
More file actions
225 lines (202 loc) · 5.35 KB
/
react2Code.js
File metadata and controls
225 lines (202 loc) · 5.35 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const { join } = require('path');
const { existsSync, mkdirSync } = require('fs');
const { transferDir, transferFile } = require('./file');
/**
* 生成 react 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,
isNeedDefine,
isNeedBatchDefine,
} = options;
return transferFile(fromFile, toFile, data => {
const apiConfig = { list: '/mock/list.json' };
const batchToolbar = [];
const rowToolbar = [];
if (isNeedAdd) {
apiConfig.add = '/mock/modify.json';
batchToolbar.push(`{
label: '添加',
type: 'primary',
toolbarType: ICrudToolbarTypeEnum.Add,
request: (row) =>
fetch(apiConfig.add, {
method: 'get',
}).then(() => {
message.success('添加成功');
}),
}`);
}
if (isNeedEdit) {
apiConfig.edit = '/mock/modify.json';
rowToolbar.push(`{
label: '编辑',
type: 'link',
toolbarType: ICrudToolbarTypeEnum.Edit,
request: (row) =>
fetch(apiConfig.edit, {
method: 'get',
}).then(() => {
message.success('编辑成功');
}),
}`);
}
if (isNeedDelete) {
apiConfig.delete = '/mock/delete.json';
rowToolbar.push(`{
label: '删除',
type: 'link',
danger: true,
toolbarType: ICrudToolbarTypeEnum.Delete,
request: (row) =>
fetch(apiConfig.delete, {
method: 'get',
}).then(() => {
message.success('删除成功');
}),
}`);
}
if (isNeedBatchDelete) {
apiConfig.batchDelete = '/mock/delete.json';
batchToolbar.push(`{
label: '批量删除',
type: 'dashed',
danger: true,
toolbarType: ICrudToolbarTypeEnum.DeleteBatch,
request: (row) =>
fetch(apiConfig.delete, {
method: 'get',
}).then(() => {
message.success('删除成功');
}),
}`);
}
if (isNeedDefine) {
rowToolbar.push(`{
render: (row, index) => {
return (
<Button
type="link"
onClick={() => {
message.warning('自定义事件处理');
console.log(1111, row, index);
}}
>
行级操作
</Button>
);
}
}`);
}
if (isNeedBatchDefine) {
batchToolbar.push(`{
render: (rows, rowKeys) => {
console.log(rows, rowKeys);
return (
<Button type="text" onClick={() => message.warning('自定义事件处理')}>
自定义按钮
</Button>
);
},
}`);
}
// 移除筛选
!isNeedQuery && data.replace(/isFilter: true,/g, 'isFilter: false,');
// replace content
const replaceData = `import { Button, message } from 'antd';\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;
});
};
/**
* 生成 react 模板代码
*
* @param {*} fromPath
* @param {*} toPath
* @param {*} options
*/
const generateReactCode = (fromPath, toPath, options) => {
const { isTs, file, model } = options;
// 基础 crud 模板
const baseCrudTemplatePath = 'template/crud';
const templateSuffix = isTs ? 'tsx' : 'jsx';
const templateFromPath = join(
fromPath,
`${baseCrudTemplatePath}.${templateSuffix}`,
);
const templateToPath = join(toPath, model);
if (!existsSync(templateFromPath)) return;
// 模块不存在则新建
!existsSync(templateToPath) && mkdirSync(templateToPath);
// replace args
replaceArgsToFile(
templateFromPath,
join(templateToPath, `${file}.${templateSuffix}`),
options,
).then(isSuccess => {});
};
/**
* 初始化 react crud 基础组件
* 用作生成基础组件到项目基架中
*
* @param {*} fromPath
* @param {*} toPath
*/
const initReactBase = (fromPath, toPath) => {
// 基础组件
const baseComponentPath = 'components';
// ignore file
const ignoreFiles = [
'node_modules',
'.DS_Store',
'dist',
'yarn.lock',
'package-lock.json',
'package.json',
'tsconfig.json',
];
const baseFromPath = join(fromPath, baseComponentPath);
const baseToPath = join(toPath, baseComponentPath);
transferDir(baseFromPath, baseToPath, ignoreFiles);
};
/**
* 迁移 mock
*
* @param {*} fromPath
* @param {*} toPath
*/
const generateReactMock = (fromPath, toPath) => {
const mockFromPath = join(fromPath, 'mock');
const mockToPath = join(toPath, 'mock');
transferDir(mockFromPath, mockToPath);
};
module.exports = {
initReactBase,
generateReactCode,
generateReactMock,
};