ee-core 2.11.1 → 4.0.0-beta.1
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.
- package/app/application.js +31 -0
- package/app/boot.js +81 -0
- package/app/dir.js +27 -0
- package/app/events.js +56 -0
- package/app/index.js +7 -0
- package/config/config_loader.js +66 -0
- package/config/default_config.js +110 -0
- package/config/index.js +21 -83
- package/const/channel.js +26 -16
- package/const/index.js +0 -4
- package/controller/controller_loader.js +78 -0
- package/controller/index.js +18 -19
- package/core/index.js +6 -5
- package/core/{lib/loader → loader}/file_loader.js +39 -156
- package/core/utils/index.js +83 -0
- package/core/{lib/utils → utils}/timing.js +3 -2
- package/cross/cross.js +152 -0
- package/cross/index.js +6 -181
- package/cross/spawnProcess.js +34 -51
- package/electron/app/index.js +38 -54
- package/electron/index.js +13 -17
- package/electron/window/index.js +260 -65
- package/exception/index.js +34 -32
- package/html/index.js +7 -10
- package/index.js +4 -52
- package/jobs/child/app.js +9 -9
- package/jobs/child/forkProcess.js +29 -45
- package/jobs/child/index.js +13 -10
- package/jobs/child-pool/index.js +13 -11
- package/jobs/index.js +2 -4
- package/loader/index.js +91 -144
- package/log/index.js +74 -57
- package/log/logger.js +76 -84
- package/message/childMessage.js +12 -13
- package/message/index.js +7 -16
- package/package.json +2 -2
- package/ps/index.js +137 -223
- package/socket/httpServer.js +46 -43
- package/socket/index.js +52 -69
- package/socket/ipcServer.js +80 -94
- package/socket/socketServer.js +31 -24
- package/storage/index.js +5 -37
- package/storage/sqliteStorage.js +18 -18
- package/utils/extend.js +10 -5
- package/utils/helper.js +38 -42
- package/utils/index.js +40 -23
- package/utils/ip.js +5 -45
- package/utils/is.js +107 -141
- package/utils/json.js +15 -7
- package/utils/pargv.js +5 -1
- package/utils/{get-port → port}/index.js +4 -26
- package/utils/wrap.js +8 -3
- package/addon/index.js +0 -35
- package/addon/window/index.js +0 -99
- package/bin/tools.js +0 -8
- package/config/cache.js +0 -42
- package/config/config.default.js +0 -331
- package/controller/baseContextClass.js +0 -25
- package/core/lib/ee.js +0 -216
- package/core/lib/loader/context_loader.js +0 -106
- package/core/lib/loader/ee_loader.js +0 -435
- package/core/lib/loader/mixin/addon.js +0 -32
- package/core/lib/loader/mixin/config.js +0 -130
- package/core/lib/loader/mixin/controller.js +0 -125
- package/core/lib/loader/mixin/service.js +0 -28
- package/core/lib/utils/base_context_class.js +0 -34
- package/core/lib/utils/function.js +0 -30
- package/core/lib/utils/index.js +0 -133
- package/core/lib/utils/sequencify.js +0 -59
- package/ee/appLoader.js +0 -48
- package/ee/application.js +0 -100
- package/ee/baseApp.js +0 -104
- package/ee/eeApp.js +0 -409
- package/ee/index.js +0 -58
- package/electron/window/winState.js +0 -186
- package/httpclient/index.js +0 -161
- package/jobs/baseJobClass.js +0 -16
- package/jobs/renderer/index.js +0 -141
- package/jobs/renderer/loadView.js +0 -41
- package/jobs/unification.js +0 -64
- package/main/index.js +0 -57
- package/old-utils/index.js +0 -91
- package/services/baseContextClass.js +0 -24
- package/services/index.js +0 -41
- package/socket/io.js +0 -28
- package/storage/jsondb/adapters/Base.js +0 -23
- package/storage/jsondb/adapters/FileSync.js +0 -64
- package/storage/jsondb/main.js +0 -55
- package/storage/jsondbStorage.js +0 -196
- package/utils/co.js +0 -237
- package/utils/copyto.js +0 -161
- package/utils/depd/index.js +0 -538
- package/utils/depd/lib/browser/index.js +0 -77
- package/utils/get-port/index.d.ts +0 -64
- package/utils/time/index.js +0 -20
- package/utils/time/ms.js +0 -162
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const assert = require('assert');
|
|
6
|
-
const is = require('is-type-of');
|
|
7
|
-
const debug = require('debug')('ee-core:EeLoader');
|
|
8
|
-
const FileLoader = require('./file_loader');
|
|
9
|
-
const ContextLoader = require('./context_loader');
|
|
10
|
-
const Utils = require('../utils');
|
|
11
|
-
const Timing = require('../utils/timing');
|
|
12
|
-
const Ps = require('../../../ps');
|
|
13
|
-
|
|
14
|
-
const REQUIRE_COUNT = Symbol('EeLoader#requireCount');
|
|
15
|
-
|
|
16
|
-
class EeLoader {
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @class
|
|
20
|
-
* @param {Object} options - options
|
|
21
|
-
* @param {String} options.baseDir - the directory of application
|
|
22
|
-
* @param {EeCore} options.app - Application instance
|
|
23
|
-
* @param {Logger} options.logger - logger
|
|
24
|
-
* @since 1.0.0
|
|
25
|
-
*/
|
|
26
|
-
constructor(options) {
|
|
27
|
-
this.options = options;
|
|
28
|
-
// assert(fs.existsSync(this.options.baseDir), `${this.options.baseDir} not exists`);
|
|
29
|
-
assert(this.options.app, 'options.app is required');
|
|
30
|
-
assert(this.options.logger, 'options.logger is required');
|
|
31
|
-
|
|
32
|
-
this.app = this.options.app;
|
|
33
|
-
this.timing = this.app.timing || new Timing();
|
|
34
|
-
this[REQUIRE_COUNT] = 0;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @member {Object} EeLoader#pkg
|
|
38
|
-
* @see {@link AppInfo#pkg}
|
|
39
|
-
* @since 1.0.0
|
|
40
|
-
*/
|
|
41
|
-
this.pkg = this.getPkg();
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Framework directories
|
|
45
|
-
*
|
|
46
|
-
* @member {Array} EeLoader#EePaths
|
|
47
|
-
* @see EeLoader#getEePaths
|
|
48
|
-
* @since 1.0.0
|
|
49
|
-
*/
|
|
50
|
-
this.EePaths = this.getEePaths();
|
|
51
|
-
debug('Loaded EePaths %j', this.EePaths);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @member {String} EeLoader#serverEnv
|
|
55
|
-
* @see AppInfo#env
|
|
56
|
-
* @since 1.0.0
|
|
57
|
-
*/
|
|
58
|
-
this.serverEnv = this.getServerEnv();
|
|
59
|
-
debug('Loaded serverEnv %j', this.serverEnv);
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @member {AppInfo} EeLoader#appInfo
|
|
63
|
-
* @since 1.0.0
|
|
64
|
-
*/
|
|
65
|
-
this.appInfo = this.getAppInfo();
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @member {String} EeLoader#serverScope
|
|
69
|
-
* @see AppInfo#serverScope
|
|
70
|
-
*/
|
|
71
|
-
this.serverScope = options.serverScope !== undefined
|
|
72
|
-
? options.serverScope
|
|
73
|
-
: this.getServerScope();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Get {@link AppInfo#env}
|
|
78
|
-
* @return {String} env
|
|
79
|
-
* @see AppInfo#env
|
|
80
|
-
* @private
|
|
81
|
-
* @since 1.0.0
|
|
82
|
-
*/
|
|
83
|
-
getServerEnv() {
|
|
84
|
-
let serverEnv = this.options.env;
|
|
85
|
-
|
|
86
|
-
if (!serverEnv) {
|
|
87
|
-
throw new Error('[core] [lib] [loader] getServerEnv serverEnv can not be empty!');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return serverEnv;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Get {@link AppInfo#scope}
|
|
95
|
-
* @return {String} serverScope
|
|
96
|
-
* @private
|
|
97
|
-
*/
|
|
98
|
-
getServerScope() {
|
|
99
|
-
return process.env.EE_SERVER_SCOPE || '';
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Get {@link AppInfo#name}
|
|
104
|
-
* @return {String} appname
|
|
105
|
-
* @private
|
|
106
|
-
* @since 1.0.0
|
|
107
|
-
*/
|
|
108
|
-
getAppname() {
|
|
109
|
-
if (this.pkg.name) {
|
|
110
|
-
debug('Loaded appname(%s) from package.json', this.pkg.name);
|
|
111
|
-
return this.pkg.name;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
throw new Error(`name is required from ${pkg}`);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Get home directory
|
|
119
|
-
* @return {String} home directory
|
|
120
|
-
* @since 3.4.0
|
|
121
|
-
*/
|
|
122
|
-
getHomedir() {
|
|
123
|
-
return this.options.homeDir;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Get app info
|
|
128
|
-
* @return {AppInfo} appInfo
|
|
129
|
-
* @since 1.0.0
|
|
130
|
-
*/
|
|
131
|
-
getAppInfo() {
|
|
132
|
-
const env = this.serverEnv;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Meta information of the application
|
|
136
|
-
* @class AppInfo
|
|
137
|
-
*/
|
|
138
|
-
return {
|
|
139
|
-
/**
|
|
140
|
-
* The name of the application, retrieve from the name property in `package.json`.
|
|
141
|
-
* @member {String} AppInfo#name
|
|
142
|
-
*/
|
|
143
|
-
name: this.getAppname(),
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* The current directory, where the application code is.
|
|
147
|
-
* @member {String} AppInfo#baseDir
|
|
148
|
-
*/
|
|
149
|
-
baseDir: this.options.baseDir,
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* The environment of the application, **it's not NODE_ENV**
|
|
153
|
-
*
|
|
154
|
-
* 1. from `$baseDir/config/env`
|
|
155
|
-
* 2. from EE_SERVER_ENV
|
|
156
|
-
* 3. from NODE_ENV
|
|
157
|
-
*
|
|
158
|
-
* env | description
|
|
159
|
-
* --- | ---
|
|
160
|
-
* test | system integration testing
|
|
161
|
-
* prod | production
|
|
162
|
-
* local | local on your own computer
|
|
163
|
-
* unittest | unit test
|
|
164
|
-
*
|
|
165
|
-
* @member {String} AppInfo#env
|
|
166
|
-
* @see https://Eejs.org/zh-cn/basics/env.html
|
|
167
|
-
*/
|
|
168
|
-
env: env,
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* @member {String} AppInfo#scope
|
|
172
|
-
*/
|
|
173
|
-
scope: this.serverScope,
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* The use directory, same as `process.env.HOME`
|
|
177
|
-
* @member {String} AppInfo#HOME
|
|
178
|
-
*/
|
|
179
|
-
home: this.getHomedir(),
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* The directory whether is homeDir or appUserData depend on env.
|
|
183
|
-
* @member {String} AppInfo#root
|
|
184
|
-
*/
|
|
185
|
-
root: Ps.getRootDir(),
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* electron application data dir
|
|
189
|
-
* @member {String} AppInfo#appUserDataDir
|
|
190
|
-
*/
|
|
191
|
-
appUserDataDir: this.options.appUserData,
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* system user home dir
|
|
195
|
-
* @member {String} AppInfo#userHome
|
|
196
|
-
*/
|
|
197
|
-
userHome: this.options.userHome,
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* application version
|
|
201
|
-
* @member {String} AppInfo#appVersion
|
|
202
|
-
*/
|
|
203
|
-
appVersion: this.options.appVersion,
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* application package status
|
|
207
|
-
* @member {boolean} AppInfo#isPackaged
|
|
208
|
-
*/
|
|
209
|
-
isPackaged: this.options.isPackaged,
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* application exec file dir
|
|
213
|
-
* @member {String} AppInfo#execDir
|
|
214
|
-
*/
|
|
215
|
-
execDir: this.options.execDir
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Get {@link EeLoader#EePaths}
|
|
221
|
-
* @return {Array} framework directories
|
|
222
|
-
* @see {@link EeLoader#EePaths}
|
|
223
|
-
* @private
|
|
224
|
-
* @since 1.0.0
|
|
225
|
-
*/
|
|
226
|
-
getEePaths() {
|
|
227
|
-
// avoid require recursively
|
|
228
|
-
const EePaths = [];
|
|
229
|
-
EePaths.push(this.app[Symbol.for('ee#eePath')]);
|
|
230
|
-
|
|
231
|
-
return EePaths;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Low Level API
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Load single file, will invoke when export is function
|
|
238
|
-
*
|
|
239
|
-
* @param {String} filepath - fullpath
|
|
240
|
-
* @param {Array} inject - pass rest arguments into the function when invoke
|
|
241
|
-
* @return {Object} exports
|
|
242
|
-
* @example
|
|
243
|
-
* ```js
|
|
244
|
-
* app.loader.loadFile(path.join(app.options.baseDir, 'config/router.js'));
|
|
245
|
-
* ```
|
|
246
|
-
* @since 1.0.0
|
|
247
|
-
*/
|
|
248
|
-
loadFile(filepath, ...inject) {
|
|
249
|
-
filepath = filepath && this.resolveModule(filepath);
|
|
250
|
-
if (!filepath) {
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// function(arg1, args, ...) {}
|
|
255
|
-
if (inject.length === 0) inject = [ this.app ];
|
|
256
|
-
|
|
257
|
-
let ret = this.requireFile(filepath);
|
|
258
|
-
if (is.function(ret) && !is.class(ret) && !Utils.isBytecodeClass(ret)) {
|
|
259
|
-
ret = ret(...inject);
|
|
260
|
-
}
|
|
261
|
-
return ret;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* @param {String} filepath - fullpath
|
|
266
|
-
* @return {Object} exports
|
|
267
|
-
* @private
|
|
268
|
-
*/
|
|
269
|
-
requireFile(filepath) {
|
|
270
|
-
const timingKey = `Require(${this[REQUIRE_COUNT]++}) ${Utils.getResolvedFilename(filepath, this.options.baseDir)}`;
|
|
271
|
-
this.timing.start(timingKey);
|
|
272
|
-
const ret = Utils.loadFile(filepath);
|
|
273
|
-
this.timing.end(timingKey);
|
|
274
|
-
return ret;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Get all loadUnit
|
|
279
|
-
*
|
|
280
|
-
* loadUnit is a directory that can be loaded by EeLoader, it has the same structure.
|
|
281
|
-
* loadUnit has a path and a type(app, framework, plugin).
|
|
282
|
-
*
|
|
283
|
-
* The order of the loadUnits:
|
|
284
|
-
*
|
|
285
|
-
* 1. plugin
|
|
286
|
-
* 2. framework
|
|
287
|
-
* 3. app
|
|
288
|
-
*
|
|
289
|
-
* @return {Array} loadUnits
|
|
290
|
-
* @since 1.0.0
|
|
291
|
-
*/
|
|
292
|
-
getLoadUnits() {
|
|
293
|
-
if (this.dirs) {
|
|
294
|
-
return this.dirs;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
const dirs = this.dirs = [];
|
|
298
|
-
|
|
299
|
-
// framework or Ee path
|
|
300
|
-
for (const EePath of this.EePaths) {
|
|
301
|
-
dirs.push({
|
|
302
|
-
path: EePath,
|
|
303
|
-
type: 'framework',
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// application
|
|
308
|
-
dirs.push({
|
|
309
|
-
path: this.options.baseDir,
|
|
310
|
-
type: 'app',
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
debug('Loaded dirs %j', dirs);
|
|
314
|
-
return dirs;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Load files using {@link FileLoader}, inject to {@link Application}
|
|
319
|
-
* @param {String|Array} directory - see {@link FileLoader}
|
|
320
|
-
* @param {String} property - see {@link FileLoader}
|
|
321
|
-
* @param {Object} opt - see {@link FileLoader}
|
|
322
|
-
* @since 1.0.0
|
|
323
|
-
*/
|
|
324
|
-
loadToApp(directory, property, opt) {
|
|
325
|
-
const target = this.app[property] = {};
|
|
326
|
-
opt = Object.assign({}, {
|
|
327
|
-
directory,
|
|
328
|
-
target,
|
|
329
|
-
inject: this.app,
|
|
330
|
-
}, opt);
|
|
331
|
-
|
|
332
|
-
const timingKey = `Load "${String(property)}" to Application`;
|
|
333
|
-
this.timing.start(timingKey);
|
|
334
|
-
new FileLoader(opt).load();
|
|
335
|
-
this.timing.end(timingKey);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Load files using {@link ContextLoader}
|
|
340
|
-
* @param {String|Array} directory - see {@link ContextLoader}
|
|
341
|
-
* @param {String} property - see {@link ContextLoader}
|
|
342
|
-
* @param {Object} opt - see {@link ContextLoader}
|
|
343
|
-
* @since 1.0.0
|
|
344
|
-
*/
|
|
345
|
-
loadToContext(directory, property, opt) {
|
|
346
|
-
opt = Object.assign({}, {
|
|
347
|
-
directory,
|
|
348
|
-
property,
|
|
349
|
-
inject: this.app,
|
|
350
|
-
loader: this
|
|
351
|
-
}, opt);
|
|
352
|
-
const timingKey = `Load "${String(property)}" to Context`;
|
|
353
|
-
this.timing.start(timingKey);
|
|
354
|
-
if (['addon'].includes(property)) {
|
|
355
|
-
new ContextLoader(opt).loadAddons();
|
|
356
|
-
} else {
|
|
357
|
-
new ContextLoader(opt).load();
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
this.timing.end(timingKey);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* @member {FileLoader} EeLoader#FileLoader
|
|
365
|
-
* @since 1.0.0
|
|
366
|
-
*/
|
|
367
|
-
get FileLoader() {
|
|
368
|
-
return FileLoader;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* @member {ContextLoader} EeLoader#ContextLoader
|
|
373
|
-
* @since 1.0.0
|
|
374
|
-
*/
|
|
375
|
-
get ContextLoader() {
|
|
376
|
-
return ContextLoader;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
getTypeFiles(filename) {
|
|
380
|
-
const files = [ `${filename}.default` ];
|
|
381
|
-
files.push(`${filename}.${this.serverEnv}`);
|
|
382
|
-
|
|
383
|
-
return files;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
resolveModule(filepath) {
|
|
387
|
-
let fullpath;
|
|
388
|
-
try {
|
|
389
|
-
fullpath = require.resolve(filepath);
|
|
390
|
-
} catch (e) {
|
|
391
|
-
|
|
392
|
-
// 特殊后缀处理
|
|
393
|
-
if (filepath && (filepath.endsWith('.defalut') || filepath.endsWith('.prod'))) {
|
|
394
|
-
fullpath = filepath + '.jsc';
|
|
395
|
-
} else if (filepath && filepath.endsWith('.js')) {
|
|
396
|
-
fullpath = filepath + 'c';
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
|
|
400
|
-
//this.options.logger.warn(`[ee-core] [core/lib/loader/ee_loader] resolveModule unknow filepath: ${filepath}`)
|
|
401
|
-
return undefined;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
return fullpath;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
getPkg() {
|
|
409
|
-
const filePath = path.join(this.options.homeDir, 'package.json');
|
|
410
|
-
if (!fs.existsSync(filePath)) {
|
|
411
|
-
throw new Error(filePath + ' is not found');
|
|
412
|
-
}
|
|
413
|
-
const json = JSON.parse(fs.readFileSync(filePath));
|
|
414
|
-
|
|
415
|
-
return json;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Mixin methods to EeLoader
|
|
421
|
-
* // ES6 Multiple Inheritance
|
|
422
|
-
* https://medium.com/@leocavalcante/es6-multiple-inheritance-73a3c66d2b6b
|
|
423
|
-
*/
|
|
424
|
-
const loaders = [
|
|
425
|
-
require('./mixin/config'),
|
|
426
|
-
require('./mixin/service'),
|
|
427
|
-
require('./mixin/controller'),
|
|
428
|
-
require('./mixin/addon'),
|
|
429
|
-
];
|
|
430
|
-
|
|
431
|
-
for (const loader of loaders) {
|
|
432
|
-
Object.assign(EeLoader.prototype, loader);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
module.exports = EeLoader;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Load app/addon
|
|
7
|
-
* @param {Object} opt - LoaderOptions
|
|
8
|
-
* @function
|
|
9
|
-
* @since 1.0.0
|
|
10
|
-
*/
|
|
11
|
-
loadAddon(opt) {
|
|
12
|
-
this.timing.start('Load Addon');
|
|
13
|
-
|
|
14
|
-
// 加载ee-core的插件 和 用户插件
|
|
15
|
-
const directorys = [
|
|
16
|
-
path.join(this.options.framework, 'addon'),
|
|
17
|
-
path.join(this.options.baseDir, 'addon'),
|
|
18
|
-
]
|
|
19
|
-
opt = Object.assign({
|
|
20
|
-
call: true,
|
|
21
|
-
caseStyle: 'lower',
|
|
22
|
-
directory: directorys
|
|
23
|
-
}, opt);
|
|
24
|
-
|
|
25
|
-
const addonPaths = opt.directory;
|
|
26
|
-
this.loadToContext(addonPaths, 'addon', opt);
|
|
27
|
-
|
|
28
|
-
this.timing.end('Load Addon');
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')('ee-core:config');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const extend = require('../../../../utils/extend');
|
|
6
|
-
const assert = require('assert');
|
|
7
|
-
const { Console } = require('console');
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Load config/config.js
|
|
13
|
-
*
|
|
14
|
-
* Will merge config.default.js 和 config.${env}.js
|
|
15
|
-
*
|
|
16
|
-
* @function EggLoader#loadConfig
|
|
17
|
-
* @since 1.0.0
|
|
18
|
-
*/
|
|
19
|
-
loadConfig() {
|
|
20
|
-
this.timing.start('Load Config');
|
|
21
|
-
this.configMeta = {};
|
|
22
|
-
|
|
23
|
-
const target = {};
|
|
24
|
-
|
|
25
|
-
// Load Application config first
|
|
26
|
-
const appConfig = this._preloadAppConfig();
|
|
27
|
-
|
|
28
|
-
for (const filename of this.getTypeFiles('config')) {
|
|
29
|
-
for (const unit of this.getLoadUnits()) {
|
|
30
|
-
const isApp = unit.type === 'app';
|
|
31
|
-
const config = this._loadConfig(unit.path, filename, isApp ? undefined : appConfig, unit.type);
|
|
32
|
-
|
|
33
|
-
if (!config) {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
debug('Loaded config %s, %s, %j', unit.path, filename, config);
|
|
38
|
-
extend(true, target, config);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// load env from process.env.EE_APP_CONFIG
|
|
43
|
-
const envConfig = this._loadConfigFromEnv();
|
|
44
|
-
debug('Loaded config from private env, %j', envConfig);
|
|
45
|
-
extend(true, target, envConfig);
|
|
46
|
-
|
|
47
|
-
// You can manipulate the order of app.config.coreMiddleware and app.config.appMiddleware in app.js
|
|
48
|
-
target.coreMiddleware = target.coreMiddlewares = target.coreMiddleware || [];
|
|
49
|
-
target.appMiddleware = target.appMiddlewares = target.middleware || [];
|
|
50
|
-
|
|
51
|
-
this.config = target;
|
|
52
|
-
this.timing.end('Load Config');
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
_preloadAppConfig() {
|
|
56
|
-
const names = [
|
|
57
|
-
'config.default',
|
|
58
|
-
`config.${this.serverEnv}`,
|
|
59
|
-
];
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const target = {};
|
|
63
|
-
for (const filename of names) {
|
|
64
|
-
const config = this._loadConfig(this.options.baseDir, filename, undefined, 'app');
|
|
65
|
-
extend(true, target, config);
|
|
66
|
-
}
|
|
67
|
-
return target;
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
_loadConfig(dirpath, filename, extraInject, type) {
|
|
71
|
-
const isPlugin = type === 'plugin';
|
|
72
|
-
const isApp = type === 'app';
|
|
73
|
-
let filepath = this.resolveModule(path.join(dirpath, 'config', filename));
|
|
74
|
-
|
|
75
|
-
// let config.js compatible
|
|
76
|
-
// if (filename === 'config.default' && !filepath) {
|
|
77
|
-
// filepath = this.resolveModule(path.join(dirpath, 'config/config'));
|
|
78
|
-
// }
|
|
79
|
-
|
|
80
|
-
const config = this.loadFile(filepath, this.appInfo, extraInject);
|
|
81
|
-
|
|
82
|
-
if (!config) return null;
|
|
83
|
-
|
|
84
|
-
if (isPlugin || isApp) {
|
|
85
|
-
assert(!config.coreMiddleware, 'Can not define coreMiddleware in app or plugin');
|
|
86
|
-
}
|
|
87
|
-
if (!isApp) {
|
|
88
|
-
assert(!config.middleware, 'Can not define middleware in ' + filepath);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// store config meta, check where is the property of config come from.
|
|
92
|
-
this._setConfigMeta(config, filepath);
|
|
93
|
-
|
|
94
|
-
return config;
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
_loadConfigFromEnv() {
|
|
98
|
-
const envConfigStr = process.env.EE_APP_CONFIG;
|
|
99
|
-
if (!envConfigStr) return;
|
|
100
|
-
try {
|
|
101
|
-
const envConfig = JSON.parse(envConfigStr);
|
|
102
|
-
this._setConfigMeta(envConfig, '<process.env.EE_APP_CONFIG>');
|
|
103
|
-
return envConfig;
|
|
104
|
-
} catch (err) {
|
|
105
|
-
this.options.logger.warn('[ee-core] [core/.../config] process.env.EE_APP_CONFIG is not invalid JSON: %s', envConfigStr);
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
_setConfigMeta(config, filepath) {
|
|
110
|
-
config = extend(true, {}, config);
|
|
111
|
-
setConfig(config, filepath);
|
|
112
|
-
extend(true, this.configMeta, config);
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
function setConfig(obj, filepath) {
|
|
117
|
-
for (const key of Object.keys(obj)) {
|
|
118
|
-
const val = obj[key];
|
|
119
|
-
// ignore console
|
|
120
|
-
if (key === 'console' && val && typeof val.Console === 'function' && val.Console === Console) {
|
|
121
|
-
obj[key] = filepath;
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
if (val && Object.getPrototypeOf(val) === Object.prototype && Object.keys(val).length > 0) {
|
|
125
|
-
setConfig(val, filepath);
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
obj[key] = filepath;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const is = require('is-type-of');
|
|
5
|
-
const UtilsFn = require('../../utils/function');
|
|
6
|
-
const Utils = require('../../utils');
|
|
7
|
-
const FULLPATH = require('../file_loader').FULLPATH;
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Load app/controller
|
|
13
|
-
* @param {Object} opt - LoaderOptions
|
|
14
|
-
* @since 1.0.0
|
|
15
|
-
*/
|
|
16
|
-
loadController(opt) {
|
|
17
|
-
this.timing.start('Load Controller');
|
|
18
|
-
opt = Object.assign({
|
|
19
|
-
caseStyle: 'lower',
|
|
20
|
-
directory: path.join(this.options.baseDir, 'controller'),
|
|
21
|
-
initializer: (obj, opt) => {
|
|
22
|
-
// return class if it exports a function
|
|
23
|
-
// ```js
|
|
24
|
-
// module.exports = app => {
|
|
25
|
-
// return class HomeController extends app.Controller {};
|
|
26
|
-
// }
|
|
27
|
-
// ```
|
|
28
|
-
|
|
29
|
-
if (is.function(obj) && !is.generatorFunction(obj) && !is.class(obj) && !is.asyncFunction(obj) && !Utils.isBytecodeClass(obj)) {
|
|
30
|
-
obj = obj(this.app);
|
|
31
|
-
}
|
|
32
|
-
if (is.class(obj) || Utils.isBytecodeClass(obj)) {
|
|
33
|
-
obj.prototype.pathName = opt.pathName;
|
|
34
|
-
obj.prototype.fullPath = opt.path;
|
|
35
|
-
return wrapClass(obj);
|
|
36
|
-
}
|
|
37
|
-
if (is.object(obj)) {
|
|
38
|
-
return wrapObject(obj, opt.path);
|
|
39
|
-
}
|
|
40
|
-
// support generatorFunction for forward compatbility
|
|
41
|
-
if (is.generatorFunction(obj) || is.asyncFunction(obj)) {
|
|
42
|
-
return wrapObject({ 'module.exports': obj }, opt.path)['module.exports'];
|
|
43
|
-
}
|
|
44
|
-
return obj;
|
|
45
|
-
},
|
|
46
|
-
}, opt);
|
|
47
|
-
const controllerBase = opt.directory;
|
|
48
|
-
|
|
49
|
-
this.loadToApp(controllerBase, 'controller', opt);
|
|
50
|
-
//this.options.logger.info('[ee-core] [core/.../controller] loaded: %s', controllerBase);
|
|
51
|
-
this.timing.end('Load Controller');
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
// wrap the class, yield a object with middlewares
|
|
57
|
-
function wrapClass(Controller) {
|
|
58
|
-
let proto = Controller.prototype;
|
|
59
|
-
const ret = {};
|
|
60
|
-
// tracing the prototype chain
|
|
61
|
-
while (proto !== Object.prototype) {
|
|
62
|
-
const keys = Object.getOwnPropertyNames(proto);
|
|
63
|
-
for (const key of keys) {
|
|
64
|
-
// getOwnPropertyNames will return constructor
|
|
65
|
-
// that should be ignored
|
|
66
|
-
if (key === 'constructor') {
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
// skip getter, setter & non-function properties
|
|
70
|
-
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
71
|
-
// prevent to override sub method
|
|
72
|
-
if (is.function(d.value) && !ret.hasOwnProperty(key)) {
|
|
73
|
-
ret[key] = methodToMiddleware(Controller, key);
|
|
74
|
-
ret[key][FULLPATH] = Controller.prototype.fullPath + '#' + Controller.name + '.' + key + '()';
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
proto = Object.getPrototypeOf(proto);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return ret;
|
|
81
|
-
|
|
82
|
-
function methodToMiddleware(Controller, key) {
|
|
83
|
-
return function classControllerMiddleware(...args) {
|
|
84
|
-
const controller = new Controller(this);
|
|
85
|
-
// if (!this.app.config.controller || !this.app.config.controller.supportParams) {
|
|
86
|
-
// args = [ this ];
|
|
87
|
-
// }
|
|
88
|
-
//args = [ this ];
|
|
89
|
-
return Utils.callFn(controller[key], args, controller);
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// wrap the method of the object, method can receive ctx as it's first argument
|
|
95
|
-
function wrapObject(obj, path, prefix) {
|
|
96
|
-
const keys = Object.keys(obj);
|
|
97
|
-
const ret = {};
|
|
98
|
-
for (const key of keys) {
|
|
99
|
-
if (is.function(obj[key])) {
|
|
100
|
-
const names = UtilsFn.getParamNames(obj[key]);
|
|
101
|
-
if (names[0] === 'next') {
|
|
102
|
-
throw new Error(`controller \`${prefix || ''}${key}\` should not use next as argument from file ${path}`);
|
|
103
|
-
}
|
|
104
|
-
ret[key] = functionToMiddleware(obj[key]);
|
|
105
|
-
ret[key][FULLPATH] = `${path}#${prefix || ''}${key}()`;
|
|
106
|
-
} else if (is.object(obj[key])) {
|
|
107
|
-
ret[key] = wrapObject(obj[key], path, `${prefix || ''}${key}.`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return ret;
|
|
111
|
-
|
|
112
|
-
function functionToMiddleware(func) {
|
|
113
|
-
const objectControllerMiddleware = async function(...args) {
|
|
114
|
-
// if (!this.app.config.controller || !this.app.config.controller.supportParams) {
|
|
115
|
-
// args = [ this ];
|
|
116
|
-
// }
|
|
117
|
-
return await Utils.callFn(func, args, this);
|
|
118
|
-
};
|
|
119
|
-
for (const key in func) {
|
|
120
|
-
objectControllerMiddleware[key] = func[key];
|
|
121
|
-
}
|
|
122
|
-
return objectControllerMiddleware;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|