-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathgenBlockConfig.js
185 lines (174 loc) · 5.1 KB
/
genBlockConfig.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
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
const fs = require('fs');
const { join } = require('path');
const gitUrl = 'https://github.com/ant-design/pro-blocks';
const menuData = {
home: '首页',
Empty: '空白',
login: '登录',
register: '注册',
'register/result': '注册结果',
dashboard: 'Dashboard',
'dashboard/analysis': '分析页',
'dashboard/monitor': '监控页',
'dashboard/workplace': '工作台',
'exception/403': '403',
'exception/404': '404',
'exception/500': '500',
form: '表单页',
'user/login': '登录页',
'user/register': '注册页',
'user/register/result': '注册结果页',
'form/basic/form': '基础表单',
'form/step/form': '分步表单',
'form/advanced/form': '高级表单',
list: '列表页',
'list/table/list': '查询表格',
'list/basic/list': '标准列表',
'list/card/list': '卡片列表',
'list/search': '搜索列表',
'list/search/articles': '搜索列表(文章)',
'list/search/projects': '搜索列表(项目)',
'list/search/applications': '搜索列表(应用)',
profile: '详情页',
'profile/basic': '基础详情页',
'profile/advanced': '高级详情页',
result: '结果页',
'result/success': '成功页',
'result/fail': '失败页',
exception: '异常页',
'exception/not-permission': '403',
'exception/not-find': '404',
'exception/server-error': '500',
'exception/trigger': '触发错误',
account: '个人页',
'account/center': '个人中心',
'account/settings': '个人设置',
'account/trigger': '触发报错',
'account/logout': '退出登录',
editor: '图形编辑器',
'editor/flow': '流程编辑器',
'editor/mind': '脑图编辑器',
'editor/koni': '拓扑编辑器',
};
const tagsKey = {
list: '列表',
search: '搜索',
articles: 'remove',
table: '表格',
form: '表单',
step: 'remove',
basic: '基本',
card: 'remove',
applications: 'remove',
projects: 'remove',
404: 'remove',
403: 'remove',
500: 'remove',
profile: '详情',
advanced: '高级',
result: '结果',
fail: 'remove',
success: 'remove',
user: '用户',
login: '登录',
register: '注册',
account: 'remove',
center: '个人中心',
settings: '个人设置',
dashboard: 'dashboard',
analysis: 'remove',
monitor: 'remove',
workplace: 'remove',
editor: '图形编辑',
flow: 'remove',
koni: 'remove',
mind: 'remove',
exception: '异常',
};
/**
* 从文件数组映射为 pro 的路由
*
* @param {any} name
*/
const genBlockName = (name) =>
name
.match(/[A-Z]?[a-z]+|[0-9]+/g)
.map((p) => p.toLowerCase())
.join('/');
/**
* 从文件数组映射为 tags 列表
*
* @param {any} name
*/
const genBlockTags = (name) =>
Array.from(new Set(name.match(/[A-Z]?[a-z]+|[0-9]+/g).map((p) => p.toLowerCase())))
.map((key) => tagsKey[key] || key)
.filter((key) => key !== 'remove');
const getFeature = (filePath) => {
const feature = ['antd'];
const srcPath = join(filePath, 'src');
const localesPath = join(srcPath, 'locales');
if (fs.existsSync(localesPath)) {
feature.push('i18n');
}
const modalTsxPath = join(srcPath, 'model.tsx');
const modalTsPath = join(srcPath, 'model.ts');
const modalJsPath = join(srcPath, 'model.js');
const modalJsxPath = join(srcPath, 'model.jsx');
if (
fs.existsSync(modalTsxPath) ||
fs.existsSync(modalTsPath) ||
fs.existsSync(modalJsPath) ||
fs.existsSync(modalJsxPath)
) {
feature.push('dva');
}
return feature;
};
/**
* 遍历文件地址
*
* @param path
*/
const getFolderTreeData = (filePath) => {
const files = fs.readdirSync(filePath);
const blockList = files
.map((fileName) => {
const status = fs.statSync(join(filePath, fileName));
if (status.isDirectory() && fileName.indexOf('.') !== 0 && fileName !== 'EmptyPage') {
const absPkgPath = join(filePath, fileName, 'package.json');
if (fs.existsSync(absPkgPath)) {
const pkg = require(absPkgPath);
return {
name: menuData[genBlockName(fileName)],
key: fileName,
description: pkg.description,
url: `${gitUrl}/tree/master/${fileName}`,
path: fileName,
features: getFeature(join(filePath, fileName)),
img: `https://raw.githubusercontent.com/ant-design/pro-blocks/master/${fileName}/snapshot.png?raw=true`,
tags: genBlockTags(fileName),
previewUrl: `https://preview.pro.ant.design/${genBlockName(fileName)}`,
};
}
}
return undefined;
})
.filter((obj) => obj);
blockList.unshift({
key: 'EmptyPage',
name: '空白页面',
description: '一个空白的页面,一切都从这里开始!',
url: 'https://github.com/ant-design/pro-blocks/tree/master/EmptyPage',
path: 'NewPage',
features: ['antd'],
img: 'https://raw.githubusercontent.com/ant-design/pro-blocks/master/EmptyPage/snapshot.png?raw=true',
tags: ['空白页'],
previewUrl: 'https://preview.pro.ant.design',
});
return blockList;
};
fs.writeFileSync(
join(__dirname, '..', 'umi-block.json'),
JSON.stringify({ list: getFolderTreeData(join(__dirname, '../')) }, null, 2),
);