@slice_cy/mint 1.2.0

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.
Files changed (98) hide show
  1. package/README.md +697 -0
  2. package/app/common/base-response.js +10 -0
  3. package/app/common/business-error.js +10 -0
  4. package/app/common/error-code.js +22 -0
  5. package/app/common/return-utils.js +40 -0
  6. package/app/common/router-schema-validator.js +25 -0
  7. package/app/controller/base.js +24 -0
  8. package/app/controller/project.js +90 -0
  9. package/app/controller/view.js +13 -0
  10. package/app/docs/dsl-doc.js +148 -0
  11. package/app/extend/logger.js +30 -0
  12. package/app/extend/params-valid.js +6 -0
  13. package/app/extend/return-utils.js +9 -0
  14. package/app/middleware/api-sign-verify.js +25 -0
  15. package/app/middleware/err-handler.js +18 -0
  16. package/app/middleware/project-handler.js +25 -0
  17. package/app/pages/boot.js +34 -0
  18. package/app/pages/dashboard/complex-view/header-view/index.vue +108 -0
  19. package/app/pages/dashboard/complex-view/iframe-view/index.vue +37 -0
  20. package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/index.vue +36 -0
  21. package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/index.vue +123 -0
  22. package/app/pages/dashboard/complex-view/schema-view/components/component-config.js +14 -0
  23. package/app/pages/dashboard/complex-view/schema-view/components/createForm/index.vue +72 -0
  24. package/app/pages/dashboard/complex-view/schema-view/components/detailPanel/index.vue +52 -0
  25. package/app/pages/dashboard/complex-view/schema-view/components/editForm/index.vue +87 -0
  26. package/app/pages/dashboard/complex-view/schema-view/hook/use-schema.js +113 -0
  27. package/app/pages/dashboard/complex-view/schema-view/index.vue +114 -0
  28. package/app/pages/dashboard/complex-view/sider-view/index.vue +116 -0
  29. package/app/pages/dashboard/entry.dashboard.js +44 -0
  30. package/app/pages/dashboard/index.vue +60 -0
  31. package/app/pages/service/index.js +70 -0
  32. package/app/pages/store/index.js +4 -0
  33. package/app/pages/store/menu.js +86 -0
  34. package/app/pages/store/project.js +9 -0
  35. package/app/pages/utils/index.js +26 -0
  36. package/app/pages/widgets/header-container/assets/avatar.png +0 -0
  37. package/app/pages/widgets/header-container/assets/icon.png +0 -0
  38. package/app/pages/widgets/header-container/index.vue +108 -0
  39. package/app/pages/widgets/schema-form/components/input/index.vue +155 -0
  40. package/app/pages/widgets/schema-form/components/input-number/index.vue +148 -0
  41. package/app/pages/widgets/schema-form/components/select/index.vue +132 -0
  42. package/app/pages/widgets/schema-form/form-item-cofnig.js +18 -0
  43. package/app/pages/widgets/schema-form/index.vue +137 -0
  44. package/app/pages/widgets/schema-search-bar/component/date-range-picker/index.vue +55 -0
  45. package/app/pages/widgets/schema-search-bar/component/dynamic-select/index.vue +70 -0
  46. package/app/pages/widgets/schema-search-bar/component/input/index.vue +54 -0
  47. package/app/pages/widgets/schema-search-bar/component/select/index.vue +65 -0
  48. package/app/pages/widgets/schema-search-bar/index.vue +143 -0
  49. package/app/pages/widgets/schema-search-bar/search-item-config.js +20 -0
  50. package/app/pages/widgets/schema-table/index.vue +198 -0
  51. package/app/pages/widgets/sider-container/index.vue +30 -0
  52. package/app/post-router.js +9 -0
  53. package/app/pre-middleware.js +37 -0
  54. package/app/public/dist/entry.dashboard.tpl +2 -0
  55. package/app/public/dist/entry.project-list.tpl +2 -0
  56. package/app/public/dist/prod/css/async.609.bcbe5779.css +1 -0
  57. package/app/public/dist/prod/css/async.952.9bc7c180.css +1 -0
  58. package/app/public/dist/prod/css/common.1a98227b.css +1 -0
  59. package/app/public/dist/prod/css/entry.project-list.14f64dd9.css +1 -0
  60. package/app/public/dist/prod/js/142.56646c73.bundle.js +1 -0
  61. package/app/public/dist/prod/js/609.8270b20d.bundle.js +1 -0
  62. package/app/public/dist/prod/js/68.2813fafe.bundle.js +1 -0
  63. package/app/public/dist/prod/js/952.c76c04fb.bundle.js +1 -0
  64. package/app/public/dist/prod/js/common.9985f827.bundle.js +1 -0
  65. package/app/public/dist/prod/js/entry.dashboard.cd7591e4.bundle.js +1 -0
  66. package/app/public/dist/prod/js/entry.project-list.beb0fa9d.bundle.js +1 -0
  67. package/app/public/dist/prod/js/runtime.5602b906.bundle.js +1 -0
  68. package/app/public/dist/prod/js/vendor.709fbf32.bundle.js +53 -0
  69. package/app/public/dist/prod/static/images/avatar.78e68eaa.png +0 -0
  70. package/app/public/dist/static/logo.png +0 -0
  71. package/app/public/dist/static/normalize.css +1 -0
  72. package/app/public/static/logo.png +0 -0
  73. package/app/public/static/normalize.css +267 -0
  74. package/app/router/project.js +6 -0
  75. package/app/router/view.js +5 -0
  76. package/app/router-schema/project.js +14 -0
  77. package/app/service/base.js +22 -0
  78. package/app/service/project.js +49 -0
  79. package/app/view/entry.tpl +18 -0
  80. package/app/webpack/config/webpack.base.js +288 -0
  81. package/app/webpack/config/webpack.dev.js +53 -0
  82. package/app/webpack/config/webpack.prod.js +106 -0
  83. package/app/webpack/dev.js +34 -0
  84. package/app/webpack/libs/block.js +1 -0
  85. package/app/webpack/prod.js +21 -0
  86. package/config/config.default.js +5 -0
  87. package/index.js +37 -0
  88. package/mint-core/env.js +23 -0
  89. package/mint-core/index.js +101 -0
  90. package/mint-core/loader/config.js +59 -0
  91. package/mint-core/loader/controller.js +59 -0
  92. package/mint-core/loader/extend.js +42 -0
  93. package/mint-core/loader/middleware.js +54 -0
  94. package/mint-core/loader/router-schema.js +39 -0
  95. package/mint-core/loader/router.js +36 -0
  96. package/mint-core/loader/service.js +57 -0
  97. package/model/index.js +94 -0
  98. package/package.json +93 -0
