-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload.js
57 lines (51 loc) · 1.33 KB
/
load.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
const fs = require('fs');
const path = require('path');
const app = require('./application');
const { Tool } = require('./utils/');
const { PATH, polix } = require('./lib/enum');
/**
* 加载service 和 controller
*/
exports.loadBase = function(type){
let filePath = app.config.root;
filePath = path.join(filePath,type);
let files = [];
try {
files = fs.readdirSync(filePath);
} catch (error) {
// ignore error
}
files.map(file => {
app[`add${Tool.firstUpperCase(type)}`](file.substring(0,file.length - 3),require(path.join(filePath,file)));
});
};
exports.loadMiddware = function(){
let middwarePath = path.join(app.config.root,PATH.MIDDWARE);
let isHave = false;
try {
fs.accessSync(middwarePath,fs.constants.F_OK);
isHave = true;
} catch (error) {
//
}
isHave && app.addMiddwares(require(middwarePath));
};
exports.loadPlugin = function(){
let pluginPath = path.join(app.config.root,PATH.PLUGIN);
let isHave = false;
try {
fs.accessSync(pluginPath,fs.constants.F_OK);
isHave = true;
} catch (error) {
//
}
app.addPlugins(isHave ? pluginPath : void(0));
};
exports.load = function(){
exports.loadMiddware();
exports.loadBase(polix.CONTROLLER);
exports.loadBase(polix.SERVICE);
exports.loadBase(polix.MODEL);
exports.loadBase(polix.ENTITY);
exports.loadPlugin();
};