ee-core 1.2.8-beta.4 → 1.2.9-bate.2

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.
Files changed (44) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/bin/tools.js +22 -22
  4. package/config/config.default.js +255 -250
  5. package/core/index.js +12 -12
  6. package/core/lib/ee.js +209 -209
  7. package/core/lib/loader/context_loader.js +105 -105
  8. package/core/lib/loader/ee_loader.js +451 -451
  9. package/core/lib/loader/file_loader.js +262 -262
  10. package/core/lib/loader/mixin/config.js +138 -138
  11. package/core/lib/loader/mixin/controller.js +123 -123
  12. package/core/lib/loader/mixin/service.js +29 -29
  13. package/core/lib/utils/base_context_class.js +34 -34
  14. package/core/lib/utils/index.js +100 -100
  15. package/core/lib/utils/sequencify.js +59 -59
  16. package/core/lib/utils/timing.js +77 -77
  17. package/index.js +49 -49
  18. package/lib/appLoader.js +45 -45
  19. package/lib/application.js +79 -80
  20. package/lib/baseApp.js +118 -118
  21. package/lib/constant.js +28 -28
  22. package/lib/eeApp.js +326 -326
  23. package/lib/helper.js +51 -51
  24. package/lib/httpclient.js +136 -136
  25. package/lib/logger.js +46 -46
  26. package/lib/socket/httpServer.js +134 -102
  27. package/lib/socket/io.js +23 -23
  28. package/lib/socket/ipcServer.js +128 -128
  29. package/lib/socket/socketClient.js +50 -50
  30. package/lib/socket/socketServer.js +76 -76
  31. package/lib/socket/start.js +22 -22
  32. package/lib/storage/index.js +33 -33
  33. package/lib/storage/lowdbStorage.js +98 -98
  34. package/lib/storage/sqliteStorage.js +58 -58
  35. package/package.json +45 -45
  36. package/tools/codeCompress.js +204 -204
  37. package/tools/replaceDist.js +76 -76
  38. package/utils/common.js +90 -90
  39. package/utils/index.js +207 -207
  40. package/utils/wrap.js +37 -37
  41. package/resource/images/loding.gif +0 -0
  42. package/resource/images/tray_logo.png +0 -0
  43. package/resource/loading.html +0 -22
  44. package/resource/view_example.html +0 -22
