ylyx-cli 1.0.14 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/bin/ylyx.js +168 -168
- package/lib/config.js +283 -299
- package/lib/deploy.js +529 -415
- package/lib/generator.js +192 -192
- package/lib/index.js +113 -113
- package/lib/interactive.js +87 -87
- package/lib/remote.js +179 -179
- package/lib/template.js +174 -174
- package/lib/utils.js +148 -148
- package/package.json +5 -5
- package/templates/react-app/files/README.md +23 -23
- package/templates/react-app/files/package.json +28 -28
- package/templates/react-app/files/public/index.html +11 -11
- package/templates/react-app/files/src/App.css +15 -15
- package/templates/react-app/files/src/App.js +15 -15
- package/templates/react-app/files/src/index.css +10 -10
- package/templates/react-app/files/src/index.js +11 -11
- package/templates/react-app/template.json +11 -11
- package/templates/react-component/files/Component.module.css +5 -5
- package/templates/react-component/files/Component.tsx +31 -31
- package/templates/react-component/files/index.ts +2 -2
- package/templates/react-component/template.json +48 -48
- package/templates/vue-app/files/.editorconfig +14 -14
- package/templates/vue-app/files/.env.development +21 -21
- package/templates/vue-app/files/.env.production +18 -18
- package/templates/vue-app/files/.eslintignore +5 -5
- package/templates/vue-app/files/.eslintrc.js +267 -267
- package/templates/vue-app/files/.prettierignore +11 -11
- package/templates/vue-app/files/.travis.yml +5 -5
- package/templates/vue-app/files/.vscode/extensions.json +7 -7
- package/templates/vue-app/files/.vscode/settings.json +39 -39
- package/templates/vue-app/files/LICENSE +191 -191
- package/templates/vue-app/files/README.md +87 -87
- package/templates/vue-app/files/babel.config.js +11 -11
- package/templates/vue-app/files/jest.config.js +24 -24
- package/templates/vue-app/files/package.json +136 -136
- package/templates/vue-app/files/plopfile.js +7 -7
- package/templates/vue-app/files/postcss.config.js +5 -5
- package/templates/vue-app/files/public/default.js +25 -25
- package/templates/vue-app/files/public/index.html +26 -26
- package/templates/vue-app/files/src/App.vue +54 -54
- package/templates/vue-app/files/src/api/bigDataV/index.js +198 -198
- package/templates/vue-app/files/src/api/data.js +42 -42
- package/templates/vue-app/files/src/api/dataArchive/index.js +186 -186
- package/templates/vue-app/files/src/api/generator/genConfig.js +16 -16
- package/templates/vue-app/files/src/api/generator/generator.js +32 -32
- package/templates/vue-app/files/src/api/indexManagement/index.js +39 -39
- package/templates/vue-app/files/src/api/institutionMonitor/index.js +396 -396
- package/templates/vue-app/files/src/api/institutionSet/index.js +120 -120
- package/templates/vue-app/files/src/api/login.js +44 -44
- package/templates/vue-app/files/src/api/selfStatistics/index.js +50 -50
- package/templates/vue-app/files/src/api/system/code.js +15 -15
- package/templates/vue-app/files/src/api/system/dict.js +34 -34
- package/templates/vue-app/files/src/api/system/dictDetail.js +52 -52
- package/templates/vue-app/files/src/api/system/equipment.js +35 -35
- package/templates/vue-app/files/src/api/system/examManage.js +49 -49
- package/templates/vue-app/files/src/api/system/examProject.js +48 -48
- package/templates/vue-app/files/src/api/system/hosModel.js +35 -35
- package/templates/vue-app/files/src/api/system/hospital.js +88 -88
- package/templates/vue-app/files/src/api/system/job.js +40 -40
- package/templates/vue-app/files/src/api/system/menu.js +67 -67
- package/templates/vue-app/files/src/api/system/model.js +42 -42
- package/templates/vue-app/files/src/api/system/risConfig.js +9 -9
- package/templates/vue-app/files/src/api/system/role.js +64 -64
- package/templates/vue-app/files/src/api/system/sysSet.js +19 -19
- package/templates/vue-app/files/src/api/system/timing.js +41 -41
- package/templates/vue-app/files/src/api/system/user.js +121 -121
- package/templates/vue-app/files/src/api/tools/alipay.js +25 -25
- package/templates/vue-app/files/src/api/tools/email.js +24 -24
- package/templates/vue-app/files/src/api/tools/localStorage.js +27 -27
- package/templates/vue-app/files/src/api/tools/qiniu.js +40 -40
- package/templates/vue-app/files/src/assets/css/font-awesome.css +2342 -2342
- package/templates/vue-app/files/src/assets/css/font-awesome.min.css +2336 -2336
- package/templates/vue-app/files/src/assets/fonts/fontawesome-webfont.svg +2671 -2671
- package/templates/vue-app/files/src/assets/icons/index.js +9 -9
- package/templates/vue-app/files/src/assets/icons/svg/hospital.svg +1 -1
- package/templates/vue-app/files/src/assets/icons/svg/hospital1.svg +1 -1
- package/templates/vue-app/files/src/assets/icons/svg/loginOne.svg +35 -35
- package/templates/vue-app/files/src/assets/icons/svg/loginOneWhite.svg +19 -19
- package/templates/vue-app/files/src/assets/icons/svg/loginOr.svg +93 -93
- package/templates/vue-app/files/src/assets/icons/svg/loginUp.svg +96 -96
- package/templates/vue-app/files/src/assets/icons/svg/monitor.svg +1 -1
- package/templates/vue-app/files/src/assets/icons/svg/offline.svg +1 -1
- package/templates/vue-app/files/src/assets/icons/svg/online.svg +1 -1
- package/templates/vue-app/files/src/assets/icons/svg/system.svg +1 -1
- package/templates/vue-app/files/src/assets/icons/svgo.yml +21 -21
- package/templates/vue-app/files/src/assets/styles/btn.scss +103 -103
- package/templates/vue-app/files/src/assets/styles/eladmin.scss +124 -124
- package/templates/vue-app/files/src/assets/styles/element-ui.scss +217 -217
- package/templates/vue-app/files/src/assets/styles/element-variables.scss +31 -31
- package/templates/vue-app/files/src/assets/styles/global.scss +73 -73
- package/templates/vue-app/files/src/assets/styles/index.scss +220 -220
- package/templates/vue-app/files/src/assets/styles/mixin.scss +60 -60
- package/templates/vue-app/files/src/assets/styles/publicStyle.scss +224 -224
- package/templates/vue-app/files/src/assets/styles/sidebar.scss +239 -239
- package/templates/vue-app/files/src/assets/styles/transition.scss +48 -48
- package/templates/vue-app/files/src/assets/styles/variables.scss +44 -44
- package/templates/vue-app/files/src/common/flexible.js +145 -145
- package/templates/vue-app/files/src/components/Breadcrumb/index.vue +87 -87
- package/templates/vue-app/files/src/components/Crud/CRUD.operation.vue +248 -248
- package/templates/vue-app/files/src/components/Crud/Pagination.vue +20 -20
- package/templates/vue-app/files/src/components/Crud/RR.operation.vue +39 -39
- package/templates/vue-app/files/src/components/Crud/UD.operation.vue +106 -106
- package/templates/vue-app/files/src/components/Crud/crud.js +907 -907
- package/templates/vue-app/files/src/components/Crud/user/UR.operation.vue +154 -154
- package/templates/vue-app/files/src/components/DateRangePicker/index.vue +42 -42
- package/templates/vue-app/files/src/components/DateRangePickerProhibitNext/index.vue +42 -42
- package/templates/vue-app/files/src/components/DateRangePickerProhibitTodayNext/index.vue +48 -48
- package/templates/vue-app/files/src/components/Dict/Dict.js +31 -31
- package/templates/vue-app/files/src/components/Dict/index.js +29 -29
- package/templates/vue-app/files/src/components/Doc/index.vue +16 -16
- package/templates/vue-app/files/src/components/Echarts/BarChart.vue +115 -115
- package/templates/vue-app/files/src/components/Echarts/Category.vue +768 -768
- package/templates/vue-app/files/src/components/Echarts/Funnel.vue +120 -120
- package/templates/vue-app/files/src/components/Echarts/Gauge.vue +74 -74
- package/templates/vue-app/files/src/components/Echarts/Graph.vue +101 -101
- package/templates/vue-app/files/src/components/Echarts/HeatMap.vue +301 -301
- package/templates/vue-app/files/src/components/Echarts/Line3D.vue +110 -110
- package/templates/vue-app/files/src/components/Echarts/PieChart.vue +84 -84
- package/templates/vue-app/files/src/components/Echarts/Point.vue +161 -161
- package/templates/vue-app/files/src/components/Echarts/RadarChart.vue +123 -123
- package/templates/vue-app/files/src/components/Echarts/Rich.vue +160 -160
- package/templates/vue-app/files/src/components/Echarts/Sankey.vue +114 -114
- package/templates/vue-app/files/src/components/Echarts/Scatter.vue +192 -192
- package/templates/vue-app/files/src/components/Echarts/Sunburst.vue +127 -127
- package/templates/vue-app/files/src/components/Echarts/ThemeRiver.vue +233 -233
- package/templates/vue-app/files/src/components/Echarts/WordCloud.vue +201 -201
- package/templates/vue-app/files/src/components/GithubCorner/index.vue +55 -55
- package/templates/vue-app/files/src/components/Hamburger/index.vue +44 -44
- package/templates/vue-app/files/src/components/HeaderSearch/index.vue +196 -196
- package/templates/vue-app/files/src/components/IconSelect/index.vue +74 -74
- package/templates/vue-app/files/src/components/IconSelect/requireIcons.js +10 -10
- package/templates/vue-app/files/src/components/Iframe/index.vue +30 -30
- package/templates/vue-app/files/src/components/JavaEdit/index.vue +78 -78
- package/templates/vue-app/files/src/components/Pagination/index.vue +100 -100
- package/templates/vue-app/files/src/components/PanThumb/index.vue +149 -149
- package/templates/vue-app/files/src/components/ParentView/index.vue +3 -3
- package/templates/vue-app/files/src/components/Permission/index.js +13 -13
- package/templates/vue-app/files/src/components/Permission/permission.js +21 -21
- package/templates/vue-app/files/src/components/RightPanel/index.vue +149 -149
- package/templates/vue-app/files/src/components/Screenfull/index.vue +60 -60
- package/templates/vue-app/files/src/components/SizeSelect/index.vue +60 -60
- package/templates/vue-app/files/src/components/SvgIcon/index.vue +66 -66
- package/templates/vue-app/files/src/components/ThemePicker/index.vue +177 -177
- package/templates/vue-app/files/src/components/UploadExcel/index.vue +149 -149
- package/templates/vue-app/files/src/components/WaterMark/setWaterMark.js +68 -68
- package/templates/vue-app/files/src/components/WaterMark/waterMark.vue +69 -69
- package/templates/vue-app/files/src/components/Xgplayer/index.vue +49 -49
- package/templates/vue-app/files/src/components/YamlEdit/index.vue +81 -81
- package/templates/vue-app/files/src/layout/components/AppMain.vue +67 -67
- package/templates/vue-app/files/src/layout/components/Navbar.vue +209 -209
- package/templates/vue-app/files/src/layout/components/Settings/index.vue +125 -125
- package/templates/vue-app/files/src/layout/components/Sidebar/FixiOSBug.js +26 -26
- package/templates/vue-app/files/src/layout/components/Sidebar/Item.vue +29 -29
- package/templates/vue-app/files/src/layout/components/Sidebar/Link.vue +35 -35
- package/templates/vue-app/files/src/layout/components/Sidebar/Logo.vue +93 -93
- package/templates/vue-app/files/src/layout/components/Sidebar/SidebarItem.vue +104 -104
- package/templates/vue-app/files/src/layout/components/Sidebar/index.vue +79 -79
- package/templates/vue-app/files/src/layout/components/TagsView/ScrollPane.vue +90 -90
- package/templates/vue-app/files/src/layout/components/TagsView/index.vue +299 -299
- package/templates/vue-app/files/src/layout/components/index.js +5 -5
- package/templates/vue-app/files/src/layout/index.vue +119 -119
- package/templates/vue-app/files/src/layout/mixin/ResizeHandler.js +45 -45
- package/templates/vue-app/files/src/main.js +89 -89
- package/templates/vue-app/files/src/mixins/crud.js +348 -348
- package/templates/vue-app/files/src/router/index.js +156 -156
- package/templates/vue-app/files/src/router/routers.js +203 -203
- package/templates/vue-app/files/src/settings.js +57 -57
- package/templates/vue-app/files/src/store/getters.js +29 -29
- package/templates/vue-app/files/src/store/index.js +25 -25
- package/templates/vue-app/files/src/store/modules/api.js +33 -33
- package/templates/vue-app/files/src/store/modules/app.js +55 -55
- package/templates/vue-app/files/src/store/modules/permission.js +79 -79
- package/templates/vue-app/files/src/store/modules/register.js +17 -17
- package/templates/vue-app/files/src/store/modules/settings.js +46 -46
- package/templates/vue-app/files/src/store/modules/tagsView.js +168 -168
- package/templates/vue-app/files/src/store/modules/user.js +119 -119
- package/templates/vue-app/files/src/utils/auth.js +18 -18
- package/templates/vue-app/files/src/utils/base64ToFile.js +27 -27
- package/templates/vue-app/files/src/utils/clipboard.js +36 -36
- package/templates/vue-app/files/src/utils/datetime.js +289 -289
- package/templates/vue-app/files/src/utils/getBlobFile.js +10 -10
- package/templates/vue-app/files/src/utils/getTime.js +29 -29
- package/templates/vue-app/files/src/utils/index.js +520 -520
- package/templates/vue-app/files/src/utils/permission.js +29 -29
- package/templates/vue-app/files/src/utils/pinyin.js +450 -450
- package/templates/vue-app/files/src/utils/request.js +139 -139
- package/templates/vue-app/files/src/utils/rsaEncrypt.js +37 -37
- package/templates/vue-app/files/src/utils/shortcuts.js +210 -210
- package/templates/vue-app/files/src/utils/upload.js +12 -12
- package/templates/vue-app/files/src/utils/validate.js +262 -262
- package/templates/vue-app/files/src/views/components/Echarts.vue +124 -124
- package/templates/vue-app/files/src/views/components/Editor.vue +75 -75
- package/templates/vue-app/files/src/views/components/MarkDown.vue +44 -44
- package/templates/vue-app/files/src/views/components/UVideo.vue +37 -37
- package/templates/vue-app/files/src/views/components/YamlEdit.vue +206 -206
- package/templates/vue-app/files/src/views/components/camera/camera.vue +115 -115
- package/templates/vue-app/files/src/views/components/camera/cameraTest.vue +158 -158
- package/templates/vue-app/files/src/views/components/excel/upload-excel.vue +41 -41
- package/templates/vue-app/files/src/views/components/icons/element-icons.js +74 -74
- package/templates/vue-app/files/src/views/components/icons/index.vue +106 -106
- package/templates/vue-app/files/src/views/components/icons/svg-icons.js +10 -10
- package/templates/vue-app/files/src/views/components/scroll/index.vue +107 -107
- package/templates/vue-app/files/src/views/components/scrollFull/index.vue +99 -99
- package/templates/vue-app/files/src/views/dashboard/LineChart.vue +138 -138
- package/templates/vue-app/files/src/views/dashboard/PanelGroup.vue +173 -173
- package/templates/vue-app/files/src/views/dashboard/mixins/resize.js +57 -57
- package/templates/vue-app/files/src/views/dataArchive/delTable.vue +354 -354
- package/templates/vue-app/files/src/views/dataArchive/exportDetail.vue +121 -121
- package/templates/vue-app/files/src/views/dataArchive/exportList.vue +160 -160
- package/templates/vue-app/files/src/views/dataArchive/index.vue +2104 -2104
- package/templates/vue-app/files/src/views/features/401.vue +83 -83
- package/templates/vue-app/files/src/views/features/404.vue +226 -226
- package/templates/vue-app/files/src/views/features/redirect.vue +12 -12
- package/templates/vue-app/files/src/views/generator/config.vue +348 -348
- package/templates/vue-app/files/src/views/generator/index.vue +143 -143
- package/templates/vue-app/files/src/views/generator/preview.vue +39 -39
- package/templates/vue-app/files/src/views/home.vue +80 -80
- package/templates/vue-app/files/src/views/indexManagement/index.vue +422 -422
- package/templates/vue-app/files/src/views/indexManagement/other.vue +286 -286
- package/templates/vue-app/files/src/views/institutionMonitor/components/LineChart.vue +191 -191
- package/templates/vue-app/files/src/views/institutionMonitor/index.scss +339 -339
- package/templates/vue-app/files/src/views/institutionMonitor/index.vue +1385 -1385
- package/templates/vue-app/files/src/views/institutionMonitor/mixins/resize.js +57 -57
- package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/checkProjectCode.vue +267 -267
- package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/imageAbnormalMonitor.vue +361 -361
- package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/index.vue +686 -686
- package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/leakageAnomaly.vue +270 -270
- package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/moveMonitore.vue +114 -114
- package/templates/vue-app/files/src/views/institutionMonitor/monitorDetail/qualityMonitore.vue +338 -338
- package/templates/vue-app/files/src/views/institutionSet/cloudSet.vue +534 -534
- package/templates/vue-app/files/src/views/institutionSet/index.vue +2550 -2550
- package/templates/vue-app/files/src/views/login.vue +243 -243
- package/templates/vue-app/files/src/views/qualityLogin.vue +280 -280
- package/templates/vue-app/files/src/views/selfStatistics/components/LineChart.vue +296 -296
- package/templates/vue-app/files/src/views/selfStatistics/hospitalDetail/index.vue +533 -533
- package/templates/vue-app/files/src/views/selfStatistics/index.scss +447 -447
- package/templates/vue-app/files/src/views/selfStatistics/index.vue +903 -903
- package/templates/vue-app/files/src/views/selfStatistics/mixins/resize.js +57 -57
- package/templates/vue-app/files/src/views/selfStatistics/monitorDetail/index.vue +285 -285
- package/templates/vue-app/files/src/views/selfStatistics/offlineDetail/index.vue +104 -104
- package/templates/vue-app/files/src/views/selfStatistics/selfActiveDetail/index.vue +104 -104
- package/templates/vue-app/files/src/views/selfStatistics/selfMonitorDetail/index.vue +420 -420
- package/templates/vue-app/files/src/views/selfStatistics/setWaterMark/setWaterMark.js +68 -68
- package/templates/vue-app/files/src/views/selfStatistics/setWaterMark/waterMark.vue +69 -69
- package/templates/vue-app/files/src/views/system/dept/index.vue +144 -144
- package/templates/vue-app/files/src/views/system/dict/dictDetail.vue +146 -146
- package/templates/vue-app/files/src/views/system/dict/index.vue +159 -159
- package/templates/vue-app/files/src/views/system/examModality/index.vue +174 -174
- package/templates/vue-app/files/src/views/system/examPart/index.vue +184 -184
- package/templates/vue-app/files/src/views/system/examProject/index.vue +321 -321
- package/templates/vue-app/files/src/views/system/examine/index.vue +66 -66
- package/templates/vue-app/files/src/views/system/job/index.vue +119 -119
- package/templates/vue-app/files/src/views/system/job/module/form.vue +72 -72
- package/templates/vue-app/files/src/views/system/job/module/header.vue +48 -48
- package/templates/vue-app/files/src/views/system/menu/index.vue +327 -327
- package/templates/vue-app/files/src/views/system/model/index.vue +285 -285
- package/templates/vue-app/files/src/views/system/role/index.vue +434 -434
- package/templates/vue-app/files/src/views/system/sysSet/index.vue +84 -84
- package/templates/vue-app/files/src/views/system/timing/index.vue +289 -289
- package/templates/vue-app/files/src/views/system/timing/log.vue +143 -143
- package/templates/vue-app/files/src/views/system/user/admin.vue +414 -414
- package/templates/vue-app/files/src/views/system/user/center/updateEmail.vue +146 -146
- package/templates/vue-app/files/src/views/system/user/center/updatePass.vue +168 -168
- package/templates/vue-app/files/src/views/system/user/center.vue +140 -140
- package/templates/vue-app/files/src/views/system/user/index.vue +972 -972
- package/templates/vue-app/files/src/views/systemSet/role/index.vue +314 -314
- package/templates/vue-app/files/src/views/systemSet/user/admin.vue +414 -414
- package/templates/vue-app/files/src/views/systemSet/user/center/updateEmail.vue +146 -146
- package/templates/vue-app/files/src/views/systemSet/user/center/updatePass.vue +115 -115
- package/templates/vue-app/files/src/views/systemSet/user/center.vue +351 -351
- package/templates/vue-app/files/src/views/systemSet/user/index.vue +928 -928
- package/templates/vue-app/files/src/views/tools/aliPay/config.vue +102 -102
- package/templates/vue-app/files/src/views/tools/aliPay/index.vue +48 -48
- package/templates/vue-app/files/src/views/tools/aliPay/toPay.vue +91 -91
- package/templates/vue-app/files/src/views/tools/email/config.vue +91 -91
- package/templates/vue-app/files/src/views/tools/email/index.vue +42 -42
- package/templates/vue-app/files/src/views/tools/email/send.vue +159 -159
- package/templates/vue-app/files/src/views/tools/storage/index.vue +35 -35
- package/templates/vue-app/files/src/views/tools/storage/local/index.vue +207 -207
- package/templates/vue-app/files/src/views/tools/storage/qiniu/form.vue +108 -108
- package/templates/vue-app/files/src/views/tools/storage/qiniu/index.vue +241 -241
- package/templates/vue-app/files/src/views/tools/swagger/index.vue +14 -14
- package/templates/vue-app/files/vue.config.js +173 -173
- package/templates/vue-app/template.json +29 -29
package/lib/generator.js
CHANGED
|
@@ -1,192 +1,192 @@
|
|
|
1
|
-
const fs = require('fs-extra');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const handlebars = require('handlebars');
|
|
4
|
-
const glob = require('glob');
|
|
5
|
-
const { minimatch } = require('minimatch');
|
|
6
|
-
const { registerHandlebarsHelpers, log } = require('./utils');
|
|
7
|
-
|
|
8
|
-
// 注册 Handlebars 辅助函数
|
|
9
|
-
registerHandlebarsHelpers(handlebars);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 判断文件是否需要 Handlebars 处理
|
|
13
|
-
*/
|
|
14
|
-
function shouldProcessFile(filePath, templateConfig = {}) {
|
|
15
|
-
const filePathNormalized = filePath.replace(/\\/g, '/'); // 统一使用正斜杠
|
|
16
|
-
|
|
17
|
-
// 如果配置了 skipFiles(黑名单),先检查是否应该跳过
|
|
18
|
-
if (templateConfig.skipFiles && Array.isArray(templateConfig.skipFiles)) {
|
|
19
|
-
const shouldSkip = templateConfig.skipFiles.some((pattern) => {
|
|
20
|
-
if (typeof pattern === 'string') {
|
|
21
|
-
return minimatch(filePathNormalized, pattern, { matchBase: true });
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
});
|
|
25
|
-
if (shouldSkip) {
|
|
26
|
-
return false; // 在黑名单中,不处理
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// 如果配置了 processFiles(白名单),只处理列表中的文件
|
|
31
|
-
if (templateConfig.processFiles && Array.isArray(templateConfig.processFiles)) {
|
|
32
|
-
const inWhitelist = templateConfig.processFiles.some((pattern) => {
|
|
33
|
-
if (typeof pattern === 'string') {
|
|
34
|
-
// 支持通配符匹配,使用 matchBase 以便匹配文件名
|
|
35
|
-
return minimatch(filePathNormalized, pattern, { matchBase: true });
|
|
36
|
-
}
|
|
37
|
-
return false;
|
|
38
|
-
});
|
|
39
|
-
return inWhitelist; // 在白名单中返回 true,不在返回 false
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 默认策略:只处理常见的配置文件和文档文件
|
|
43
|
-
// 跳过代码文件(可能包含 Vue/React 模板语法)
|
|
44
|
-
const processExtensions = ['.json', '.md', '.html', '.txt', '.yml', '.yaml', '.env'];
|
|
45
|
-
const processBasenames = ['.gitignore', '.editorconfig', '.prettierignore', '.eslintignore'];
|
|
46
|
-
const skipExtensions = ['.vue', '.js', '.ts', '.jsx', '.tsx', '.css', '.scss', '.less', '.sass', '.styl'];
|
|
47
|
-
|
|
48
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
49
|
-
const basename = path.basename(filePath).toLowerCase();
|
|
50
|
-
|
|
51
|
-
// 如果文件扩展名在跳过列表中,不处理
|
|
52
|
-
if (skipExtensions.includes(ext)) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 如果文件扩展名或文件名在处理列表中,处理
|
|
57
|
-
if (processExtensions.includes(ext) || processBasenames.includes(basename)) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 默认不处理(安全起见,避免破坏代码文件)
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 处理文件内容,替换模板变量
|
|
67
|
-
*/
|
|
68
|
-
function processFileContent(content, variables) {
|
|
69
|
-
try {
|
|
70
|
-
const template = handlebars.compile(content, { noEscape: true, strict: false });
|
|
71
|
-
return template(variables);
|
|
72
|
-
} catch (e) {
|
|
73
|
-
// 如果处理失败,返回原内容(避免因为 Vue 模板语法等导致失败)
|
|
74
|
-
// 注意:这个函数只应该在 shouldProcessFile 返回 true 时调用
|
|
75
|
-
// 如果还是失败了(比如文件中有复杂语法),返回原内容
|
|
76
|
-
return content;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 处理文件名,替换模板变量
|
|
82
|
-
*/
|
|
83
|
-
function processFileName(fileName, variables) {
|
|
84
|
-
try {
|
|
85
|
-
// 使用 Handlebars 处理文件名,支持 helper 函数
|
|
86
|
-
const template = handlebars.compile(fileName, { noEscape: true, strict: false });
|
|
87
|
-
return template(variables);
|
|
88
|
-
} catch (e) {
|
|
89
|
-
// 如果 Handlebars 编译失败,尝试简单字符串替换
|
|
90
|
-
let result = fileName;
|
|
91
|
-
Object.keys(variables).forEach((key) => {
|
|
92
|
-
if (typeof variables[key] === 'string') {
|
|
93
|
-
const pattern = new RegExp(`\\{\\{${key}\\}\\}`, 'g');
|
|
94
|
-
result = result.replace(pattern, variables[key]);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
return result;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 获取模板文件列表
|
|
103
|
-
*/
|
|
104
|
-
function getTemplateFiles(templatePath) {
|
|
105
|
-
const filesDir = path.join(templatePath, 'files');
|
|
106
|
-
|
|
107
|
-
if (!fs.existsSync(filesDir)) {
|
|
108
|
-
throw new Error(`模板文件目录不存在: ${filesDir}`);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// 使用 glob 匹配所有文件(排除 node_modules 等)
|
|
112
|
-
// 在 Windows 上,glob 需要正斜杠路径
|
|
113
|
-
const pattern = path.join(filesDir, '**', '*').replace(/\\/g, '/');
|
|
114
|
-
const files = glob.sync(pattern, {
|
|
115
|
-
dot: true,
|
|
116
|
-
nodir: true,
|
|
117
|
-
ignore: ['**/node_modules/**', '**/.git/**'],
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
return files.map((file) => {
|
|
121
|
-
// glob 返回的是绝对路径
|
|
122
|
-
return {
|
|
123
|
-
absolutePath: file,
|
|
124
|
-
relativePath: path.relative(filesDir, file),
|
|
125
|
-
};
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 生成代码文件
|
|
131
|
-
*/
|
|
132
|
-
async function generateFiles(templatePath, outputDir, variables, templateConfig = {}) {
|
|
133
|
-
const files = getTemplateFiles(templatePath);
|
|
134
|
-
|
|
135
|
-
if (files.length === 0) {
|
|
136
|
-
log.warn('模板中没有找到文件');
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
log.info(`开始生成 ${files.length} 个文件...\n`);
|
|
141
|
-
|
|
142
|
-
for (const file of files) {
|
|
143
|
-
try {
|
|
144
|
-
// 读取文件内容
|
|
145
|
-
const content = fs.readFileSync(file.absolutePath, 'utf-8');
|
|
146
|
-
|
|
147
|
-
// 判断是否需要 Handlebars 处理
|
|
148
|
-
// 默认只处理配置文件(如 .json, .md),跳过代码文件(如 .vue, .js)避免冲突
|
|
149
|
-
const needProcess = shouldProcessFile(file.relativePath, templateConfig);
|
|
150
|
-
const processedContent = needProcess ? processFileContent(content, variables) : content;
|
|
151
|
-
|
|
152
|
-
// 处理文件名
|
|
153
|
-
let processedFileName = file.relativePath;
|
|
154
|
-
|
|
155
|
-
// 如果配置了 fileMappings,优先使用映射
|
|
156
|
-
if (templateConfig.fileMappings) {
|
|
157
|
-
const fileName = path.basename(file.relativePath);
|
|
158
|
-
const dirName = path.dirname(file.relativePath);
|
|
159
|
-
if (templateConfig.fileMappings[fileName]) {
|
|
160
|
-
const mappedName = processFileName(templateConfig.fileMappings[fileName], variables);
|
|
161
|
-
processedFileName = dirName !== '.' ? path.join(dirName, mappedName) : mappedName;
|
|
162
|
-
} else {
|
|
163
|
-
processedFileName = processFileName(file.relativePath, variables);
|
|
164
|
-
}
|
|
165
|
-
} else {
|
|
166
|
-
processedFileName = processFileName(file.relativePath, variables);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 生成目标文件路径
|
|
170
|
-
const targetPath = path.join(outputDir, processedFileName);
|
|
171
|
-
const targetDir = path.dirname(targetPath);
|
|
172
|
-
|
|
173
|
-
// 确保目录存在
|
|
174
|
-
fs.ensureDirSync(targetDir);
|
|
175
|
-
|
|
176
|
-
// 写入文件
|
|
177
|
-
fs.writeFileSync(targetPath, processedContent, 'utf-8');
|
|
178
|
-
|
|
179
|
-
log.success(`生成: ${processedFileName}`);
|
|
180
|
-
} catch (e) {
|
|
181
|
-
log.error(`处理文件失败 ${file.relativePath}: ${e.message}`);
|
|
182
|
-
throw e;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
console.log('');
|
|
187
|
-
log.success('代码生成完成!');
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
module.exports = {
|
|
191
|
-
generateFiles,
|
|
192
|
-
};
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const handlebars = require('handlebars');
|
|
4
|
+
const glob = require('glob');
|
|
5
|
+
const { minimatch } = require('minimatch');
|
|
6
|
+
const { registerHandlebarsHelpers, log } = require('./utils');
|
|
7
|
+
|
|
8
|
+
// 注册 Handlebars 辅助函数
|
|
9
|
+
registerHandlebarsHelpers(handlebars);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 判断文件是否需要 Handlebars 处理
|
|
13
|
+
*/
|
|
14
|
+
function shouldProcessFile(filePath, templateConfig = {}) {
|
|
15
|
+
const filePathNormalized = filePath.replace(/\\/g, '/'); // 统一使用正斜杠
|
|
16
|
+
|
|
17
|
+
// 如果配置了 skipFiles(黑名单),先检查是否应该跳过
|
|
18
|
+
if (templateConfig.skipFiles && Array.isArray(templateConfig.skipFiles)) {
|
|
19
|
+
const shouldSkip = templateConfig.skipFiles.some((pattern) => {
|
|
20
|
+
if (typeof pattern === 'string') {
|
|
21
|
+
return minimatch(filePathNormalized, pattern, { matchBase: true });
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
});
|
|
25
|
+
if (shouldSkip) {
|
|
26
|
+
return false; // 在黑名单中,不处理
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 如果配置了 processFiles(白名单),只处理列表中的文件
|
|
31
|
+
if (templateConfig.processFiles && Array.isArray(templateConfig.processFiles)) {
|
|
32
|
+
const inWhitelist = templateConfig.processFiles.some((pattern) => {
|
|
33
|
+
if (typeof pattern === 'string') {
|
|
34
|
+
// 支持通配符匹配,使用 matchBase 以便匹配文件名
|
|
35
|
+
return minimatch(filePathNormalized, pattern, { matchBase: true });
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
});
|
|
39
|
+
return inWhitelist; // 在白名单中返回 true,不在返回 false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 默认策略:只处理常见的配置文件和文档文件
|
|
43
|
+
// 跳过代码文件(可能包含 Vue/React 模板语法)
|
|
44
|
+
const processExtensions = ['.json', '.md', '.html', '.txt', '.yml', '.yaml', '.env'];
|
|
45
|
+
const processBasenames = ['.gitignore', '.editorconfig', '.prettierignore', '.eslintignore'];
|
|
46
|
+
const skipExtensions = ['.vue', '.js', '.ts', '.jsx', '.tsx', '.css', '.scss', '.less', '.sass', '.styl'];
|
|
47
|
+
|
|
48
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
49
|
+
const basename = path.basename(filePath).toLowerCase();
|
|
50
|
+
|
|
51
|
+
// 如果文件扩展名在跳过列表中,不处理
|
|
52
|
+
if (skipExtensions.includes(ext)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 如果文件扩展名或文件名在处理列表中,处理
|
|
57
|
+
if (processExtensions.includes(ext) || processBasenames.includes(basename)) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 默认不处理(安全起见,避免破坏代码文件)
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 处理文件内容,替换模板变量
|
|
67
|
+
*/
|
|
68
|
+
function processFileContent(content, variables) {
|
|
69
|
+
try {
|
|
70
|
+
const template = handlebars.compile(content, { noEscape: true, strict: false });
|
|
71
|
+
return template(variables);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
// 如果处理失败,返回原内容(避免因为 Vue 模板语法等导致失败)
|
|
74
|
+
// 注意:这个函数只应该在 shouldProcessFile 返回 true 时调用
|
|
75
|
+
// 如果还是失败了(比如文件中有复杂语法),返回原内容
|
|
76
|
+
return content;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 处理文件名,替换模板变量
|
|
82
|
+
*/
|
|
83
|
+
function processFileName(fileName, variables) {
|
|
84
|
+
try {
|
|
85
|
+
// 使用 Handlebars 处理文件名,支持 helper 函数
|
|
86
|
+
const template = handlebars.compile(fileName, { noEscape: true, strict: false });
|
|
87
|
+
return template(variables);
|
|
88
|
+
} catch (e) {
|
|
89
|
+
// 如果 Handlebars 编译失败,尝试简单字符串替换
|
|
90
|
+
let result = fileName;
|
|
91
|
+
Object.keys(variables).forEach((key) => {
|
|
92
|
+
if (typeof variables[key] === 'string') {
|
|
93
|
+
const pattern = new RegExp(`\\{\\{${key}\\}\\}`, 'g');
|
|
94
|
+
result = result.replace(pattern, variables[key]);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 获取模板文件列表
|
|
103
|
+
*/
|
|
104
|
+
function getTemplateFiles(templatePath) {
|
|
105
|
+
const filesDir = path.join(templatePath, 'files');
|
|
106
|
+
|
|
107
|
+
if (!fs.existsSync(filesDir)) {
|
|
108
|
+
throw new Error(`模板文件目录不存在: ${filesDir}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 使用 glob 匹配所有文件(排除 node_modules 等)
|
|
112
|
+
// 在 Windows 上,glob 需要正斜杠路径
|
|
113
|
+
const pattern = path.join(filesDir, '**', '*').replace(/\\/g, '/');
|
|
114
|
+
const files = glob.sync(pattern, {
|
|
115
|
+
dot: true,
|
|
116
|
+
nodir: true,
|
|
117
|
+
ignore: ['**/node_modules/**', '**/.git/**'],
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return files.map((file) => {
|
|
121
|
+
// glob 返回的是绝对路径
|
|
122
|
+
return {
|
|
123
|
+
absolutePath: file,
|
|
124
|
+
relativePath: path.relative(filesDir, file),
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 生成代码文件
|
|
131
|
+
*/
|
|
132
|
+
async function generateFiles(templatePath, outputDir, variables, templateConfig = {}) {
|
|
133
|
+
const files = getTemplateFiles(templatePath);
|
|
134
|
+
|
|
135
|
+
if (files.length === 0) {
|
|
136
|
+
log.warn('模板中没有找到文件');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
log.info(`开始生成 ${files.length} 个文件...\n`);
|
|
141
|
+
|
|
142
|
+
for (const file of files) {
|
|
143
|
+
try {
|
|
144
|
+
// 读取文件内容
|
|
145
|
+
const content = fs.readFileSync(file.absolutePath, 'utf-8');
|
|
146
|
+
|
|
147
|
+
// 判断是否需要 Handlebars 处理
|
|
148
|
+
// 默认只处理配置文件(如 .json, .md),跳过代码文件(如 .vue, .js)避免冲突
|
|
149
|
+
const needProcess = shouldProcessFile(file.relativePath, templateConfig);
|
|
150
|
+
const processedContent = needProcess ? processFileContent(content, variables) : content;
|
|
151
|
+
|
|
152
|
+
// 处理文件名
|
|
153
|
+
let processedFileName = file.relativePath;
|
|
154
|
+
|
|
155
|
+
// 如果配置了 fileMappings,优先使用映射
|
|
156
|
+
if (templateConfig.fileMappings) {
|
|
157
|
+
const fileName = path.basename(file.relativePath);
|
|
158
|
+
const dirName = path.dirname(file.relativePath);
|
|
159
|
+
if (templateConfig.fileMappings[fileName]) {
|
|
160
|
+
const mappedName = processFileName(templateConfig.fileMappings[fileName], variables);
|
|
161
|
+
processedFileName = dirName !== '.' ? path.join(dirName, mappedName) : mappedName;
|
|
162
|
+
} else {
|
|
163
|
+
processedFileName = processFileName(file.relativePath, variables);
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
processedFileName = processFileName(file.relativePath, variables);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 生成目标文件路径
|
|
170
|
+
const targetPath = path.join(outputDir, processedFileName);
|
|
171
|
+
const targetDir = path.dirname(targetPath);
|
|
172
|
+
|
|
173
|
+
// 确保目录存在
|
|
174
|
+
fs.ensureDirSync(targetDir);
|
|
175
|
+
|
|
176
|
+
// 写入文件
|
|
177
|
+
fs.writeFileSync(targetPath, processedContent, 'utf-8');
|
|
178
|
+
|
|
179
|
+
log.success(`生成: ${processedFileName}`);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
log.error(`处理文件失败 ${file.relativePath}: ${e.message}`);
|
|
182
|
+
throw e;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
console.log('');
|
|
187
|
+
log.success('代码生成完成!');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = {
|
|
191
|
+
generateFiles,
|
|
192
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
const fs = require('fs-extra');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const inquirer = require('inquirer');
|
|
4
|
-
const { getTemplateConfig } = require('./template');
|
|
5
|
-
const { generateFiles } = require('./generator');
|
|
6
|
-
const { getOutputDir, log } = require('./utils');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 生成代码
|
|
10
|
-
*/
|
|
11
|
-
async function generate(templateName, options = {}) {
|
|
12
|
-
try {
|
|
13
|
-
// 获取模板配置
|
|
14
|
-
const templateConfig = getTemplateConfig(templateName);
|
|
15
|
-
const templatePath = templateConfig.path;
|
|
16
|
-
|
|
17
|
-
// 准备变量
|
|
18
|
-
let variables = { ...(options.variables || {}) };
|
|
19
|
-
|
|
20
|
-
// 如果模板定义了变量但用户没有提供,使用交互式收集
|
|
21
|
-
if (templateConfig.variables && Object.keys(templateConfig.variables).length > 0) {
|
|
22
|
-
const missingVars = Object.keys(templateConfig.variables).filter((key) => !(key in variables));
|
|
23
|
-
|
|
24
|
-
if (missingVars.length > 0) {
|
|
25
|
-
const prompts = missingVars.map((key) => {
|
|
26
|
-
const config = templateConfig.variables[key];
|
|
27
|
-
return {
|
|
28
|
-
type: config.type || 'input',
|
|
29
|
-
name: key,
|
|
30
|
-
message: config.message || `请输入 ${key}`,
|
|
31
|
-
default: config.default,
|
|
32
|
-
choices: config.choices,
|
|
33
|
-
validate: config.validate,
|
|
34
|
-
};
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const answers = await inquirer.prompt(prompts);
|
|
38
|
-
variables = { ...variables, ...answers };
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 应用变量替换规则
|
|
43
|
-
if (templateConfig.replacements) {
|
|
44
|
-
Object.entries(templateConfig.replacements).forEach(([key, value]) => {
|
|
45
|
-
if (typeof value === 'string' && value.includes('{{')) {
|
|
46
|
-
// 使用 Handlebars 处理替换规则
|
|
47
|
-
const handlebars = require('handlebars');
|
|
48
|
-
const { registerHandlebarsHelpers } = require('./utils');
|
|
49
|
-
registerHandlebarsHelpers(handlebars);
|
|
50
|
-
const template = handlebars.compile(value, { noEscape: true });
|
|
51
|
-
variables[key] = template(variables);
|
|
52
|
-
} else {
|
|
53
|
-
variables[key] = value;
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 添加一些常用的辅助变量
|
|
59
|
-
if (variables.name) {
|
|
60
|
-
const { camelCase, pascalCase, kebabCase, snakeCase } = require('./utils');
|
|
61
|
-
variables.nameCamel = camelCase(variables.name);
|
|
62
|
-
variables.namePascal = pascalCase(variables.name);
|
|
63
|
-
variables.nameKebab = kebabCase(variables.name);
|
|
64
|
-
variables.nameSnake = snakeCase(variables.name);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 为 React 组件添加 className 辅助变量
|
|
68
|
-
if (variables.componentName) {
|
|
69
|
-
const { pascalCase, kebabCase } = require('./utils');
|
|
70
|
-
const componentNameKebab = kebabCase(variables.componentName);
|
|
71
|
-
const useCSS = variables.useCSS || 'none';
|
|
72
|
-
|
|
73
|
-
// 根据样式方案生成 className 属性字符串(完整的 JSX 属性)
|
|
74
|
-
if (useCSS === 'modules') {
|
|
75
|
-
variables.classNameAttr = "className={`${styles.container} ${className || ''}`}";
|
|
76
|
-
} else if (useCSS === 'css') {
|
|
77
|
-
variables.classNameAttr = `className={\`${componentNameKebab} \${className || ''}\`}`;
|
|
78
|
-
} else {
|
|
79
|
-
variables.classNameAttr = 'className={className}';
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// 生成导入语句
|
|
83
|
-
if (useCSS === 'modules') {
|
|
84
|
-
variables.cssImport = `import styles from './${componentNameKebab}.module.css';`;
|
|
85
|
-
} else if (useCSS === 'styled') {
|
|
86
|
-
variables.cssImport = "import styled from 'styled-components';";
|
|
87
|
-
} else if (useCSS === 'css') {
|
|
88
|
-
variables.cssImport = `import './${componentNameKebab}.css';`;
|
|
89
|
-
} else {
|
|
90
|
-
variables.cssImport = '';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
variables.componentNamePascal = pascalCase(variables.componentName);
|
|
94
|
-
variables.componentNameKebab = componentNameKebab;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 确定输出目录
|
|
98
|
-
const outputDir = getOutputDir(options.outputDir);
|
|
99
|
-
fs.ensureDirSync(outputDir);
|
|
100
|
-
|
|
101
|
-
// 生成文件
|
|
102
|
-
await generateFiles(templatePath, outputDir, variables, templateConfig);
|
|
103
|
-
|
|
104
|
-
log.success(`代码已生成到: ${outputDir}`);
|
|
105
|
-
} catch (error) {
|
|
106
|
-
log.error(`生成失败: ${error.message}`);
|
|
107
|
-
throw error;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
module.exports = {
|
|
112
|
-
generate,
|
|
113
|
-
};
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const inquirer = require('inquirer');
|
|
4
|
+
const { getTemplateConfig } = require('./template');
|
|
5
|
+
const { generateFiles } = require('./generator');
|
|
6
|
+
const { getOutputDir, log } = require('./utils');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 生成代码
|
|
10
|
+
*/
|
|
11
|
+
async function generate(templateName, options = {}) {
|
|
12
|
+
try {
|
|
13
|
+
// 获取模板配置
|
|
14
|
+
const templateConfig = getTemplateConfig(templateName);
|
|
15
|
+
const templatePath = templateConfig.path;
|
|
16
|
+
|
|
17
|
+
// 准备变量
|
|
18
|
+
let variables = { ...(options.variables || {}) };
|
|
19
|
+
|
|
20
|
+
// 如果模板定义了变量但用户没有提供,使用交互式收集
|
|
21
|
+
if (templateConfig.variables && Object.keys(templateConfig.variables).length > 0) {
|
|
22
|
+
const missingVars = Object.keys(templateConfig.variables).filter((key) => !(key in variables));
|
|
23
|
+
|
|
24
|
+
if (missingVars.length > 0) {
|
|
25
|
+
const prompts = missingVars.map((key) => {
|
|
26
|
+
const config = templateConfig.variables[key];
|
|
27
|
+
return {
|
|
28
|
+
type: config.type || 'input',
|
|
29
|
+
name: key,
|
|
30
|
+
message: config.message || `请输入 ${key}`,
|
|
31
|
+
default: config.default,
|
|
32
|
+
choices: config.choices,
|
|
33
|
+
validate: config.validate,
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const answers = await inquirer.prompt(prompts);
|
|
38
|
+
variables = { ...variables, ...answers };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 应用变量替换规则
|
|
43
|
+
if (templateConfig.replacements) {
|
|
44
|
+
Object.entries(templateConfig.replacements).forEach(([key, value]) => {
|
|
45
|
+
if (typeof value === 'string' && value.includes('{{')) {
|
|
46
|
+
// 使用 Handlebars 处理替换规则
|
|
47
|
+
const handlebars = require('handlebars');
|
|
48
|
+
const { registerHandlebarsHelpers } = require('./utils');
|
|
49
|
+
registerHandlebarsHelpers(handlebars);
|
|
50
|
+
const template = handlebars.compile(value, { noEscape: true });
|
|
51
|
+
variables[key] = template(variables);
|
|
52
|
+
} else {
|
|
53
|
+
variables[key] = value;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 添加一些常用的辅助变量
|
|
59
|
+
if (variables.name) {
|
|
60
|
+
const { camelCase, pascalCase, kebabCase, snakeCase } = require('./utils');
|
|
61
|
+
variables.nameCamel = camelCase(variables.name);
|
|
62
|
+
variables.namePascal = pascalCase(variables.name);
|
|
63
|
+
variables.nameKebab = kebabCase(variables.name);
|
|
64
|
+
variables.nameSnake = snakeCase(variables.name);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 为 React 组件添加 className 辅助变量
|
|
68
|
+
if (variables.componentName) {
|
|
69
|
+
const { pascalCase, kebabCase } = require('./utils');
|
|
70
|
+
const componentNameKebab = kebabCase(variables.componentName);
|
|
71
|
+
const useCSS = variables.useCSS || 'none';
|
|
72
|
+
|
|
73
|
+
// 根据样式方案生成 className 属性字符串(完整的 JSX 属性)
|
|
74
|
+
if (useCSS === 'modules') {
|
|
75
|
+
variables.classNameAttr = "className={`${styles.container} ${className || ''}`}";
|
|
76
|
+
} else if (useCSS === 'css') {
|
|
77
|
+
variables.classNameAttr = `className={\`${componentNameKebab} \${className || ''}\`}`;
|
|
78
|
+
} else {
|
|
79
|
+
variables.classNameAttr = 'className={className}';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 生成导入语句
|
|
83
|
+
if (useCSS === 'modules') {
|
|
84
|
+
variables.cssImport = `import styles from './${componentNameKebab}.module.css';`;
|
|
85
|
+
} else if (useCSS === 'styled') {
|
|
86
|
+
variables.cssImport = "import styled from 'styled-components';";
|
|
87
|
+
} else if (useCSS === 'css') {
|
|
88
|
+
variables.cssImport = `import './${componentNameKebab}.css';`;
|
|
89
|
+
} else {
|
|
90
|
+
variables.cssImport = '';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
variables.componentNamePascal = pascalCase(variables.componentName);
|
|
94
|
+
variables.componentNameKebab = componentNameKebab;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 确定输出目录
|
|
98
|
+
const outputDir = getOutputDir(options.outputDir);
|
|
99
|
+
fs.ensureDirSync(outputDir);
|
|
100
|
+
|
|
101
|
+
// 生成文件
|
|
102
|
+
await generateFiles(templatePath, outputDir, variables, templateConfig);
|
|
103
|
+
|
|
104
|
+
log.success(`代码已生成到: ${outputDir}`);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
log.error(`生成失败: ${error.message}`);
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = {
|
|
112
|
+
generate,
|
|
113
|
+
};
|