-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (40 loc) · 1.32 KB
/
index.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
module.exports = function(base2_app, root_dir) {
var conf = config(root_dir)
var api_names = get_dependency_modules(root_dir, conf);
api_names.forEach(function(name){
base2_app.mount_routes(root_dir + '/node_modules/'+ name + '/' + conf.routes_dir);
})
}
function config(root_dir) {
//读取root_dir下的package.json,读取里面的config.moa_api
//读取的配置和config_default合并
var config_default = require('./config_default')
var pkg = require(root_dir + '/package.json')
return (pkg.config && pkg.config.moa_api) ? require('extend')(config_default, pkg.config.moa_api) : config_default
}
function get_dependency_modules(root_dir, config){
var api_names = [];
//读取root_dir下的package.json,读取里面的dependencies里的所有模块名称
var pkg = require(root_dir + '/package.json')
//如果模块名称和config.moa_api.pattern匹配,使用mount_routes挂载
if (pkg.dependencies) {
for(var dep in pkg.dependencies){
var name = get_api_name(dep, config.pattern);
if(name){
console.log(dep);
api_names.push(dep);
}
}
}
return api_names;
}
function get_api_name(name, pattern){
try{
var re = new RegExp(pattern);
if (re.test(name)) {
return name;
}
}catch(e){
console.log('无效正则')
}
}