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
251 lines (222 loc) · 5.89 KB
/
react2Code.js
File metadata and controls
251 lines (222 loc) · 5.89 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const {
readdirSync,
readFile,
writeFile,
createReadStream,
createWriteStream,
existsSync,
mkdirSync,
} = require('fs');
const { join } = require('path');
// ignore file
const ignoreFiles = [
'node_modules',
'.DS_Store',
'dist',
'yarn.lock',
'package-lock.json',
'package.json',
'tsconfig.json',
];
// 基础组件
const baseComponentPath = 'components';
// 基础 crud 模板
const baseCrudTemplatePath = 'template/crud';
const handleError = err => err && console.error(err) && process.exit(-1);
/**
* replace file
*
* @param {*} fromFile
* @param {*} toFile
* @param {*} options
* @returns
*/
const replaceArgsToFile = (fromFile, toFile, options) => {
if (!options || !existsSync(fromFile)) return;
const {
title, // 标题
containerType = 'Modal', // 交互方式
isNeedQuery, // 是否需要筛选
isNeedAdd,
isNeedEdit,
isNeedDelete,
isNeedBatchDelete,
isNeedDefine,
isNeedBatchDefine,
} = options;
readFile(fromFile, 'utf8', (err, data) => {
if (err) {
handleError(err);
return;
}
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) =>
request(apiConfig.add, { method: 'post', data: row }).then(() => {
message.success('添加成功');
}),
}`);
}
if (isNeedEdit) {
apiConfig.edit = '/api/json/edit';
rowToolbar.push(`{
label: '编辑',
type: 'link',
toolbarType: ICrudToolbarTypeEnum.Edit,
request: (row) =>
request(apiConfig.edit, { method: 'post', data: row }).then(() => {
message.success('编辑成功');
}),
}`);
}
if (isNeedDelete) {
apiConfig.delete = '/api/json/delete';
rowToolbar.push(`{
label: '删除',
type: 'link',
danger: true,
toolbarType: ICrudToolbarTypeEnum.Delete,
request: (row) =>
request(apiConfig.delete, { method: 'post', data: row }).then(() => {
message.success('删除成功');
}),
}`);
}
if (isNeedBatchDelete) {
apiConfig.batchDelete = '/api/json/delete';
batchToolbar.push(`{
label: '批量删除',
type: 'dashed',
danger: true,
toolbarType: ICrudToolbarTypeEnum.DeleteBatch,
request: (row) =>
request(apiConfig.delete, { method: 'post', data: row }).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(',')}]`)}`;
writeFile(toFile, replaceData, 'utf8', handleError);
});
};
/**
* 迁移文件到固定目录
*
* @param {*} fromPath
* @param {*} toPath
* @param {*} cb
* @returns
*/
const copyFiles = (fromPath, toPath) => {
if (!existsSync(fromPath)) return;
// not exist and mkdir
!existsSync(toPath) && mkdirSync(toPath);
const files = readdirSync(fromPath, { withFileTypes: true });
for (const file of files) {
if (!ignoreFiles.includes(file.name)) {
const fromFilePath = join(fromPath, file.name);
const toFilePath = join(toPath, file.name);
if (file.isFile()) {
// copy file
const reader = createReadStream(fromFilePath);
const writer = createWriteStream(toFilePath);
reader.pipe(writer);
} else {
copyFiles(fromFilePath, toFilePath);
}
}
}
};
/**
* copy file
*
* @param {*} fromPath
* @param {*} toPath
* @param {*} options
*/
const generateReactCode = (fromPath, toPath, options) => {
const { isTs, file, model } = options;
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,
);
};
/**
* 初始化 react crud 基础组件
* 用作生成基础组件到项目基架中
*
* @param {*} fromPath
* @param {*} toPath
*/
const initReactBase = (fromPath, toPath) => {
const baseFromPath = join(fromPath, baseComponentPath);
const baseToPath = join(toPath, baseComponentPath);
copyFiles(baseFromPath, baseToPath);
};
module.exports = {
initReactBase,
generateReactCode,
};