@@ -0,0 +1,53 @@
1
+ const webpackConfig = require("./webpack.base");
2
+ const { mergeWithRules } = require("webpack-merge");
3
+ const path = require("path");
4
+ const webpack = require("webpack");
5
+ const DEV_SERVER_CONFIG = {
6
+ HOST: "127.0.0.1",
7
+ PORT: 9003,
8
+ TIMEOUT: 20000,
9
+ HMR_PATH: "__webpack_hmr",
10
+ };
11
+
12
+ /**
13
+ * 基于webapck.base.js的entry入口配置,给每一个入口的添加一个指定hmr代码的文件
14
+ * HMR 客户端模块作为 entry 的一部分打进 bundl
15
+ * 这段代码应该就是利用EventSource或者webstock与指定的目标服务器之间建立双向通信连接
16
+ */
17
+ const { HOST, PORT, TIMEOUT, HMR_PATH } = DEV_SERVER_CONFIG;
18
+ const entryObj = {};
19
+ Object.entries(webpackConfig.entry).forEach(([key, value]) => {
20
+ if (key === "vendor") return;
21
+ entryObj[key] = [
22
+ value,
23
+ `${require.resolve("webpack-hot-middleware/client")}?path=http://${HOST}:${PORT}/${HMR_PATH}&timeout=${TIMEOUT}&reload=true`,
24
+ ];
25
+ });
26
+
27
+ const webapckDevConfig = mergeWithRules({
28
+ module: {
29
+ rules: {
30
+ test: "match",
31
+ use: "replace",
32
+ },
33
+ },
34
+ })(webpackConfig, {
35
+ mode: "development",
36
+ entry: entryObj,
37
+ output: {
38
+ // 产物输出的目标目录
39
+ path: path.resolve(process.cwd(), "./app/public/dist/dev"),
40
+ // 打包的js文件的命名
41
+ filename: "js/[name].bundle.js",
42
+ publicPath: `http://${HOST}:${PORT}/public/dist/dev/`,
43
+ // 异步模块的命名
44
+ chunkFilename: "js/async.[name].chunk.js",
45
+ clean: true,
46
+ },
47
+ plugins: [new webpack.HotModuleReplacementPlugin({})],
48
+ });
49
+
50
+ module.exports = {
51
+ webapckDevConfig,
52
+ DEV_SERVER_CONFIG,
53
+ };
@@ -0,0 +1,106 @@
1
+ /**
2
+ * 生产环境构建配置:
3
+ * 1. 在基础配置上合并生产配置,产物输出到 app/public/dist/prod,并在构建前清理旧文件。
4
+ * 2. JS、CSS 和异步 chunk 使用 contenthash 命名,配合浏览器长期缓存,只在内容变化时更新资源 URL。
5
+ * 3. 抽离并压缩 CSS,压缩 JS,同时移除 console、debugger 和注释,减少生产包体积。
6
+ * 4. CSS/LESS/JS loader 以及 JS/CSS 压缩启用多进程,降低构建和压缩耗时。
7
+ * 5. 给注入到页面的静态资源添加 crossorigin="anonymous",避免跨源资源请求携带用户凭证。
8
+ * 6. 关闭 sourcemap,避免生产环境暴露源码、源码路径和调试信息。
9
+ */
10
+ const webpackConfig = require("./webpack.base");
11
+ const { mergeWithRules } = require("webpack-merge");
12
+ const path = require("path");
13
+ const os = require("os");
14
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin");
15
+ const CssMinimizer = require("css-minimizer-webpack-plugin");
16
+ const HtmlWebpackInjectAttributesPlugin = require("html-webpack-inject-attributes-plugin");
17
+ const TerserPlugin = require("terser-webpack-plugin");
18
+
19
+ module.exports = mergeWithRules({
20
+ module: {
21
+ rules: {
22
+ test: "match",
23
+ use: "replace",
24
+ },
25
+ },
26
+ })(webpackConfig, {
27
+ mode: "production",
28
+ devtool: false,
29
+ output: {
30
+ // 产物输出的目标目录
31
+ path: path.resolve(process.cwd(), "./app/public/dist/prod"),
32
+ // 打包的js文件的命名
33
+ filename: "js/[name].[contenthash:8].bundle.js",
34
+ publicPath: "/dist/prod/",
35
+ clean: true,
36
+ },
37
+ module: {
38
+ rules: [
39
+ {
40
+ test: /\.css$/i,
41
+ use: [
42
+ MiniCssExtractPlugin.loader,
43
+ {
44
+ loader: require.resolve("thread-loader"),
45
+ options: {
46
+ workers: os.cpus().length,
47
+ },
48
+ },
49
+
50
+ require.resolve("css-loader"),
51
+ ],
52
+ },
53
+ {
54
+ test: /\.less$/i,
55
+ use: [
56
+ MiniCssExtractPlugin.loader,
57
+ {
58
+ loader: require.resolve("thread-loader"),
59
+ options: {
60
+ workers: os.cpus().length,
61
+ },
62
+ },
63
+ require.resolve("css-loader"),
64
+ require.resolve("less-loader"),
65
+ ],
66
+ },
67
+ {
68
+ test: /\.js$/i,
69
+ use: [
70
+ {
71
+ loader: require.resolve("thread-loader"),
72
+ options: {
73
+ workers: os.cpus().length,
74
+ },
75
+ },
76
+ require.resolve("babel-loader"),
77
+ ],
78
+ },
79
+ ],
80
+ },
81
+ plugins: [
82
+ // 抽离css成独立文件
83
+ new MiniCssExtractPlugin({
84
+ filename: "css/[name].[contenthash:8].css",
85
+ chunkFilename: "css/async.[name].[contenthash:8].css",
86
+ }),
87
+ new HtmlWebpackInjectAttributesPlugin({
88
+ crossorigin: "anonymous",
89
+ }),
90
+ ],
91
+ optimization: {
92
+ minimize: true,
93
+ minimizer: [
94
+ // 压缩js
95
+ new TerserPlugin({
96
+ // 多进程压缩
97
+ parallel: true,
98
+ // 提取注释到单独文件(生产关闭)
99
+ extractComments: false,
100
+ }),
101
+ new CssMinimizer({
102
+ parallel: true,
103
+ }),
104
+ ],
105
+ },
106
+ });
@@ -0,0 +1,34 @@
1
+ const express = require("express");
2
+ const { webapckDevConfig, DEV_SERVER_CONFIG } = require("./config/webpack.dev");
3
+ const webpackDevMiddleware = require("webpack-dev-middleware");
4
+ const webpack = require("webpack");
5
+ const webpackHotMiddleware = require("webpack-hot-middleware");
6
+ const compiler = webpack(webapckDevConfig);
7
+ const cors = require("cors");
8
+
9
+ module.exports = () => {
10
+ const app = express();
11
+ // 跨域中间件
12
+ app.use(cors());
13
+
14
+ // 负责监听文件系统的改动,根据webpack配置重新编译,将tpl文件落地到磁盘,其他文件落地到内存中
15
+ // 配置publicPath,可以拦截对应的文件请求,从内存中返回出去
16
+ app.use(
17
+ webpackDevMiddleware(compiler, {
18
+ writeToDisk: (filePath) => /.tpl$/.test(filePath),
19
+ publicPath: webapckDevConfig.output.publicPath,
20
+ }),
21
+ );
22
+
23
+ // webpack编译完成,webpack-hot-middleware中间件会监听到done钩子,通过sse或者webstock通知客户端,重新请求资源清单进行热更新
24
+ app.use(
25
+ webpackHotMiddleware(compiler, {
26
+ path: `/${DEV_SERVER_CONFIG.HMR_PATH}`,
27
+ }),
28
+ );
29
+
30
+ // 服务启动
31
+ app.listen(DEV_SERVER_CONFIG.PORT, DEV_SERVER_CONFIG.HOST, () => {
32
+ console.log(`listening on http://${DEV_SERVER_CONFIG.HOST}:${DEV_SERVER_CONFIG.PORT}`);
33
+ });
34
+ };
@@ -0,0 +1 @@
1
+ module.exports = {};
@@ -0,0 +1,21 @@
1
+ const webpack = require("webpack");
2
+ const webpackConfig = require("./config/webpack.prod");
3
+
4
+ module.exports = () => {
5
+ webpack(webpackConfig, (err, stats) => {
6
+ if (err) {
7
+ console.log(err);
8
+ } else {
9
+ console.log(
10
+ stats.toString({
11
+ colors: true,
12
+ modules: false, // 关闭打印单个module文件明细(解决日志刷屏)
13
+ children: false, // 关闭HtmlWebpackPlugin子编译明细
14
+ chunks: true,
15
+ chunkModules: false,
16
+ timing: true, // 保留总耗时
17
+ }),
18
+ );
19
+ }
20
+ });
21
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = (app) => {
2
+ return {
3
+ name: "mint",
4
+ };
5
+ };
package/index.js ADDED
@@ -0,0 +1,37 @@
1
+ const mintCore = require("./mint-core");
2
+ // 引入前端工程化构建方法
3
+ const FEBuildDev = require("./app/webpack/dev");
4
+ const FEBuildProd = require("./app/webpack/prod");
5
+ /**
6
+ * 对外启动暴露的方法
7
+ * @param {object} options
8
+ * @returns
9
+ */
10
+ const start = (options = {}) => {
11
+ const app = mintCore.start(options);
12
+ return app;
13
+ };
14
+
15
+ /**
16
+ * 编译构建前端工程
17
+ * @param {stirng} env development / production
18
+ */
19
+ const frontendBuild = (env = "development") => {
20
+ if (env === "development") {
21
+ FEBuildDev();
22
+ } else {
23
+ FEBuildProd();
24
+ }
25
+ };
26
+
27
+ module.exports = {
28
+ start,
29
+ frontendBuild,
30
+ Controller: {
31
+ Base: require("./app/controller/base"),
32
+ },
33
+ Service: {
34
+ Base: require("./app/service/base"),
35
+ },
36
+ BaseResponse: require("./app/common/base-response"),
37
+ };
@@ -0,0 +1,23 @@
1
+ module.exports = (app) => {
2
+ return {
3
+ // 判断当前环境是否开发环境
4
+ isDevelopment() {
5
+ return process.env.NODE_ENV === "development";
6
+ },
7
+
8
+ // 判断当前环境是否测试环境
9
+ isTest() {
10
+ return process.env.NODE_ENV === "test";
11
+ },
12
+
13
+ // 判断当前环境是否生产环境
14
+ isProduction() {
15
+ return process.env.NODE_ENV === "production";
16
+ },
17
+
18
+ // 获取当前环境
19
+ get() {
20
+ return process.env.NODE_ENV ?? "development";
21
+ },
22
+ };
23
+ };
@@ -0,0 +1,101 @@
1
+ const Koa = require("koa");
2
+ const path = require("path");
3
+ const env = require("./env");
4
+ const { sep } = path;
5
+
6
+ const middlewareLoader = require("./loader/middleware");
7
+ const routerLoader = require("./loader/router");
8
+ const routerSchema = require("./loader/router-schema");
9
+ const controllerLoader = require("./loader/controller");
10
+ const serviceLoader = require("./loader/service");
11
+ const configLoader = require("./loader/config");
12
+ const extendLoader = require("./loader/extend");
13
+
14
+ module.exports = {
15
+ /**
16
+ * 启动项目
17
+ * @param {object} options 项目配置
18
+ * @param {string} options.name 项目名
19
+ */
20
+ start(options = {}) {
21
+ const app = new Koa();
22
+
23
+ // 项目配置
24
+ app.options = options;
25
+
26
+ // 基础路径
27
+ app.baseDir = process.cwd();
28
+
29
+ // 应用路径
30
+ app.businessPath = path.resolve(app.baseDir, `.${sep}app`);
31
+
32
+ // 环境变量加载器
33
+ app.$env = env(app);
34
+ console.log(`-- [start] env: ${app.$env.get()}`);
35
+
36
+ // 配置加载器
37
+ configLoader(app);
38
+ console.log(`-- [start] config loader done--`);
39
+
40
+ // 拓展加载器
41
+ extendLoader(app);
42
+ console.log(`-- [start] extend loader done--`);
43
+
44
+ // 服务加载器
45
+ serviceLoader(app);
46
+ console.log(`-- [start] service loader done--`);
47
+
48
+ // middleware加载器
49
+ middlewareLoader(app);
50
+ console.log(`-- [start] middleware loader done--`);
51
+
52
+ // 路由参数配置加载器
53
+ routerSchema(app);
54
+ console.log(`-- [start] router schema loader done--`);
55
+
56
+ // 控制器加载器
57
+ controllerLoader(app);
58
+ console.log(`-- [start] controller loader done--`);
59
+
60
+ // 前置中间件注册
61
+ try {
62
+ require(path.resolve(__dirname, `..${sep}app${sep}pre-middleware.js`))(app);
63
+ console.log(`-- [start] mint pre middleware loader done--`);
64
+ } catch (err) {
65
+ console.error(`-- [exception] mint pre middleware load error ] ${err.message} --`);
66
+ }
67
+
68
+ try {
69
+ require(path.resolve(app.businessPath, `.${sep}pre-middleware.js`))(app);
70
+ console.log(`-- [start] business pre middleware loader done--`);
71
+ } catch (err) {
72
+ console.error(`-- [exception] business pre middleware load error ] ${err.message} --`);
73
+ }
74
+
75
+ // 路由加载器
76
+ try {
77
+ routerLoader(app, require(path.join(__dirname, `..${sep}app${sep}post-router.js`)));
78
+ console.log(`-- [start] business post router loader done--`);
79
+ } catch (err) {
80
+ console.error(`-- [exception] business post router loader error] ${err.message}--`);
81
+ }
82
+
83
+ try {
84
+ routerLoader(app, require(path.join(app.businessPath, `post-router.js`)));
85
+ console.log(`-- [start] business post router loader done--`);
86
+ } catch (err) {
87
+ console.error(`-- [exception] business post router loader error] ${err.message}--`);
88
+ }
89
+
90
+ // 启动服务
91
+ try {
92
+ const port = process.env.PORT || 8080;
93
+ const host = process.env.HOST || "0.0.0.0";
94
+ app.listen(port, host);
95
+ console.log(`${app.options.name} Server listening on ${host}:${port}`);
96
+ } catch (error) {
97
+ console.error(error);
98
+ }
99
+ return app;
100
+ },
101
+ };
@@ -0,0 +1,59 @@
1
+ const path = require("path");
2
+ const { sep } = path;
3
+ /**
4
+ * 把多个config合并成一个
5
+ * @param {object} app koa 实例
6
+ *
7
+ * 文件系统:config
8
+ * | config.development.js
9
+ * | config.default.js
10
+ * | config.test.js
11
+ * | config.production.js
12
+ *
13
+ * 结果: app.config = { ...config.default, ...config.[env] }
14
+ */
15
+
16
+ module.exports = (app) => {
17
+ // 获取business config目录路径
18
+ const businessConfigPath = path.join(app.baseDir, `config`);
19
+ // mint 内部的config目录路径
20
+ const mintConfigPath = path.join(__dirname, `..${sep}..${sep}config`);
21
+
22
+ // 获取默认的config配置
23
+ let defaultConfig = {};
24
+ try {
25
+ defaultConfig = require(path.join(mintConfigPath, "config.default.js"))(app);
26
+ } catch (err) {
27
+ console.error(`mint default config load exception] ${err.message}`);
28
+ }
29
+
30
+ try {
31
+ const applicationDefaultConfig = require(path.join(businessConfigPath, "config.default.js"))(
32
+ app,
33
+ );
34
+ defaultConfig = Object.assign(defaultConfig, applicationDefaultConfig);
35
+ } catch (err) {
36
+ console.error(`application default config load exception] ${err.message}`);
37
+ }
38
+
39
+ // 获取环境配置
40
+ let envConfig = {};
41
+ const { isDevelopment, isTest, isProduction } = app.$env;
42
+ try {
43
+ if (isDevelopment()) {
44
+ envConfig = require(path.join(businessConfigPath, `config.development.js`))(app);
45
+ } else if (isTest()) {
46
+ envConfig = require(path.join(businessConfigPath, `config.test.js`))(app);
47
+ } else if (isProduction()) {
48
+ envConfig = require(path.join(businessConfigPath, `config.production.js`))(app);
49
+ }
50
+ } catch (err) {
51
+ console.error(`[env config load exception] ${err.message}`);
52
+ }
53
+
54
+ // 合并配置
55
+ app.config = {
56
+ ...defaultConfig,
57
+ ...envConfig,
58
+ };
59
+ };
@@ -0,0 +1,59 @@
1
+ const path = require("path");
2
+ const glob = require("glob");
3
+ const { sep } = path;
4
+ /**
5
+ * 把多个中间件函数注册到app.controller
6
+ * @param {object} app koa 实例
7
+ *
8
+ * 文件系统:app/controller
9
+ * | custom-model
10
+ * | xxxx.js
11
+ * | custom-controller.js
12
+ * 结果: app.controller.customModel.customController
13
+ */
14
+
15
+ module.exports = (app) => {
16
+ // 获取business app/controller目录
17
+ const businessControllerPath = path.join(app.businessPath, `controller`);
18
+ // 获取mint app/controller目录
19
+ const mintControllerPath = path.join(__dirname, `..${sep}..${sep}app${sep}controller`);
20
+
21
+ // 获取所有文件
22
+ const businessFileList = glob.sync(path.join(businessControllerPath, "**", "*.js"));
23
+ const mintFileList = glob.sync(path.join(mintControllerPath, "**", "*.js"));
24
+
25
+ const controller = {};
26
+
27
+ const handleFile = (filePath, controllerPath) => {
28
+ let tempController = controller;
29
+ // 从路径中截取合法的子路径
30
+ let relPath = path.relative(controllerPath, filePath);
31
+ // 将 - 或者 _ 转换成驼峰
32
+ relPath = relPath.replace(/[-_](\w)/gi, (_, letter) => letter.toUpperCase());
33
+ const parts = relPath.split(sep);
34
+ let filename = parts.pop();
35
+ filename = path.parse(filename).name;
36
+ const names = [...parts, filename];
37
+
38
+ // 创建嵌套对象指向
39
+
40
+ for (let i = 0, len = names.length; i < len; ++i) {
41
+ if (i == len - 1) {
42
+ const Controller = require(filePath)(app);
43
+ tempController[names[i]] = new Controller();
44
+ } else {
45
+ if (!tempController[names[i]]) {
46
+ tempController[names[i]] = {};
47
+ }
48
+ tempController = tempController[names[i]];
49
+ }
50
+ }
51
+ };
52
+
53
+ // 遍历所有文件
54
+ mintFileList.forEach((filePath) => handleFile(filePath, mintControllerPath));
55
+ businessFileList.forEach((filePath) => handleFile(filePath, businessControllerPath));
56
+
57
+ // 挂载到app.controller中
58
+ app.controller = controller;
59
+ };
@@ -0,0 +1,42 @@
1
+ const path = require("path");
2
+ const glob = require("glob");
3
+ const { sep } = path;
4
+ /**
5
+ * 把每个extend文件名当做key,合并到app实例中
6
+ * @param {object} app koa 实例
7
+ *
8
+ * 文件系统:app/extend
9
+ * | xxx.js
10
+ * | xxx.js
11
+ *
12
+ * 结果: 拓展app字段
13
+ */
14
+
15
+ module.exports = (app) => {
16
+ // 读取app/extend目录下的所有文件
17
+ const businessExtendPath = path.join(app.businessPath, `extend`);
18
+ // 读取mint内部 app/extend目录下的所有文件
19
+ const mintExtendPath = path.join(__dirname, `..${sep}..${sep}app${sep}extend`);
20
+ // 获取所有文件
21
+ const mintFileList = glob.sync(path.join(mintExtendPath, `*.js`));
22
+ const businessFileList = glob.sync(path.join(businessExtendPath, `*.js`));
23
+ const handleFile = (filePath) => {
24
+ // 截取有效文件名
25
+ let name = path.relative(businessExtendPath, filePath);
26
+ // 去掉文件名后缀
27
+ name = path.basename(name, ".js");
28
+ // 将 - 或者 _ 转换成驼峰
29
+ name = name.replace(/[-_](\w)/gi, (_, letter) => letter.toUpperCase());
30
+ // 给app添加字段
31
+ for (const key in app) {
32
+ if (key === name) {
33
+ console.log(`[extend loader exception] ${name} is exist`);
34
+ return;
35
+ }
36
+ }
37
+ app[name] = require(filePath)(app);
38
+ };
39
+ // 遍历所有文件
40
+ mintFileList.forEach((filePath) => handleFile(filePath));
41
+ businessFileList.forEach((filePath) => handleFile);
42
+ };
@@ -0,0 +1,54 @@
1
+ const path = require("path");
2
+ const glob = require("glob");
3
+ const { sep } = path;
4
+ /**
5
+ * 把多个中间件函数注册到app.middlewares中
6
+ * @param {object} app koa 实例
7
+ *
8
+ * 文件系统:app/middleware
9
+ * | custom-model
10
+ * | xxxx.js
11
+ * | custom-middleware.js
12
+ *
13
+ * 结果: app.middlewares.customModel.customMiddleware
14
+ */
15
+
16
+ module.exports = (app) => {
17
+ // 获取business app/middleware目录路径
18
+ const businessMiddlewarePath = path.resolve(app.businessPath, `.${sep}middleware`);
19
+ // 获取mint app/middleware目录路径
20
+ const mintMiddlewarePath = path.resolve(__dirname, `..${sep}..${sep}app${sep}middleware`);
21
+ // 获取所有文件
22
+ const businessFileList = glob.sync(path.resolve(businessMiddlewarePath, `.${sep}**${sep}*.js`));
23
+ const mintFileList = glob.sync(path.resolve(mintMiddlewarePath, `.${sep}**${sep}*.js`));
24
+
25
+ const middlewares = {};
26
+ const handleFile = (filePath, middlewaresPath) => {
27
+ let tempMiddleware = middlewares;
28
+ // 从路径中截取合法的子路径
29
+ let relPath = path.relative(middlewaresPath, filePath);
30
+ // 将 - 或者 _ 转换成驼峰
31
+ relPath = relPath.replace(/[-_](\w)/gi, (_, letter) => letter.toUpperCase());
32
+ const parts = relPath.split(sep);
33
+ let filename = parts.pop();
34
+ filename = path.parse(filename).name;
35
+ const names = [...parts, filename];
36
+ // 创建嵌套对象指向
37
+ for (let i = 0, len = names.length; i < len; ++i) {
38
+ if (i == len - 1) {
39
+ tempMiddleware[names[i]] = require(filePath)(app);
40
+ } else {
41
+ if (!tempMiddleware[names[i]]) {
42
+ tempMiddleware[names[i]] = {};
43
+ }
44
+ tempMiddleware = tempMiddleware[names[i]];
45
+ }
46
+ }
47
+ };
48
+ // 遍历所有文件
49
+ mintFileList.forEach((file) => handleFile(file, mintMiddlewarePath));
50
+ businessFileList.forEach((file) => handleFile(file, businessMiddlewarePath));
51
+
52
+ // 挂载到app.middlewares中
53
+ app.middlewares = middlewares;
54
+ };
@@ -0,0 +1,39 @@
1
+ const path = require("path");
2
+ const glob = require("glob");
3
+ const { sep } = path;
4
+ /**
5
+ * 把router-schema目录下的js文件合并成一个obj对象
6
+ * @param {object} app koa 实例
7
+ *
8
+ * 目标:app/router-schema目录下的js文件的内容合并成一份schema配置
9
+ * 文件系统:app/router-schema
10
+ * |
11
+ * | xxx.js
12
+ * | xxx.js
13
+ * 结果:app.routerSchema = {
14
+ * [schemaName1]: schemaConfig,
15
+ * [schemaName2]: schemaConfig
16
+ * }
17
+ */
18
+
19
+ module.exports = (app) => {
20
+ // 获取business router-schema的目录路径
21
+ const businessRouterSchemaPath = path.join(app.businessPath, "router-schema");
22
+ // 获取mint router-schema的目录路径
23
+ const eplisSchemaPath = path.join(__dirname, `..${sep}..${sep}app${sep}router-schema`);
24
+
25
+ // 获取router-schema目录下的所有文件
26
+ const businessFileList = glob.sync(path.join(businessRouterSchemaPath, `*.js`));
27
+ const mintFileList = glob.sync(path.join(eplisSchemaPath, `*.js`));
28
+ // 遍历文件
29
+ const routerSchema = {};
30
+
31
+ const handleFile = (filePath) => {
32
+ Object.assign(routerSchema, require(filePath));
33
+ };
34
+ mintFileList.forEach(handleFile);
35
+ businessFileList.forEach(handleFile);
36
+
37
+ // 得到最终的schema配置
38
+ app.routerSchema = routerSchema;
39
+ };
@@ -0,0 +1,36 @@
1
+ const path = require("path");
2
+ const glob = require("glob");
3
+ const KoaRouter = require("koa-router");
4
+ const { sep } = path;
5
+ /**
6
+ * 注册到app.router
7
+ * @param {object} app koa 实例
8
+ *
9
+ * 文件系统:app/router
10
+ * | user
11
+ * | xxx.js
12
+ */
13
+
14
+ module.exports = (app, callback) => {
15
+ // 读取business app/router目录路径
16
+ const businessRouterPath = path.join(app.businessPath, `router`);
17
+ // 获取mint app/router目录路径
18
+ const mintRouterPath = path.join(__dirname, `..${sep}..${sep}app${sep}router`);
19
+
20
+ // 获取router目录下的所有文件
21
+ const businessFileList = glob.sync(path.join(businessRouterPath, "**", "*.js"));
22
+ const mintFileList = glob.sync(path.join(mintRouterPath, "**", "*.js"));
23
+
24
+ const router = new KoaRouter();
25
+
26
+ // 遍历所有文件
27
+ [...mintFileList, ...businessFileList].forEach((filePath) => {
28
+ require(filePath)(app, router);
29
+ });
30
+
31
+ callback && callback(app, router);
32
+
33
+ // 注册
34
+ app.use(router.routes());
35
+ app.use(router.allowedMethods());
36
+ };