@@ -1,451 +1,451 @@
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 utility = require('utility');
11
- const utils = require('../utils');
12
- const Timing = require('../utils/timing');
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
- const envPath = path.join(this.options.baseDir, 'config/env');
87
- if (!serverEnv && fs.existsSync(envPath)) {
88
- serverEnv = fs.readFileSync(envPath, 'utf8').trim();
89
- }
90
-
91
- if (!serverEnv) {
92
- serverEnv = process.env.EE_SERVER_ENV;
93
- }
94
-
95
- if (!serverEnv) {
96
- if (process.env.NODE_ENV === 'test') {
97
- serverEnv = 'unittest';
98
- } else if (process.env.NODE_ENV === 'production') {
99
- serverEnv = 'prod';
100
- } else {
101
- serverEnv = 'local';
102
- }
103
- } else {
104
- serverEnv = serverEnv.trim();
105
- }
106
-
107
- return serverEnv;
108
- }
109
-
110
- /**
111
- * Get {@link AppInfo#scope}
112
- * @return {String} serverScope
113
- * @private
114
- */
115
- getServerScope() {
116
- return process.env.EE_SERVER_SCOPE || '';
117
- }
118
-
119
- /**
120
- * Get {@link AppInfo#name}
121
- * @return {String} appname
122
- * @private
123
- * @since 1.0.0
124
- */
125
- getAppname() {
126
- if (this.pkg.name) {
127
- debug('Loaded appname(%s) from package.json', this.pkg.name);
128
- return this.pkg.name;
129
- }
130
-
131
- throw new Error(`name is required from ${pkg}`);
132
- }
133
-
134
- /**
135
- * Get home directory
136
- * @return {String} home directory
137
- * @since 3.4.0
138
- */
139
- getHomedir() {
140
- return this.options.homeDir;
141
- }
142
-
143
- /**
144
- * Get app info
145
- * @return {AppInfo} appInfo
146
- * @since 1.0.0
147
- */
148
- getAppInfo() {
149
- const env = this.serverEnv;
150
-
151
- /**
152
- * Meta information of the application
153
- * @class AppInfo
154
- */
155
- return {
156
- /**
157
- * The name of the application, retrieve from the name property in `package.json`.
158
- * @member {String} AppInfo#name
159
- */
160
- name: this.getAppname(),
161
-
162
- /**
163
- * The current directory, where the application code is.
164
- * @member {String} AppInfo#baseDir
165
- */
166
- baseDir: this.options.baseDir,
167
-
168
- /**
169
- * The environment of the application, **it's not NODE_ENV**
170
- *
171
- * 1. from `$baseDir/config/env`
172
- * 2. from EE_SERVER_ENV
173
- * 3. from NODE_ENV
174
- *
175
- * env | description
176
- * --- | ---
177
- * test | system integration testing
178
- * prod | production
179
- * local | local on your own computer
180
- * unittest | unit test
181
- *
182
- * @member {String} AppInfo#env
183
- * @see https://Eejs.org/zh-cn/basics/env.html
184
- */
185
- env: env,
186
-
187
- /**
188
- * @member {String} AppInfo#scope
189
- */
190
- scope: this.serverScope,
191
-
192
- /**
193
- * The use directory, same as `process.env.HOME`
194
- * @member {String} AppInfo#HOME
195
- */
196
- home: this.getHomedir(),
197
-
198
- /**
199
- * The directory whether is baseDir or HOME depend on env.
200
- * it's good for test when you want to write some file to HOME,
201
- * but don't want to write to the real directory,
202
- * so use root to write file to baseDir instead of HOME when unittest.
203
- * keep root directory in baseDir when local and unittest
204
- * @member {String} AppInfo#root
205
- */
206
- root: env === 'local' || env === 'unittest' ? this.getHomedir() : this.options.appUserData,
207
-
208
- /**
209
- * electron application data dir
210
- * @member {String} AppInfo#appUserDataDir
211
- */
212
- appUserDataDir: this.options.appUserData,
213
-
214
- /**
215
- * system user home dir
216
- * @member {String} AppInfo#userHome
217
- */
218
- userHome: this.options.userHome,
219
-
220
- /**
221
- * application version
222
- * @member {String} AppInfo#appVersion
223
- */
224
- appVersion: this.options.appVersion,
225
-
226
- /**
227
- * application package status
228
- * @member {boolean} AppInfo#isPackaged
229
- */
230
- isPackaged: this.options.isPackaged,
231
-
232
- /**
233
- * application exec file dir
234
- * @member {String} AppInfo#execDir
235
- */
236
- execDir: this.options.execDir
237
- };
238
- }
239
-
240
- /**
241
- * Get {@link EeLoader#EePaths}
242
- * @return {Array} framework directories
243
- * @see {@link EeLoader#EePaths}
244
- * @private
245
- * @since 1.0.0
246
- */
247
- getEePaths() {
248
- // avoid require recursively
249
- const EePaths = [];
250
- EePaths.push(this.app[Symbol.for('ee#eePath')]);
251
-
252
- return EePaths;
253
- }
254
-
255
- // Low Level API
256
-
257
- /**
258
- * Load single file, will invoke when export is function
259
- *
260
- * @param {String} filepath - fullpath
261
- * @param {Array} inject - pass rest arguments into the function when invoke
262
- * @return {Object} exports
263
- * @example
264
- * ```js
265
- * app.loader.loadFile(path.join(app.options.baseDir, 'config/router.js'));
266
- * ```
267
- * @since 1.0.0
268
- */
269
- loadFile(filepath, ...inject) {
270
- filepath = filepath && this.resolveModule(filepath);
271
- if (!filepath) {
272
- return null;
273
- }
274
-
275
- // function(arg1, args, ...) {}
276
- if (inject.length === 0) inject = [ this.app ];
277
-
278
- let ret = this.requireFile(filepath);
279
- if (is.function(ret) && !is.class(ret)) {
280
- ret = ret(...inject);
281
- }
282
- return ret;
283
- }
284
-
285
- /**
286
- * @param {String} filepath - fullpath
287
- * @return {Object} exports
288
- * @private
289
- */
290
- requireFile(filepath) {
291
- const timingKey = `Require(${this[REQUIRE_COUNT]++}) ${utils.getResolvedFilename(filepath, this.options.baseDir)}`;
292
- this.timing.start(timingKey);
293
- const ret = utils.loadFile(filepath);
294
- this.timing.end(timingKey);
295
- return ret;
296
- }
297
-
298
- /**
299
- * Get all loadUnit
300
- *
301
- * loadUnit is a directory that can be loaded by EeLoader, it has the same structure.
302
- * loadUnit has a path and a type(app, framework, plugin).
303
- *
304
- * The order of the loadUnits:
305
- *
306
- * 1. plugin
307
- * 2. framework
308
- * 3. app
309
- *
310
- * @return {Array} loadUnits
311
- * @since 1.0.0
312
- */
313
- getLoadUnits() {
314
- if (this.dirs) {
315
- return this.dirs;
316
- }
317
-
318
- const dirs = this.dirs = [];
319
-
320
- if (this.orderPlugins) {
321
- for (const plugin of this.orderPlugins) {
322
- dirs.push({
323
- path: plugin.path,
324
- type: 'plugin',
325
- });
326
- }
327
- }
328
-
329
- // framework or Ee path
330
- for (const EePath of this.EePaths) {
331
- dirs.push({
332
- path: EePath,
333
- type: 'framework',
334
- });
335
- }
336
-
337
- // application
338
- dirs.push({
339
- path: this.options.baseDir,
340
- type: 'app',
341
- });
342
-
343
- debug('Loaded dirs %j', dirs);
344
- return dirs;
345
- }
346
-
347
- /**
348
- * Load files using {@link FileLoader}, inject to {@link Application}
349
- * @param {String|Array} directory - see {@link FileLoader}
350
- * @param {String} property - see {@link FileLoader}
351
- * @param {Object} opt - see {@link FileLoader}
352
- * @since 1.0.0
353
- */
354
- loadToApp(directory, property, opt) {
355
- const target = this.app[property] = {};
356
- opt = Object.assign({}, {
357
- directory,
358
- target,
359
- inject: this.app,
360
- }, opt);
361
-
362
- const timingKey = `Load "${String(property)}" to Application`;
363
- this.timing.start(timingKey);
364
- new FileLoader(opt).load();
365
- //console.log('app property:', this.app[property]);
366
- this.timing.end(timingKey);
367
- }
368
-
369
- /**
370
- * Load files using {@link ContextLoader}
371
- * @param {String|Array} directory - see {@link ContextLoader}
372
- * @param {String} property - see {@link ContextLoader}
373
- * @param {Object} opt - see {@link ContextLoader}
374
- * @since 1.0.0
375
- */
376
- loadToContext(directory, property, opt) {
377
- opt = Object.assign({}, {
378
- directory,
379
- property,
380
- inject: this.app,
381
- }, opt);
382
- const timingKey = `Load "${String(property)}" to Context`;
383
- this.timing.start(timingKey);
384
- new ContextLoader(opt).load();
385
- this.timing.end(timingKey);
386
- }
387
-
388
- /**
389
- * @member {FileLoader} EeLoader#FileLoader
390
- * @since 1.0.0
391
- */
392
- get FileLoader() {
393
- return FileLoader;
394
- }
395
-
396
- /**
397
- * @member {ContextLoader} EeLoader#ContextLoader
398
- * @since 1.0.0
399
- */
400
- get ContextLoader() {
401
- return ContextLoader;
402
- }
403
-
404
- getTypeFiles(filename) {
405
- const files = [ `${filename}.default` ];
406
- if (this.serverScope) files.push(`${filename}.${this.serverScope}`);
407
- if (this.serverEnv === 'default') return files;
408
-
409
- files.push(`${filename}.${this.serverEnv}`);
410
- if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}`);
411
- return files;
412
- }
413
-
414
- resolveModule(filepath) {
415
- let fullPath;
416
- try {
417
- fullPath = require.resolve(filepath);
418
- } catch (e) {
419
- return undefined;
420
- }
421
-
422
- if (process.env.Ee_TYPESCRIPT !== 'true' && fullPath.endsWith('.ts')) {
423
- return undefined;
424
- }
425
-
426
- return fullPath;
427
- }
428
-
429
- getPkg() {
430
- const filePath = path.join(this.options.homeDir, 'package.json');
431
- const json = utility.readJSONSync(filePath);
432
- return json;
433
- }
434
- }
435
-
436
- /**
437
- * Mixin methods to EeLoader
438
- * // ES6 Multiple Inheritance
439
- * https://medium.com/@leocavalcante/es6-multiple-inheritance-73a3c66d2b6b
440
- */
441
- const loaders = [
442
- require('./mixin/config'),
443
- require('./mixin/service'),
444
- require('./mixin/controller'),
445
- ];
446
-
447
- for (const loader of loaders) {
448
- Object.assign(EeLoader.prototype, loader);
449
- }
450
-
451
- module.exports = EeLoader;
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 utility = require('utility');
11
+ const utils = require('../utils');
12
+ const Timing = require('../utils/timing');
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
+ const envPath = path.join(this.options.baseDir, 'config/env');
87
+ if (!serverEnv && fs.existsSync(envPath)) {
88
+ serverEnv = fs.readFileSync(envPath, 'utf8').trim();
89
+ }
90
+
91
+ if (!serverEnv) {
92
+ serverEnv = process.env.EE_SERVER_ENV;
93
+ }
94
+
95
+ if (!serverEnv) {
96
+ if (process.env.NODE_ENV === 'test') {
97
+ serverEnv = 'unittest';
98
+ } else if (process.env.NODE_ENV === 'production') {
99
+ serverEnv = 'prod';
100
+ } else {
101
+ serverEnv = 'local';
102
+ }
103
+ } else {
104
+ serverEnv = serverEnv.trim();
105
+ }
106
+
107
+ return serverEnv;
108
+ }
109
+
110
+ /**
111
+ * Get {@link AppInfo#scope}
112
+ * @return {String} serverScope
113
+ * @private
114
+ */
115
+ getServerScope() {
116
+ return process.env.EE_SERVER_SCOPE || '';
117
+ }
118
+
119
+ /**
120
+ * Get {@link AppInfo#name}
121
+ * @return {String} appname
122
+ * @private
123
+ * @since 1.0.0
124
+ */
125
+ getAppname() {
126
+ if (this.pkg.name) {
127
+ debug('Loaded appname(%s) from package.json', this.pkg.name);
128
+ return this.pkg.name;
129
+ }
130
+
131
+ throw new Error(`name is required from ${pkg}`);
132
+ }
133
+
134
+ /**
135
+ * Get home directory
136
+ * @return {String} home directory
137
+ * @since 3.4.0
138
+ */
139
+ getHomedir() {
140
+ return this.options.homeDir;
141
+ }
142
+
143
+ /**
144
+ * Get app info
145
+ * @return {AppInfo} appInfo
146
+ * @since 1.0.0
147
+ */
148
+ getAppInfo() {
149
+ const env = this.serverEnv;
150
+
151
+ /**
152
+ * Meta information of the application
153
+ * @class AppInfo
154
+ */
155
+ return {
156
+ /**
157
+ * The name of the application, retrieve from the name property in `package.json`.
158
+ * @member {String} AppInfo#name
159
+ */
160
+ name: this.getAppname(),
161
+
162
+ /**
163
+ * The current directory, where the application code is.
164
+ * @member {String} AppInfo#baseDir
165
+ */
166
+ baseDir: this.options.baseDir,
167
+
168
+ /**
169
+ * The environment of the application, **it's not NODE_ENV**
170
+ *
171
+ * 1. from `$baseDir/config/env`
172
+ * 2. from EE_SERVER_ENV
173
+ * 3. from NODE_ENV
174
+ *
175
+ * env | description
176
+ * --- | ---
177
+ * test | system integration testing
178
+ * prod | production
179
+ * local | local on your own computer
180
+ * unittest | unit test
181
+ *
182
+ * @member {String} AppInfo#env
183
+ * @see https://Eejs.org/zh-cn/basics/env.html
184
+ */
185
+ env: env,
186
+
187
+ /**
188
+ * @member {String} AppInfo#scope
189
+ */
190
+ scope: this.serverScope,
191
+
192
+ /**
193
+ * The use directory, same as `process.env.HOME`
194
+ * @member {String} AppInfo#HOME
195
+ */
196
+ home: this.getHomedir(),
197
+
198
+ /**
199
+ * The directory whether is baseDir or HOME depend on env.
200
+ * it's good for test when you want to write some file to HOME,
201
+ * but don't want to write to the real directory,
202
+ * so use root to write file to baseDir instead of HOME when unittest.
203
+ * keep root directory in baseDir when local and unittest
204
+ * @member {String} AppInfo#root
205
+ */
206
+ root: env === 'local' || env === 'unittest' ? this.getHomedir() : this.options.appUserData,
207
+
208
+ /**
209
+ * electron application data dir
210
+ * @member {String} AppInfo#appUserDataDir
211
+ */
212
+ appUserDataDir: this.options.appUserData,
213
+
214
+ /**
215
+ * system user home dir
216
+ * @member {String} AppInfo#userHome
217
+ */
218
+ userHome: this.options.userHome,
219
+
220
+ /**
221
+ * application version
222
+ * @member {String} AppInfo#appVersion
223
+ */
224
+ appVersion: this.options.appVersion,
225
+
226
+ /**
227
+ * application package status
228
+ * @member {boolean} AppInfo#isPackaged
229
+ */
230
+ isPackaged: this.options.isPackaged,
231
+
232
+ /**
233
+ * application exec file dir
234
+ * @member {String} AppInfo#execDir
235
+ */
236
+ execDir: this.options.execDir
237
+ };
238
+ }
239
+
240
+ /**
241
+ * Get {@link EeLoader#EePaths}
242
+ * @return {Array} framework directories
243
+ * @see {@link EeLoader#EePaths}
244
+ * @private
245
+ * @since 1.0.0
246
+ */
247
+ getEePaths() {
248
+ // avoid require recursively
249
+ const EePaths = [];
250
+ EePaths.push(this.app[Symbol.for('ee#eePath')]);
251
+
252
+ return EePaths;
253
+ }
254
+
255
+ // Low Level API
256
+
257
+ /**
258
+ * Load single file, will invoke when export is function
259
+ *
260
+ * @param {String} filepath - fullpath
261
+ * @param {Array} inject - pass rest arguments into the function when invoke
262
+ * @return {Object} exports
263
+ * @example
264
+ * ```js
265
+ * app.loader.loadFile(path.join(app.options.baseDir, 'config/router.js'));
266
+ * ```
267
+ * @since 1.0.0
268
+ */
269
+ loadFile(filepath, ...inject) {
270
+ filepath = filepath && this.resolveModule(filepath);
271
+ if (!filepath) {
272
+ return null;
273
+ }
274
+
275
+ // function(arg1, args, ...) {}
276
+ if (inject.length === 0) inject = [ this.app ];
277
+
278
+ let ret = this.requireFile(filepath);
279
+ if (is.function(ret) && !is.class(ret)) {
280
+ ret = ret(...inject);
281
+ }
282
+ return ret;
283
+ }
284
+
285
+ /**
286
+ * @param {String} filepath - fullpath
287
+ * @return {Object} exports
288
+ * @private
289
+ */
290
+ requireFile(filepath) {
291
+ const timingKey = `Require(${this[REQUIRE_COUNT]++}) ${utils.getResolvedFilename(filepath, this.options.baseDir)}`;
292
+ this.timing.start(timingKey);
293
+ const ret = utils.loadFile(filepath);
294
+ this.timing.end(timingKey);
295
+ return ret;
296
+ }
297
+
298
+ /**
299
+ * Get all loadUnit
300
+ *
301
+ * loadUnit is a directory that can be loaded by EeLoader, it has the same structure.
302
+ * loadUnit has a path and a type(app, framework, plugin).
303
+ *
304
+ * The order of the loadUnits:
305
+ *
306
+ * 1. plugin
307
+ * 2. framework
308
+ * 3. app
309
+ *
310
+ * @return {Array} loadUnits
311
+ * @since 1.0.0
312
+ */
313
+ getLoadUnits() {
314
+ if (this.dirs) {
315
+ return this.dirs;
316
+ }
317
+
318
+ const dirs = this.dirs = [];
319
+
320
+ if (this.orderPlugins) {
321
+ for (const plugin of this.orderPlugins) {
322
+ dirs.push({
323
+ path: plugin.path,
324
+ type: 'plugin',
325
+ });
326
+ }
327
+ }
328
+
329
+ // framework or Ee path
330
+ for (const EePath of this.EePaths) {
331
+ dirs.push({
332
+ path: EePath,
333
+ type: 'framework',
334
+ });
335
+ }
336
+
337
+ // application
338
+ dirs.push({
339
+ path: this.options.baseDir,
340
+ type: 'app',
341
+ });
342
+
343
+ debug('Loaded dirs %j', dirs);
344
+ return dirs;
345
+ }
346
+
347
+ /**
348
+ * Load files using {@link FileLoader}, inject to {@link Application}
349
+ * @param {String|Array} directory - see {@link FileLoader}
350
+ * @param {String} property - see {@link FileLoader}
351
+ * @param {Object} opt - see {@link FileLoader}
352
+ * @since 1.0.0
353
+ */
354
+ loadToApp(directory, property, opt) {
355
+ const target = this.app[property] = {};
356
+ opt = Object.assign({}, {
357
+ directory,
358
+ target,
359
+ inject: this.app,
360
+ }, opt);
361
+
362
+ const timingKey = `Load "${String(property)}" to Application`;
363
+ this.timing.start(timingKey);
364
+ new FileLoader(opt).load();
365
+ //console.log('app property:', this.app[property]);
366
+ this.timing.end(timingKey);
367
+ }
368
+
369
+ /**
370
+ * Load files using {@link ContextLoader}
371
+ * @param {String|Array} directory - see {@link ContextLoader}
372
+ * @param {String} property - see {@link ContextLoader}
373
+ * @param {Object} opt - see {@link ContextLoader}
374
+ * @since 1.0.0
375
+ */
376
+ loadToContext(directory, property, opt) {
377
+ opt = Object.assign({}, {
378
+ directory,
379
+ property,
380
+ inject: this.app,
381
+ }, opt);
382
+ const timingKey = `Load "${String(property)}" to Context`;
383
+ this.timing.start(timingKey);
384
+ new ContextLoader(opt).load();
385
+ this.timing.end(timingKey);
386
+ }
387
+
388
+ /**
389
+ * @member {FileLoader} EeLoader#FileLoader
390
+ * @since 1.0.0
391
+ */
392
+ get FileLoader() {
393
+ return FileLoader;
394
+ }
395
+
396
+ /**
397
+ * @member {ContextLoader} EeLoader#ContextLoader
398
+ * @since 1.0.0
399
+ */
400
+ get ContextLoader() {
401
+ return ContextLoader;
402
+ }
403
+
404
+ getTypeFiles(filename) {
405
+ const files = [ `${filename}.default` ];
406
+ if (this.serverScope) files.push(`${filename}.${this.serverScope}`);
407
+ if (this.serverEnv === 'default') return files;
408
+
409
+ files.push(`${filename}.${this.serverEnv}`);
410
+ if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}`);
411
+ return files;
412
+ }
413
+
414
+ resolveModule(filepath) {
415
+ let fullPath;
416
+ try {
417
+ fullPath = require.resolve(filepath);
418
+ } catch (e) {
419
+ return undefined;
420
+ }
421
+
422
+ if (process.env.Ee_TYPESCRIPT !== 'true' && fullPath.endsWith('.ts')) {
423
+ return undefined;
424
+ }
425
+
426
+ return fullPath;
427
+ }
428
+
429
+ getPkg() {
430
+ const filePath = path.join(this.options.homeDir, 'package.json');
431
+ const json = utility.readJSONSync(filePath);
432
+ return json;
433
+ }
434
+ }
435
+
436
+ /**
437
+ * Mixin methods to EeLoader
438
+ * // ES6 Multiple Inheritance
439
+ * https://medium.com/@leocavalcante/es6-multiple-inheritance-73a3c66d2b6b
440
+ */
441
+ const loaders = [
442
+ require('./mixin/config'),
443
+ require('./mixin/service'),
444
+ require('./mixin/controller'),
445
+ ];
446
+
447
+ for (const loader of loaders) {
448
+ Object.assign(EeLoader.prototype, loader);
449
+ }
450
+
451
+ module.exports = EeLoader;