-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.js
More file actions
162 lines (145 loc) · 4.81 KB
/
lang.js
File metadata and controls
162 lines (145 loc) · 4.81 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('fs');
const path = require('path');
const { load } = require('./file');
const { assertAndReturn } = require('./framework');
const intersect = require('@evio/intersect');
exports.checkPortCanUse = checkPortCanUse;
exports.loadFileWorker = loadFileWorker;
exports.installPlugin = installPlugin;
exports.mergeConfigs = mergeConfigs;
exports.delay = delay;
async function delay(time) {
await new Promise(resolve => setTimeout(resolve, time));
}
function checkPortCanUse(port, logger) {
return new Promise((resolve, reject) => {
const args = [];
if (port) {
args.push(port);
}
args.push((err, port) => {
if (err) {
err.name = 'ClusterPortConflictError';
err.message = '[master] try get free port error, ' + err.message;
// TODO: this.logger.error(err);
if (logger) {
logger.error(err);
}
reject(err);
process.exit(1);
}
resolve(port);
});
detectPort(...args);
});
}
async function loadFileWorker(file, object) {
if (file && fs.existsSync(file)) {
const exports = load(file);
if (typeof exports === 'function') {
await exports(object);
}
}
}
function installPlugin(configs, env, agent, baseDir, framework) {
const tree = {};
const file = agent ? 'agent.js' : 'app.js';
const configKeys = Object.keys(configs);
for (const plugin in configs) {
const config = configs[plugin];
if (config.enable === undefined) config.enable = true;
if (!config.env) config.env = [];
if (!Array.isArray(config.env)) config.env = [config.env];
if (!config.agent) config.agent = [];
if (!Array.isArray(config.agent)) config.agent = [config.agent];
if (!config.enable) continue;
if (env && config.env.length && config.env.indexOf(env) === -1) continue;
if (agent) {
if (!config.agent.length) continue;
if (config.agent.length && config.agent.indexOf(agent) === -1) continue;
}
const pluginPackageName = config.package;
const pluginPathName = config.path;
if (!pluginPackageName && !pluginPathName) {
throw new Error(`plugin of ${plugin} miss 'package' or 'path'`);
}
let pkgPath, modal, exportsPath;
if (pluginPathName) {
pkgPath = path.resolve(baseDir, pluginPathName, 'package.json');
if (!fs.existsSync(pkgPath)) {
throw new Error(`plugin of ${plugin} miss 'package.json' in ${pkgPath}`);
}
modal = load(pkgPath);
exportsPath = path.resolve(baseDir, pluginPathName, file);
} else {
const dir = assertAndReturn(pluginPackageName, path.resolve(baseDir, 'node_modules'));
modal = load(dir + '/package.json');
exportsPath = path.resolve(dir, file);
}
if (!modal.plugin) {
throw new Error(`plugin of ${plugin}'s package.json miss 'plugin' property in ${pkgPath}`);
}
if (modal.plugin.name !== plugin) {
throw new Error(`plugin of ${plugin}'s package.json which name is not matched in ${pkgPath}`);
}
if (!modal.plugin.framework) modal.plugin.framework = [];
if (!Array.isArray(modal.plugin.framework)) modal.plugin.framework = [modal.plugin.framework];
modal.plugin.framework = modal.plugin.framework.map(
fw => fw.indexOf('ys-fw-') === -1
? 'ys-fw-' + fw
: fw
);
if (modal.plugin.framework.length) {
const index = modal.plugin.framework.indexOf(framework);
if (index === -1) continue;
}
const exportsFn = fs.existsSync(exportsPath) ? load(exportsPath) : function noop() {};
tree[plugin] = {
dependencies: modal.plugin.dependencies || [],
exports: exportsFn,
dir: path.dirname(exportsPath)
};
if (config.dependencies) {
if (!Array.isArray(config.dependencies)) {
config.dependencies = [config.dependencies];
}
} else {
config.dependencies = [];
}
tree[plugin].dependencies = tree[plugin].dependencies.concat(config.dependencies);
}
return sortDependencies(tree, configKeys);
}
function sortDependencies(tree, configKeys) {
const s = Object.keys(tree);
const m = [];
let j = s.length;
while (j--) {
const obj = tree[s[j]];
if (obj.dependencies.length) {
const res = intersect(obj.dependencies, configKeys);
if (res.removes.length) {
throw new Error(`模块[${s[j]}]依赖模块不存在:${res.removes.join(',')}`);
}
}
Object.defineProperty(obj, 'deep', {
get() {
if (!obj.dependencies.length) return 0;
return Math.max(...obj.dependencies.map(d => tree[d] ? tree[d].deep : 0)) + 1;
}
});
}
for (const i in tree) {
tree[i].name = i;
m.push(tree[i]);
}
return m.sort((a, b) => a.deep - b.deep);
}
function mergeConfigs(array) {
return Object.assign({}, ...array.map(arr => {
if (!fs.existsSync(arr)) {
return {};
}
return load(arr);
}));
}