Skip to content

Commit f560fb4

Browse files
author
pedro
committed
feat:兼容单导出项
1 parent b8f609d commit f560fb4

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

lib/core.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Loader } from "./loader";
1818
import { LinRouter } from "./lin-router";
1919
import { verify } from "./password-hash";
2020
import dayjs from "dayjs";
21+
import { config } from "./config";
2122

2223
// tslint:disable-next-line:variable-name
2324
export const __version__ = "0.0.1";
@@ -92,12 +93,14 @@ export class Lin {
9293
`/${get(plugin, "name")}${get(cont, "opts.prefix")}`
9394
);
9495
get(cont, "stack", []).forEach(ly => {
95-
consola.info(
96-
`loading a route: /plugin/${get(plugin, "name")}${get(
97-
ly,
98-
"path"
99-
)}`
100-
);
96+
if (config.getItem("debug")) {
97+
consola.info(
98+
`loading a route: /plugin/${get(plugin, "name")}${get(
99+
ly,
100+
"path"
101+
)}`
102+
);
103+
}
101104
set(ly, "path", `/${get(plugin, "name")}${get(ly, "path")}`);
102105
});
103106
pluginRp
@@ -106,9 +109,11 @@ export class Lin {
106109
});
107110
} else {
108111
controllers.forEach(cont => {
109-
get(cont, "stack", []).forEach(ly => {
110-
consola.info(`loading a route: /plugin${get(ly, "path")}`);
111-
});
112+
if (config.getItem("debug")) {
113+
get(cont, "stack", []).forEach(ly => {
114+
consola.info(`loading a route: /plugin${get(ly, "path")}`);
115+
});
116+
}
112117
pluginRp
113118
.use((cont as any).routes() as IMiddleware)
114119
.use((cont as any).allowedMethods() as IMiddleware);

lib/loader.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,30 @@ export class Loader {
9595
// 现在只考虑加载.js文件,后续考虑.ts文件
9696
if (extention === ".js") {
9797
const mod = require(file);
98+
// 如果mod 为 koa-router实例
9899
// const exports = get(mod, "default");
99100
// 如果disableLoading为true,则不加载这个文件路由
100101
// tslint:disable-next-line:no-empty
101-
if (!mod[disableLoading]) {
102+
if (mod instanceof Router) {
103+
if (config.getItem("debug")) {
104+
consola.info(`loading a router instance from file: ${file}`);
105+
get(mod, "stack", []).forEach(ly => {
106+
consola.info(`loading a route: ${get(ly, "path")}`);
107+
});
108+
}
109+
mainRouter.use(mod.routes()).use(mod.allowedMethods());
110+
} else if (!mod[disableLoading]) {
102111
Object.keys(mod).forEach(key => {
103112
if (mod[key] instanceof Router) {
104-
consola.info(`loading a router instance :${key} from file: ${file}`);
105-
get(mod[key] , "stack", []).forEach(ly => {
106-
consola.info(`loading a route: ${get(ly, "path")}`);
107-
});
108-
mainRouter
109-
.use(mod[key].routes())
110-
.use(mod[key].allowedMethods());
113+
if (config.getItem("debug")) {
114+
consola.info(
115+
`loading a router instance :${key} from file: ${file}`
116+
);
117+
get(mod[key], "stack", []).forEach(ly => {
118+
consola.info(`loading a route: ${get(ly, "path")}`);
119+
});
120+
}
121+
mainRouter.use(mod[key].routes()).use(mod[key].allowedMethods());
111122
}
112123
});
113124
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lin-mizar",
3-
"version": "0.0.1-alpha.13",
3+
"version": "0.0.1-alpha.14",
44
"description": "The core library of Lin CMS",
55
"main": "lin/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)