ee-core 4.1.3 → 4.1.4
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/LICENSE +21 -21
- package/README.md +65 -65
- package/app/application.d.ts +5 -5
- package/app/application.js +30 -30
- package/app/boot.d.ts +5 -5
- package/app/boot.js +80 -80
- package/app/dir.d.ts +1 -1
- package/app/dir.js +26 -26
- package/app/events.d.ts +14 -14
- package/app/events.js +55 -55
- package/app/index.d.ts +2 -2
- package/app/index.js +6 -6
- package/config/config_loader.d.ts +13 -13
- package/config/config_loader.js +65 -65
- package/config/default_config.d.ts +95 -95
- package/config/default_config.js +111 -111
- package/config/index.d.ts +28 -28
- package/config/index.js +24 -24
- package/const/channel.d.ts +16 -16
- package/const/channel.js +27 -27
- package/controller/controller_loader.d.ts +9 -9
- package/controller/controller_loader.js +77 -77
- package/controller/index.d.ts +2 -2
- package/controller/index.js +24 -24
- package/core/index.d.ts +2 -2
- package/core/index.js +11 -11
- package/core/loader/file_loader.d.ts +66 -66
- package/core/loader/file_loader.js +211 -211
- package/core/utils/index.d.ts +12 -12
- package/core/utils/index.js +83 -83
- package/core/utils/timing.d.ts +22 -22
- package/core/utils/timing.js +78 -78
- package/cross/cross.d.ts +19 -19
- package/cross/cross.js +151 -151
- package/cross/crossProcess.d.ts +29 -29
- package/cross/crossProcess.js +172 -172
- package/cross/index.d.ts +2 -2
- package/cross/index.js +8 -8
- package/electron/app/index.d.ts +5 -5
- package/electron/app/index.js +48 -48
- package/electron/index.d.ts +3 -3
- package/electron/index.js +15 -15
- package/electron/window/index.d.ts +6 -6
- package/electron/window/index.js +268 -268
- package/exception/index.d.ts +9 -9
- package/exception/index.js +100 -100
- package/html/boot.html +98 -98
- package/html/cross-failure.html +28 -28
- package/html/failure.html +28 -28
- package/html/index.d.ts +1 -1
- package/html/index.js +10 -10
- package/index.d.ts +2 -2
- package/index.js +6 -6
- package/jobs/child/app.d.ts +1 -1
- package/jobs/child/app.js +69 -69
- package/jobs/child/index.d.ts +13 -13
- package/jobs/child/index.js +85 -85
- package/jobs/child/jobProcess.d.ts +16 -16
- package/jobs/child/jobProcess.js +133 -133
- package/jobs/child-pool/index.d.ts +26 -26
- package/jobs/child-pool/index.js +190 -190
- package/jobs/index.d.ts +3 -3
- package/jobs/index.js +6 -6
- package/jobs/load-balancer/algorithm/index.d.ts +4 -4
- package/jobs/load-balancer/algorithm/index.js +11 -11
- package/jobs/load-balancer/algorithm/minimumConnection.d.ts +2 -2
- package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
- package/jobs/load-balancer/algorithm/polling.d.ts +2 -2
- package/jobs/load-balancer/algorithm/polling.js +11 -11
- package/jobs/load-balancer/algorithm/random.d.ts +2 -2
- package/jobs/load-balancer/algorithm/random.js +9 -9
- package/jobs/load-balancer/algorithm/specify.d.ts +2 -2
- package/jobs/load-balancer/algorithm/specify.js +14 -14
- package/jobs/load-balancer/algorithm/weights.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weights.js +21 -21
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
- package/jobs/load-balancer/algorithm/weightsPolling.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
- package/jobs/load-balancer/algorithm/weightsRandom.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
- package/jobs/load-balancer/consts.d.ts +8 -8
- package/jobs/load-balancer/consts.js +9 -9
- package/jobs/load-balancer/index.d.ts +96 -96
- package/jobs/load-balancer/index.js +201 -201
- package/jobs/load-balancer/scheduler.d.ts +16 -16
- package/jobs/load-balancer/scheduler.js +31 -31
- package/loader/index.d.ts +9 -9
- package/loader/index.js +99 -98
- package/log/index.d.ts +5 -5
- package/log/index.js +85 -85
- package/log/logger.d.ts +1 -1
- package/log/logger.js +94 -94
- package/message/childMessage.d.ts +10 -10
- package/message/childMessage.js +43 -43
- package/message/index.d.ts +3 -3
- package/message/index.js +9 -9
- package/package.json +42 -42
- package/ps/index.d.ts +39 -39
- package/ps/index.js +294 -294
- package/socket/httpServer.d.ts +8 -8
- package/socket/httpServer.js +199 -199
- package/socket/index.d.ts +10 -10
- package/socket/index.js +64 -64
- package/socket/ipcServer.d.ts +9 -9
- package/socket/ipcServer.js +105 -105
- package/socket/socketServer.d.ts +10 -10
- package/socket/socketServer.js +81 -81
- package/storage/index.d.ts +4 -4
- package/storage/index.js +6 -6
- package/storage/sqliteStorage.d.ts +16 -16
- package/storage/sqliteStorage.js +123 -123
- package/tsconfig.json +13 -13
- package/utils/extend.d.ts +2 -2
- package/utils/extend.js +77 -77
- package/utils/helper.d.ts +14 -14
- package/utils/helper.js +206 -206
- package/utils/index.d.ts +10 -10
- package/utils/index.js +167 -167
- package/utils/ip.d.ts +2 -2
- package/utils/ip.js +221 -221
- package/utils/is.d.ts +14 -14
- package/utils/is.js +111 -111
- package/utils/json.d.ts +5 -5
- package/utils/json.js +79 -79
- package/utils/pargv.d.ts +3 -3
- package/utils/pargv.js +267 -267
- package/utils/port/index.d.ts +1 -1
- package/utils/port/index.js +125 -125
- package/utils/wrap.d.ts +4 -4
- package/utils/wrap.js +40 -40
|
@@ -1,212 +1,212 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')('ee-core:core:loader:file_loader');
|
|
4
|
-
const assert = require('assert');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const globby = require('globby');
|
|
8
|
-
const is = require('is-type-of');
|
|
9
|
-
const { isBytecodeClass, loadFile, filePatterns } = require('../utils');
|
|
10
|
-
const FULLPATH = Symbol('LOADER_ITEM_FULLPATH');
|
|
11
|
-
const EXPORTS = Symbol('LOADER_ITEM_EXPORTS');
|
|
12
|
-
|
|
13
|
-
const defaults = {
|
|
14
|
-
directory: null,
|
|
15
|
-
target: null,
|
|
16
|
-
match: undefined,
|
|
17
|
-
caseStyle: 'camel',
|
|
18
|
-
initializer: null,
|
|
19
|
-
call: true,
|
|
20
|
-
inject: undefined,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Load files from directory to target object.
|
|
25
|
-
*/
|
|
26
|
-
class FileLoader {
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @class
|
|
30
|
-
* @param {Object} options - options
|
|
31
|
-
* @param {String|Array} options.directory - directories to be loaded
|
|
32
|
-
* @param {Object} options.target - attach the target object from loaded files
|
|
33
|
-
* @param {String} options.match - match the files when load, support glob, default to all js files
|
|
34
|
-
* @param {Function} options.initializer - custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an `options` object that contain `path`
|
|
35
|
-
* @param {Boolean} options.call - determine whether invoke when exports is function
|
|
36
|
-
* @param {Object} options.inject - an object that be the argument when invoke the function
|
|
37
|
-
* @param {String|Function} options.caseStyle - set property's case when converting a filepath to property list.
|
|
38
|
-
*/
|
|
39
|
-
constructor(options) {
|
|
40
|
-
assert(options.directory, 'options.directory is required');
|
|
41
|
-
this.options = Object.assign({}, defaults, options);
|
|
42
|
-
debug("[constructor] options: %o", this.options);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* attach items to target object. Mapping the directory to properties.
|
|
47
|
-
* `xxx/group/repository.js` => `target.group.repository`
|
|
48
|
-
* @return {Object} target
|
|
49
|
-
*/
|
|
50
|
-
load() {
|
|
51
|
-
const items = this.parse();
|
|
52
|
-
const target = {};
|
|
53
|
-
for (const item of items) {
|
|
54
|
-
// item { fullpath, properties: [ 'a', 'b', 'c'], exports }
|
|
55
|
-
item.properties.reduce((target, property, index) => {
|
|
56
|
-
let obj;
|
|
57
|
-
// properties is a path slice, only the last value is the file name
|
|
58
|
-
if (index === item.properties.length - 1) {
|
|
59
|
-
obj = item.exports;
|
|
60
|
-
if (obj && !is.primitive(obj)) {
|
|
61
|
-
obj[FULLPATH] = item.fullpath;
|
|
62
|
-
obj[EXPORTS] = true;
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
obj = target[property] || {};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
target[property] = obj;
|
|
69
|
-
// const properties = item.properties.slice(0, index + 1).join('.');
|
|
70
|
-
// debug('[load] properties: %s', properties);
|
|
71
|
-
return obj;
|
|
72
|
-
}, target);
|
|
73
|
-
}
|
|
74
|
-
//debug('[load] target: %O', target);
|
|
75
|
-
return target;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Parse files from given directories, then return an items list, each item contains properties and exports.
|
|
80
|
-
* For example, parse `controller/group/repository.js`
|
|
81
|
-
* It returns a item
|
|
82
|
-
* ```
|
|
83
|
-
* {
|
|
84
|
-
* fullpath: '',
|
|
85
|
-
* properties: [ 'group', 'repository' ],
|
|
86
|
-
* exports: { ... },
|
|
87
|
-
* }
|
|
88
|
-
* ```
|
|
89
|
-
* `Properties` is an array that contains the directory of a filepath.
|
|
90
|
-
* `Exports` depends on type, if exports is a function, it will be called. if initializer is specified, it will be called with exports for customizing.
|
|
91
|
-
* @return {Array} items
|
|
92
|
-
*/
|
|
93
|
-
parse() {
|
|
94
|
-
let files = this.options.match;
|
|
95
|
-
if (!files) {
|
|
96
|
-
files = filePatterns();
|
|
97
|
-
} else {
|
|
98
|
-
files = Array.isArray(files) ? files : [ files ];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
let directories = this.options.directory;
|
|
102
|
-
if (!Array.isArray(directories)) {
|
|
103
|
-
directories = [ directories ];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const items = [];
|
|
107
|
-
debug('[parse] directories %o', directories);
|
|
108
|
-
|
|
109
|
-
for (const directory of directories) {
|
|
110
|
-
const filepaths = globby.sync(files, { cwd: directory });
|
|
111
|
-
debug('[parse] filepaths %o', filepaths);
|
|
112
|
-
for (const filepath of filepaths) {
|
|
113
|
-
const fullpath = path.join(directory, filepath);
|
|
114
|
-
if (!fs.statSync(fullpath).isFile()) continue;
|
|
115
|
-
// get properties
|
|
116
|
-
// controller/foo/bar.js => [ 'foo', 'bar' ]
|
|
117
|
-
const properties = getProperties(filepath, this.options);
|
|
118
|
-
// debug('[parse] properties %o', properties);
|
|
119
|
-
// controller/foo/bar.js => controller.foo.bar
|
|
120
|
-
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
121
|
-
// debug('[parse] pathName %s', pathName);
|
|
122
|
-
// get exports from the file
|
|
123
|
-
let exports = getExports(fullpath, this.options, pathName);
|
|
124
|
-
// ignore exports when it's null or false returned by filter function
|
|
125
|
-
if (exports == null) continue;
|
|
126
|
-
|
|
127
|
-
// set properties of class
|
|
128
|
-
if (is.class(exports) || isBytecodeClass(exports)) {
|
|
129
|
-
exports.prototype.pathName = pathName;
|
|
130
|
-
exports.prototype.fullPath = fullpath;
|
|
131
|
-
}
|
|
132
|
-
items.push({ fullpath, properties, exports });
|
|
133
|
-
//debug('[parse] fullpath %s, properties %o, export %o', fullpath, properties, exports);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
//debug('[parse] items %O', items);
|
|
137
|
-
return items;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// convert file path to an array of properties
|
|
142
|
-
// a/b/c.js => ['a', 'b', 'c']
|
|
143
|
-
function getProperties(filepath, { caseStyle }) {
|
|
144
|
-
// if caseStyle is function, return the result of function
|
|
145
|
-
if (is.function(caseStyle)) {
|
|
146
|
-
const result = caseStyle(filepath);
|
|
147
|
-
assert(is.array(result), `caseStyle expect an array, but got ${result}`);
|
|
148
|
-
return result;
|
|
149
|
-
}
|
|
150
|
-
// use default camelize
|
|
151
|
-
return defaultCamelize(filepath, caseStyle);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Get exports from filepath
|
|
155
|
-
// If exports is null/undefined, it will be ignored
|
|
156
|
-
function getExports(fullpath, { initializer, call, inject }, pathName) {
|
|
157
|
-
let exports = loadFile(fullpath);
|
|
158
|
-
//debug('[getExports] exports %o', exports);
|
|
159
|
-
if (initializer) {
|
|
160
|
-
// exports type is Class or Object
|
|
161
|
-
exports = initializer(exports, { path: fullpath, pathName });
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (is.class(exports) || is.generatorFunction(exports) || is.asyncFunction(exports) || isBytecodeClass(exports)) {
|
|
165
|
-
return exports;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// whether to execute the function
|
|
169
|
-
if (call && is.function(exports)) {
|
|
170
|
-
exports = exports(inject);
|
|
171
|
-
if (exports != null) {
|
|
172
|
-
return exports;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return exports;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function defaultCamelize(filepath, caseStyle) {
|
|
180
|
-
const properties = filepath.substring(0, filepath.lastIndexOf('.')).split('/');
|
|
181
|
-
return properties.map(property => {
|
|
182
|
-
if (!/^[a-z][a-z0-9_-]*$/i.test(property)) {
|
|
183
|
-
throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// use default camelize, will capitalize the first letter
|
|
187
|
-
// foo_bar.js > FooBar
|
|
188
|
-
// fooBar.js > FooBar
|
|
189
|
-
// FooBar.js > FooBar
|
|
190
|
-
// FooBar.js > FooBar
|
|
191
|
-
// FooBar.js > fooBar
|
|
192
|
-
property = property.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
|
|
193
|
-
let first = property[0];
|
|
194
|
-
switch (caseStyle) {
|
|
195
|
-
case 'lower':
|
|
196
|
-
first = first.toLowerCase();
|
|
197
|
-
break;
|
|
198
|
-
case 'upper':
|
|
199
|
-
first = first.toUpperCase();
|
|
200
|
-
break;
|
|
201
|
-
case 'camel':
|
|
202
|
-
default:
|
|
203
|
-
}
|
|
204
|
-
return first + property.substring(1);
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
module.exports = {
|
|
209
|
-
FileLoader,
|
|
210
|
-
EXPORTS,
|
|
211
|
-
FULLPATH,
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const debug = require('debug')('ee-core:core:loader:file_loader');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const globby = require('globby');
|
|
8
|
+
const is = require('is-type-of');
|
|
9
|
+
const { isBytecodeClass, loadFile, filePatterns } = require('../utils');
|
|
10
|
+
const FULLPATH = Symbol('LOADER_ITEM_FULLPATH');
|
|
11
|
+
const EXPORTS = Symbol('LOADER_ITEM_EXPORTS');
|
|
12
|
+
|
|
13
|
+
const defaults = {
|
|
14
|
+
directory: null,
|
|
15
|
+
target: null,
|
|
16
|
+
match: undefined,
|
|
17
|
+
caseStyle: 'camel',
|
|
18
|
+
initializer: null,
|
|
19
|
+
call: true,
|
|
20
|
+
inject: undefined,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Load files from directory to target object.
|
|
25
|
+
*/
|
|
26
|
+
class FileLoader {
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @class
|
|
30
|
+
* @param {Object} options - options
|
|
31
|
+
* @param {String|Array} options.directory - directories to be loaded
|
|
32
|
+
* @param {Object} options.target - attach the target object from loaded files
|
|
33
|
+
* @param {String} options.match - match the files when load, support glob, default to all js files
|
|
34
|
+
* @param {Function} options.initializer - custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an `options` object that contain `path`
|
|
35
|
+
* @param {Boolean} options.call - determine whether invoke when exports is function
|
|
36
|
+
* @param {Object} options.inject - an object that be the argument when invoke the function
|
|
37
|
+
* @param {String|Function} options.caseStyle - set property's case when converting a filepath to property list.
|
|
38
|
+
*/
|
|
39
|
+
constructor(options) {
|
|
40
|
+
assert(options.directory, 'options.directory is required');
|
|
41
|
+
this.options = Object.assign({}, defaults, options);
|
|
42
|
+
debug("[constructor] options: %o", this.options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* attach items to target object. Mapping the directory to properties.
|
|
47
|
+
* `xxx/group/repository.js` => `target.group.repository`
|
|
48
|
+
* @return {Object} target
|
|
49
|
+
*/
|
|
50
|
+
load() {
|
|
51
|
+
const items = this.parse();
|
|
52
|
+
const target = {};
|
|
53
|
+
for (const item of items) {
|
|
54
|
+
// item { fullpath, properties: [ 'a', 'b', 'c'], exports }
|
|
55
|
+
item.properties.reduce((target, property, index) => {
|
|
56
|
+
let obj;
|
|
57
|
+
// properties is a path slice, only the last value is the file name
|
|
58
|
+
if (index === item.properties.length - 1) {
|
|
59
|
+
obj = item.exports;
|
|
60
|
+
if (obj && !is.primitive(obj)) {
|
|
61
|
+
obj[FULLPATH] = item.fullpath;
|
|
62
|
+
obj[EXPORTS] = true;
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
obj = target[property] || {};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
target[property] = obj;
|
|
69
|
+
// const properties = item.properties.slice(0, index + 1).join('.');
|
|
70
|
+
// debug('[load] properties: %s', properties);
|
|
71
|
+
return obj;
|
|
72
|
+
}, target);
|
|
73
|
+
}
|
|
74
|
+
//debug('[load] target: %O', target);
|
|
75
|
+
return target;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Parse files from given directories, then return an items list, each item contains properties and exports.
|
|
80
|
+
* For example, parse `controller/group/repository.js`
|
|
81
|
+
* It returns a item
|
|
82
|
+
* ```
|
|
83
|
+
* {
|
|
84
|
+
* fullpath: '',
|
|
85
|
+
* properties: [ 'group', 'repository' ],
|
|
86
|
+
* exports: { ... },
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
* `Properties` is an array that contains the directory of a filepath.
|
|
90
|
+
* `Exports` depends on type, if exports is a function, it will be called. if initializer is specified, it will be called with exports for customizing.
|
|
91
|
+
* @return {Array} items
|
|
92
|
+
*/
|
|
93
|
+
parse() {
|
|
94
|
+
let files = this.options.match;
|
|
95
|
+
if (!files) {
|
|
96
|
+
files = filePatterns();
|
|
97
|
+
} else {
|
|
98
|
+
files = Array.isArray(files) ? files : [ files ];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let directories = this.options.directory;
|
|
102
|
+
if (!Array.isArray(directories)) {
|
|
103
|
+
directories = [ directories ];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const items = [];
|
|
107
|
+
debug('[parse] directories %o', directories);
|
|
108
|
+
|
|
109
|
+
for (const directory of directories) {
|
|
110
|
+
const filepaths = globby.sync(files, { cwd: directory });
|
|
111
|
+
debug('[parse] filepaths %o', filepaths);
|
|
112
|
+
for (const filepath of filepaths) {
|
|
113
|
+
const fullpath = path.join(directory, filepath);
|
|
114
|
+
if (!fs.statSync(fullpath).isFile()) continue;
|
|
115
|
+
// get properties
|
|
116
|
+
// controller/foo/bar.js => [ 'foo', 'bar' ]
|
|
117
|
+
const properties = getProperties(filepath, this.options);
|
|
118
|
+
// debug('[parse] properties %o', properties);
|
|
119
|
+
// controller/foo/bar.js => controller.foo.bar
|
|
120
|
+
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
121
|
+
// debug('[parse] pathName %s', pathName);
|
|
122
|
+
// get exports from the file
|
|
123
|
+
let exports = getExports(fullpath, this.options, pathName);
|
|
124
|
+
// ignore exports when it's null or false returned by filter function
|
|
125
|
+
if (exports == null) continue;
|
|
126
|
+
|
|
127
|
+
// set properties of class
|
|
128
|
+
if (is.class(exports) || isBytecodeClass(exports)) {
|
|
129
|
+
exports.prototype.pathName = pathName;
|
|
130
|
+
exports.prototype.fullPath = fullpath;
|
|
131
|
+
}
|
|
132
|
+
items.push({ fullpath, properties, exports });
|
|
133
|
+
//debug('[parse] fullpath %s, properties %o, export %o', fullpath, properties, exports);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//debug('[parse] items %O', items);
|
|
137
|
+
return items;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// convert file path to an array of properties
|
|
142
|
+
// a/b/c.js => ['a', 'b', 'c']
|
|
143
|
+
function getProperties(filepath, { caseStyle }) {
|
|
144
|
+
// if caseStyle is function, return the result of function
|
|
145
|
+
if (is.function(caseStyle)) {
|
|
146
|
+
const result = caseStyle(filepath);
|
|
147
|
+
assert(is.array(result), `caseStyle expect an array, but got ${result}`);
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
// use default camelize
|
|
151
|
+
return defaultCamelize(filepath, caseStyle);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Get exports from filepath
|
|
155
|
+
// If exports is null/undefined, it will be ignored
|
|
156
|
+
function getExports(fullpath, { initializer, call, inject }, pathName) {
|
|
157
|
+
let exports = loadFile(fullpath);
|
|
158
|
+
//debug('[getExports] exports %o', exports);
|
|
159
|
+
if (initializer) {
|
|
160
|
+
// exports type is Class or Object
|
|
161
|
+
exports = initializer(exports, { path: fullpath, pathName });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (is.class(exports) || is.generatorFunction(exports) || is.asyncFunction(exports) || isBytecodeClass(exports)) {
|
|
165
|
+
return exports;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// whether to execute the function
|
|
169
|
+
if (call && is.function(exports)) {
|
|
170
|
+
exports = exports(inject);
|
|
171
|
+
if (exports != null) {
|
|
172
|
+
return exports;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return exports;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function defaultCamelize(filepath, caseStyle) {
|
|
180
|
+
const properties = filepath.substring(0, filepath.lastIndexOf('.')).split('/');
|
|
181
|
+
return properties.map(property => {
|
|
182
|
+
if (!/^[a-z][a-z0-9_-]*$/i.test(property)) {
|
|
183
|
+
throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// use default camelize, will capitalize the first letter
|
|
187
|
+
// foo_bar.js > FooBar
|
|
188
|
+
// fooBar.js > FooBar
|
|
189
|
+
// FooBar.js > FooBar
|
|
190
|
+
// FooBar.js > FooBar
|
|
191
|
+
// FooBar.js > fooBar
|
|
192
|
+
property = property.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
|
|
193
|
+
let first = property[0];
|
|
194
|
+
switch (caseStyle) {
|
|
195
|
+
case 'lower':
|
|
196
|
+
first = first.toLowerCase();
|
|
197
|
+
break;
|
|
198
|
+
case 'upper':
|
|
199
|
+
first = first.toUpperCase();
|
|
200
|
+
break;
|
|
201
|
+
case 'camel':
|
|
202
|
+
default:
|
|
203
|
+
}
|
|
204
|
+
return first + property.substring(1);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
module.exports = {
|
|
209
|
+
FileLoader,
|
|
210
|
+
EXPORTS,
|
|
211
|
+
FULLPATH,
|
|
212
212
|
};
|
package/core/utils/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export declare const extensions: any;
|
|
2
|
-
export declare function loadFile(filepath: any): any;
|
|
3
|
-
export declare function callFn(fn: any, args: any, ctx: any): Promise<any>;
|
|
4
|
-
export declare function getResolvedFilename(filepath: any, baseDir: any): any;
|
|
5
|
-
/**
|
|
6
|
-
* 字节码类
|
|
7
|
-
*/
|
|
8
|
-
export declare function isBytecodeClass(exports: any): boolean;
|
|
9
|
-
/**
|
|
10
|
-
* 文件类型
|
|
11
|
-
*/
|
|
12
|
-
export declare function filePatterns(): string[];
|
|
1
|
+
export declare const extensions: any;
|
|
2
|
+
export declare function loadFile(filepath: any): any;
|
|
3
|
+
export declare function callFn(fn: any, args: any, ctx: any): Promise<any>;
|
|
4
|
+
export declare function getResolvedFilename(filepath: any, baseDir: any): any;
|
|
5
|
+
/**
|
|
6
|
+
* 字节码类
|
|
7
|
+
*/
|
|
8
|
+
export declare function isBytecodeClass(exports: any): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* 文件类型
|
|
11
|
+
*/
|
|
12
|
+
export declare function filePatterns(): string[];
|
package/core/utils/index.js
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
require('bytenode');
|
|
4
|
-
const is = require('is-type-of');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const BuiltinModule = require('module');
|
|
8
|
-
|
|
9
|
-
// Guard against poorly mocked module constructors.
|
|
10
|
-
const Module = module.constructor.length > 1
|
|
11
|
-
? module.constructor
|
|
12
|
-
/* istanbul ignore next */
|
|
13
|
-
: BuiltinModule;
|
|
14
|
-
|
|
15
|
-
// Module._extensions:
|
|
16
|
-
// '.js': [Function (anonymous)],
|
|
17
|
-
// '.json': [Function (anonymous)],
|
|
18
|
-
// '.node': [Function: func],
|
|
19
|
-
// '.jsc': [Function (anonymous)]
|
|
20
|
-
|
|
21
|
-
const extensions = Module._extensions;
|
|
22
|
-
|
|
23
|
-
function loadFile(filepath) {
|
|
24
|
-
try {
|
|
25
|
-
// if not js module, just return content buffer
|
|
26
|
-
const extname = path.extname(filepath);
|
|
27
|
-
if (extname && !Module._extensions[extname]) {
|
|
28
|
-
return fs.readFileSync(filepath);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// require js module
|
|
32
|
-
const obj = require(filepath);
|
|
33
|
-
if (!obj) return obj;
|
|
34
|
-
// it's es module
|
|
35
|
-
if (obj.__esModule) return 'default' in obj ? obj.default : obj;
|
|
36
|
-
return obj;
|
|
37
|
-
} catch (err) {
|
|
38
|
-
err.message = `[ee-core] load file: ${filepath}, error: ${err.message}`;
|
|
39
|
-
throw err;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async function callFn(fn, args, ctx) {
|
|
44
|
-
args = args || [];
|
|
45
|
-
if (!is.function(fn)) return;
|
|
46
|
-
return ctx ? fn.call(ctx, ...args) : fn(...args);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function getResolvedFilename(filepath, baseDir) {
|
|
50
|
-
const reg = /[/\\]/g;
|
|
51
|
-
return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 字节码类
|
|
56
|
-
*/
|
|
57
|
-
function isBytecodeClass(exports) {
|
|
58
|
-
let isClass = false;
|
|
59
|
-
|
|
60
|
-
// 标识
|
|
61
|
-
if (exports.toString().indexOf('[class') != -1) {
|
|
62
|
-
isClass = true;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return isClass;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 文件类型
|
|
70
|
-
*/
|
|
71
|
-
function filePatterns() {
|
|
72
|
-
const files = [ '**/*.js','**/*.jsc' ];
|
|
73
|
-
return files;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
module.exports = {
|
|
77
|
-
extensions,
|
|
78
|
-
loadFile,
|
|
79
|
-
callFn,
|
|
80
|
-
getResolvedFilename,
|
|
81
|
-
isBytecodeClass,
|
|
82
|
-
filePatterns,
|
|
83
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('bytenode');
|
|
4
|
+
const is = require('is-type-of');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const BuiltinModule = require('module');
|
|
8
|
+
|
|
9
|
+
// Guard against poorly mocked module constructors.
|
|
10
|
+
const Module = module.constructor.length > 1
|
|
11
|
+
? module.constructor
|
|
12
|
+
/* istanbul ignore next */
|
|
13
|
+
: BuiltinModule;
|
|
14
|
+
|
|
15
|
+
// Module._extensions:
|
|
16
|
+
// '.js': [Function (anonymous)],
|
|
17
|
+
// '.json': [Function (anonymous)],
|
|
18
|
+
// '.node': [Function: func],
|
|
19
|
+
// '.jsc': [Function (anonymous)]
|
|
20
|
+
|
|
21
|
+
const extensions = Module._extensions;
|
|
22
|
+
|
|
23
|
+
function loadFile(filepath) {
|
|
24
|
+
try {
|
|
25
|
+
// if not js module, just return content buffer
|
|
26
|
+
const extname = path.extname(filepath);
|
|
27
|
+
if (extname && !Module._extensions[extname]) {
|
|
28
|
+
return fs.readFileSync(filepath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// require js module
|
|
32
|
+
const obj = require(filepath);
|
|
33
|
+
if (!obj) return obj;
|
|
34
|
+
// it's es module
|
|
35
|
+
if (obj.__esModule) return 'default' in obj ? obj.default : obj;
|
|
36
|
+
return obj;
|
|
37
|
+
} catch (err) {
|
|
38
|
+
err.message = `[ee-core] load file: ${filepath}, error: ${err.message}`;
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function callFn(fn, args, ctx) {
|
|
44
|
+
args = args || [];
|
|
45
|
+
if (!is.function(fn)) return;
|
|
46
|
+
return ctx ? fn.call(ctx, ...args) : fn(...args);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getResolvedFilename(filepath, baseDir) {
|
|
50
|
+
const reg = /[/\\]/g;
|
|
51
|
+
return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 字节码类
|
|
56
|
+
*/
|
|
57
|
+
function isBytecodeClass(exports) {
|
|
58
|
+
let isClass = false;
|
|
59
|
+
|
|
60
|
+
// 标识
|
|
61
|
+
if (exports.toString().indexOf('[class') != -1) {
|
|
62
|
+
isClass = true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return isClass;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 文件类型
|
|
70
|
+
*/
|
|
71
|
+
function filePatterns() {
|
|
72
|
+
const files = [ '**/*.js','**/*.jsc' ];
|
|
73
|
+
return files;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = {
|
|
77
|
+
extensions,
|
|
78
|
+
loadFile,
|
|
79
|
+
callFn,
|
|
80
|
+
getResolvedFilename,
|
|
81
|
+
isBytecodeClass,
|
|
82
|
+
filePatterns,
|
|
83
|
+
};
|
package/core/utils/timing.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
export declare class Timing {
|
|
2
|
-
_enable: boolean;
|
|
3
|
-
init(): void;
|
|
4
|
-
start(name: any, start: any): {
|
|
5
|
-
name: any;
|
|
6
|
-
start: any;
|
|
7
|
-
end: any;
|
|
8
|
-
duration: any;
|
|
9
|
-
pid: number;
|
|
10
|
-
index: number;
|
|
11
|
-
};
|
|
12
|
-
end(name: any): any;
|
|
13
|
-
enable(): void;
|
|
14
|
-
disable(): void;
|
|
15
|
-
clear(): void;
|
|
16
|
-
toJSON(): any[];
|
|
17
|
-
[MAP]: Map<any, any>;
|
|
18
|
-
[LIST]: any[];
|
|
19
|
-
}
|
|
20
|
-
declare const MAP: unique symbol;
|
|
21
|
-
declare const LIST: unique symbol;
|
|
22
|
-
export {};
|
|
1
|
+
export declare class Timing {
|
|
2
|
+
_enable: boolean;
|
|
3
|
+
init(): void;
|
|
4
|
+
start(name: any, start: any): {
|
|
5
|
+
name: any;
|
|
6
|
+
start: any;
|
|
7
|
+
end: any;
|
|
8
|
+
duration: any;
|
|
9
|
+
pid: number;
|
|
10
|
+
index: number;
|
|
11
|
+
};
|
|
12
|
+
end(name: any): any;
|
|
13
|
+
enable(): void;
|
|
14
|
+
disable(): void;
|
|
15
|
+
clear(): void;
|
|
16
|
+
toJSON(): any[];
|
|
17
|
+
[MAP]: Map<any, any>;
|
|
18
|
+
[LIST]: any[];
|
|
19
|
+
}
|
|
20
|
+
declare const MAP: unique symbol;
|
|
21
|
+
declare const LIST: unique symbol;
|
|
22
|
+
export {};
|