郭减

前端开发工程师,前端爱好者

根据配置路由配置文件生成对应的文件夹/子文件夹/文件

说明:众所周知,我们在新起一个项目的时候,都是要添加路由,和创建对应的文件夹,子文件夹,index.jsx文件的,如果菜单少倒还好,如果菜单比较多,每次的复制黏贴,复制黏贴,复制黏贴,消耗大量的时间,人力,物力,最最重要的事情是!!!影响我们开发人员的心情,让我觉得我就是个无脑工具人,这种简单,无脑,但又不能搞错的工作,真的是很令人生气,在极大的程度上消耗我们的耐心

所以,简单写个小工具来做这件事情吧

参考地址: 源文件地址
文档地址: GitHub文档地址 博客文档地址

项目生成脚本

这是一个用于生成 React 项目结构的脚本。脚本会根据给定的菜单配置生成文件夹和文件,每个文件夹包含一个基本的 React 组件。

效果图如下

配置文件如下

如何使用

  1. 运行脚本

    执行以下命令:

    1
    node index.js

    这将根据 MENU_CONFIG 数据生成项目结构。

菜单配置

1
2
3
4
// 在 generateFiles.js 中的 MENU_CONFIG 数据
const MENU_CONFIG = [
// ... 你的菜单配置
];

文件结构

生成的文件结构如下:

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
- SystemConfig
- SystemInfo
- index.jsx
- WhiteList
- index.jsx
- DeviceManage
- SystemCommand
- index.jsx
- TimeSetting
- index.jsx
- NetworkConfig
- index.jsx
- DoubleHotBackup
- index.jsx
- DeviceCertificate
- index.jsx
- SystemMaintenance
- KeyBackUpAndRestore
- index.jsx
- RestoreAndUpgrade
- index.jsx
- AdminManage
- AdminList
- index.jsx
- AdminStrategy
- index.jsx
- SystemMonitor
- OnlineUser
- index.jsx
- ScheduledTask
- index.jsx
- DeviceMonitor
- index.jsx
- MonitorThreshold
- index.jsx
- About
- LicenseInfo
- index.jsx

注意事项

  • 请确保在运行脚本之前备份任何重要数据,因为脚本会覆盖已存在的文件夹和文件。
  • 你可以根据需要调整生成的组件代码和文件夹结构。
  • 若要使用 ES6 模块语法,请确保正确配置 Babel,可以参考 Babel 文档进行配置。
  • 目前只支持二级菜单配置项的生成,只有一级菜单暂不支持,有三级菜单也不支持,原因:暂时没需求,懒得搞,可自行更改就行

示例代码

以下是生成的每个组件的简单示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// SystemInfo/index.jsx
import React from 'react';
import { Button } from 'antd';

const SystemInfo = () => {
return (
<div>
<h1> SystemInfo </h1>
<Button type="primary">Primary Button</Button>
</div>
);
};

export default SystemInfo;

你可以在每个组件的 index.jsx 中添加具体的页面内容。

最后附上源码地址

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
const fs = require('fs');

// 测试数据
const MENU_CONFIG = [

{
name: '系统配置',
route: '/',
key: '/systemConfig',
subMenu: [
{
name: '系统信息',
route: '/systemInfo',
key: '/systemInfo',
},
{
name: '白名单管理',
route: '/whiteList',
key: '/whiteList',
},
]
},
// 设备管理
{
name: '设备管理',
route: '/',
key: '/deviceManage',
subMenu: [
{
name: '系统命令',
route: '/systemCommand',
key: '/systemCommand',
},
{
name: '时间设置',
route: '/timeSetting',
key: '/timeSetting',
},
{
name: '网络配置',
route: '/networkConfig',
key: '/networkConfig',
},
{
name: '双机热备',
route: '/doubleHotBackup',
key: '/doubleHotBackup',
},
{
name: '设备证书',
route: '/deviceCertificate',
key: '/deviceCertificate',
},
]
},
// 系统维护
{
name: '系统维护',
route: '/',
key: '/systemMaintenance',
subMenu: [
{
name: '备份&恢复',
route: '/keyBackUpAndRestore',
key: '/keyBackUpAndRestore',
},
{
name: '还原&升级',
route: '/restoreAndUpgrade',
key: '/restoreAndUpgrade',
},
]
},
// 管理员管理
{
name: '管理员管理',
route: '/',
key: '/adminManage',
subMenu: [
{
name: '管理员列表',
route: '/adminList',
key: '/adminList',
},
{
name: '管理员策略',
route: '/adminStrategy',
key: '/adminStrategy',
},
]
},
// 系统监控
{
name: '系统监控',
route: '/',
key: '/systemMonitor',
subMenu: [
{
name: '在线用户',
route: '/onlineUser',
key: '/onlineUser',
},
{
name: '定时任务',
route: '/scheduledTask',
key: '/scheduledTask',
},
{
name: '设备监控',
route: '/deviceMonitor',
key: '/deviceMonitor',
},
{
name: '监控阈值',
route: '/monitorThreshold',
key: '/monitorThreshold',
},
]
},
// 关于
{
name: '关于',
route: '/',
key: '/about',
subMenu: [
{
name: '授权信息',
route: '/licenseInfo',
key: '/licenseInfo',
},
]
}
]

function generateComponentCode(name, key) {
return `
import React from 'react';
import { Button } from 'antd';

const ${key.charAt(1).toUpperCase() + key.slice(2)} = () => {
return (
<div>
<h1> ${name} </h1>
{/* 这里可以写你的具体页面内容 */}
<Button type="primary">Primary Button</Button>
</div>
);
};

export default ${key.charAt(1).toUpperCase() + key.slice(2)};
`;
}


MENU_CONFIG.forEach(({ name, key, subMenu }) => {
// 生成主文件夹
fs.mkdirSync(`./${key.charAt(1).toUpperCase() + key.slice(2)}`, { recursive: true });
console.log(`创建文件夹: ./${key.charAt(1).toUpperCase() + key.slice(2)}`);

// 生成子文件夹
subMenu.forEach(({ name: subName, key: subKey }) => {
fs.mkdirSync(`./${key.charAt(1).toUpperCase() + key.slice(2)}/${subKey.charAt(1).toUpperCase() + subKey.slice(2)}`, { recursive: true });
console.log(`创建子文件夹: ./${key.charAt(1).toUpperCase() + key.slice(2)}/${subKey.charAt(1).toUpperCase() + subKey.slice(2)}`);

// 生成文件
const code = generateComponentCode(subName, subKey);
fs.writeFileSync(`./${key.charAt(1).toUpperCase() + key.slice(2)}/${subKey.charAt(1).toUpperCase() + subKey.slice(2)}/index.jsx`, code);
console.log(`创建index.jsx文件: ./${key.charAt(1).toUpperCase() + key.slice(2)}/${subKey.charAt(1).toUpperCase() + subKey.slice(2)}/index.jsx`);
});
});


原创的技术分享,请我喝杯奶茶吧