@midwayjs/core 3.11.12-beta.1 → 3.11.12-beta.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.
- package/dist/common/fileDetector.d.ts +1 -1
- package/dist/common/fileDetector.js +6 -10
- package/dist/context/container.js +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/setup.d.ts +6 -1
- package/dist/setup.js +128 -28
- package/dist/util/index.d.ts +11 -0
- package/dist/util/index.js +43 -1
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import { IFileDetector, IMidwayContainer } from '../interface';
|
|
|
2
2
|
export declare abstract class AbstractFileDetector<T> implements IFileDetector {
|
|
3
3
|
options: T;
|
|
4
4
|
extraDetectorOptions: T;
|
|
5
|
-
constructor(options
|
|
5
|
+
constructor(options?: T);
|
|
6
6
|
abstract run(container: IMidwayContainer): void | Promise<void>;
|
|
7
7
|
setExtraDetectorOptions(detectorOptions: T): void;
|
|
8
8
|
}
|
|
@@ -6,15 +6,13 @@ const glob_1 = require("@midwayjs/glob");
|
|
|
6
6
|
const error_1 = require("../error");
|
|
7
7
|
const constants_1 = require("../constants");
|
|
8
8
|
const decorator_1 = require("../decorator");
|
|
9
|
-
|
|
10
|
-
async function loadModule(modulePath, type) {
|
|
9
|
+
async function requireModule(modulePath, type) {
|
|
11
10
|
var _a;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return await (_a = modulePath, Promise.resolve().then(() => require(_a)));
|
|
11
|
+
if (type === 'commonjs') {
|
|
12
|
+
return require(modulePath);
|
|
15
13
|
}
|
|
16
14
|
else {
|
|
17
|
-
return
|
|
15
|
+
return await (_a = modulePath, Promise.resolve().then(() => require(_a)));
|
|
18
16
|
}
|
|
19
17
|
}
|
|
20
18
|
class AbstractFileDetector {
|
|
@@ -32,9 +30,6 @@ const DEFAULT_IGNORE_PATTERN = [
|
|
|
32
30
|
'**/logs/**',
|
|
33
31
|
'**/run/**',
|
|
34
32
|
'**/public/**',
|
|
35
|
-
'**/app/view/**',
|
|
36
|
-
'**/app/views/**',
|
|
37
|
-
'**/app/extend/**',
|
|
38
33
|
'**/node_modules/**',
|
|
39
34
|
'**/**.test.ts',
|
|
40
35
|
'**/**.test.js',
|
|
@@ -50,6 +45,7 @@ class CommonJSFileDetector extends AbstractFileDetector {
|
|
|
50
45
|
}
|
|
51
46
|
async run(container) {
|
|
52
47
|
var _a;
|
|
48
|
+
this.options = this.options || {};
|
|
53
49
|
const loadDirs = [].concat((_a = this.options.loadDir) !== null && _a !== void 0 ? _a : container.get('baseDir'));
|
|
54
50
|
for (const dir of loadDirs) {
|
|
55
51
|
const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
|
|
@@ -73,7 +69,7 @@ class CommonJSFileDetector extends AbstractFileDetector {
|
|
|
73
69
|
}
|
|
74
70
|
};
|
|
75
71
|
for (const file of fileResults) {
|
|
76
|
-
const exports = await
|
|
72
|
+
const exports = await requireModule(file, this.getType());
|
|
77
73
|
// add module to set
|
|
78
74
|
container.bindClass(exports, {
|
|
79
75
|
namespace: this.options.namespace,
|
|
@@ -239,7 +239,9 @@ class MidwayContainer {
|
|
|
239
239
|
// load configuration
|
|
240
240
|
const configuration = new ContainerConfiguration(this);
|
|
241
241
|
for (const mod of module) {
|
|
242
|
-
|
|
242
|
+
if (mod) {
|
|
243
|
+
configuration.load(mod);
|
|
244
|
+
}
|
|
243
245
|
}
|
|
244
246
|
for (const ns of configuration.getNamespaceList()) {
|
|
245
247
|
this.namespaceSet.add(ns);
|
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export { AsyncContextManager, ASYNC_ROOT_CONTEXT, AsyncContext, } from './common
|
|
|
33
33
|
export * from './decorator';
|
|
34
34
|
export * from './decorator/decoratorManager';
|
|
35
35
|
export * from './decorator/constant';
|
|
36
|
-
export { safelyGet, safeRequire, delegateTargetPrototypeMethod, delegateTargetMethod, delegateTargetProperties, delegateTargetAllPrototypeMethod, deprecatedOutput, transformRequestObjectByType, pathMatching, wrapMiddleware, wrapAsync, } from './util/';
|
|
36
|
+
export { safelyGet, safeRequire, loadModule, delegateTargetPrototypeMethod, delegateTargetMethod, delegateTargetProperties, delegateTargetAllPrototypeMethod, deprecatedOutput, transformRequestObjectByType, pathMatching, wrapMiddleware, wrapAsync, } from './util/';
|
|
37
37
|
export { extend } from './util/extend';
|
|
38
38
|
export * from './util/webRouterParam';
|
|
39
39
|
export * from './util/contextUtil';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.FORMAT = exports.FileUtils = exports.PathFileUtil = exports.Types = exports.Utils = exports.sleep = exports.retryWith = exports.retryWithAsync = exports.extend = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.ASYNC_ROOT_CONTEXT = exports.DataSourceManager = exports.WebRouterCollector = exports.MidwayServerlessFunctionService = exports.MidwayWebRouterService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.FunctionalConfiguration = exports.createConfiguration = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
17
|
+
exports.FORMAT = exports.FileUtils = exports.PathFileUtil = exports.Types = exports.Utils = exports.sleep = exports.retryWith = exports.retryWithAsync = exports.extend = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.loadModule = exports.safeRequire = exports.safelyGet = exports.ASYNC_ROOT_CONTEXT = exports.DataSourceManager = exports.WebRouterCollector = exports.MidwayServerlessFunctionService = exports.MidwayWebRouterService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.FunctionalConfiguration = exports.createConfiguration = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
18
18
|
__exportStar(require("./interface"), exports);
|
|
19
19
|
__exportStar(require("./context/container"), exports);
|
|
20
20
|
var requestContainer_1 = require("./context/requestContainer");
|
|
@@ -74,6 +74,7 @@ __exportStar(require("./decorator/constant"), exports);
|
|
|
74
74
|
var util_1 = require("./util/");
|
|
75
75
|
Object.defineProperty(exports, "safelyGet", { enumerable: true, get: function () { return util_1.safelyGet; } });
|
|
76
76
|
Object.defineProperty(exports, "safeRequire", { enumerable: true, get: function () { return util_1.safeRequire; } });
|
|
77
|
+
Object.defineProperty(exports, "loadModule", { enumerable: true, get: function () { return util_1.loadModule; } });
|
|
77
78
|
Object.defineProperty(exports, "delegateTargetPrototypeMethod", { enumerable: true, get: function () { return util_1.delegateTargetPrototypeMethod; } });
|
|
78
79
|
Object.defineProperty(exports, "delegateTargetMethod", { enumerable: true, get: function () { return util_1.delegateTargetMethod; } });
|
|
79
80
|
Object.defineProperty(exports, "delegateTargetProperties", { enumerable: true, get: function () { return util_1.delegateTargetProperties; } });
|
package/dist/setup.d.ts
CHANGED
|
@@ -5,9 +5,14 @@ import { MidwayContainer, IMidwayBootstrapOptions, IMidwayContainer } from './';
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function initializeGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): Promise<IMidwayContainer>;
|
|
7
7
|
export declare function destroyGlobalApplicationContext(applicationContext: IMidwayContainer): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* prepare applicationContext
|
|
10
|
+
* @param globalOptions
|
|
11
|
+
*/
|
|
12
|
+
export declare function prepareGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): Promise<IMidwayContainer | MidwayContainer>;
|
|
8
13
|
/**
|
|
9
14
|
* prepare applicationContext, it use in egg framework.
|
|
10
15
|
* @param globalOptions
|
|
11
16
|
*/
|
|
12
|
-
export declare function
|
|
17
|
+
export declare function prepareGlobalApplicationContextSync(globalOptions: IMidwayBootstrapOptions): IMidwayContainer | MidwayContainer;
|
|
13
18
|
//# sourceMappingURL=setup.d.ts.map
|
package/dist/setup.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.prepareGlobalApplicationContext = exports.destroyGlobalApplicationContext = exports.initializeGlobalApplicationContext = void 0;
|
|
3
|
+
exports.prepareGlobalApplicationContextSync = exports.prepareGlobalApplicationContext = exports.destroyGlobalApplicationContext = exports.initializeGlobalApplicationContext = void 0;
|
|
4
4
|
const _1 = require("./");
|
|
5
5
|
const config_default_1 = require("./config/config.default");
|
|
6
6
|
const decorator_1 = require("./decorator");
|
|
7
7
|
const util = require("util");
|
|
8
|
-
const path_1 = require("path");
|
|
9
8
|
const slsFunctionService_1 = require("./service/slsFunctionService");
|
|
10
|
-
const
|
|
9
|
+
const path_1 = require("path");
|
|
11
10
|
const debug = util.debuglog('midway:debug');
|
|
12
11
|
let stepIdx = 1;
|
|
13
12
|
function printStepDebugInfo(stepInfo) {
|
|
@@ -68,11 +67,120 @@ async function destroyGlobalApplicationContext(applicationContext) {
|
|
|
68
67
|
global['MIDWAY_MAIN_FRAMEWORK'] = undefined;
|
|
69
68
|
}
|
|
70
69
|
exports.destroyGlobalApplicationContext = destroyGlobalApplicationContext;
|
|
70
|
+
/**
|
|
71
|
+
* prepare applicationContext
|
|
72
|
+
* @param globalOptions
|
|
73
|
+
*/
|
|
74
|
+
async function prepareGlobalApplicationContext(globalOptions) {
|
|
75
|
+
var _a, _b, _c, _d, _e;
|
|
76
|
+
printStepDebugInfo('Ready to create applicationContext');
|
|
77
|
+
debug('[core]: start "initializeGlobalApplicationContext"');
|
|
78
|
+
debug(`[core]: bootstrap options = ${util.inspect(globalOptions)}`);
|
|
79
|
+
const appDir = (_a = globalOptions.appDir) !== null && _a !== void 0 ? _a : '';
|
|
80
|
+
const baseDir = (_b = globalOptions.baseDir) !== null && _b !== void 0 ? _b : '';
|
|
81
|
+
// new container
|
|
82
|
+
const applicationContext = (_c = globalOptions.applicationContext) !== null && _c !== void 0 ? _c : new _1.MidwayContainer();
|
|
83
|
+
// bind container to decoratorManager
|
|
84
|
+
debug('[core]: delegate module map from decoratorManager');
|
|
85
|
+
(0, decorator_1.bindContainer)(applicationContext);
|
|
86
|
+
global['MIDWAY_APPLICATION_CONTEXT'] = applicationContext;
|
|
87
|
+
// register baseDir and appDir
|
|
88
|
+
applicationContext.registerObject('baseDir', baseDir);
|
|
89
|
+
applicationContext.registerObject('appDir', appDir);
|
|
90
|
+
debug('[core]: set default file detector');
|
|
91
|
+
// set module detector
|
|
92
|
+
if (globalOptions.moduleDetector !== false) {
|
|
93
|
+
const pkgJSON = await (0, _1.loadModule)((0, path_1.join)(appDir, 'package.json'), {
|
|
94
|
+
safeLoad: true,
|
|
95
|
+
enableCache: false,
|
|
96
|
+
});
|
|
97
|
+
const loadMode = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
|
|
98
|
+
debug('[core]: module load mode = %s', loadMode);
|
|
99
|
+
// set default entry file
|
|
100
|
+
if (!globalOptions.imports) {
|
|
101
|
+
globalOptions.imports = [
|
|
102
|
+
await (0, _1.loadModule)((0, path_1.join)(baseDir, 'configuration'), {
|
|
103
|
+
loadMode,
|
|
104
|
+
safeLoad: true,
|
|
105
|
+
}),
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
if (globalOptions.moduleDetector === undefined) {
|
|
109
|
+
if (loadMode === 'esm') {
|
|
110
|
+
applicationContext.setFileDetector(new _1.ESModuleFileDetector({
|
|
111
|
+
loadDir: baseDir,
|
|
112
|
+
ignore: (_d = globalOptions.ignore) !== null && _d !== void 0 ? _d : [],
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
applicationContext.setFileDetector(new _1.CommonJSFileDetector({
|
|
117
|
+
loadDir: baseDir,
|
|
118
|
+
ignore: (_e = globalOptions.ignore) !== null && _e !== void 0 ? _e : [],
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
printStepDebugInfo('Binding inner service');
|
|
124
|
+
// bind inner service
|
|
125
|
+
applicationContext.bindClass(_1.MidwayEnvironmentService);
|
|
126
|
+
applicationContext.bindClass(_1.MidwayInformationService);
|
|
127
|
+
applicationContext.bindClass(_1.MidwayAspectService);
|
|
128
|
+
applicationContext.bindClass(_1.MidwayDecoratorService);
|
|
129
|
+
applicationContext.bindClass(_1.MidwayConfigService);
|
|
130
|
+
applicationContext.bindClass(_1.MidwayLoggerService);
|
|
131
|
+
applicationContext.bindClass(_1.MidwayApplicationManager);
|
|
132
|
+
applicationContext.bindClass(_1.MidwayFrameworkService);
|
|
133
|
+
applicationContext.bindClass(_1.MidwayMiddlewareService);
|
|
134
|
+
applicationContext.bindClass(_1.MidwayLifeCycleService);
|
|
135
|
+
applicationContext.bindClass(_1.MidwayMockService);
|
|
136
|
+
applicationContext.bindClass(_1.MidwayWebRouterService);
|
|
137
|
+
applicationContext.bindClass(slsFunctionService_1.MidwayServerlessFunctionService);
|
|
138
|
+
printStepDebugInfo('Binding preload module');
|
|
139
|
+
// bind preload module
|
|
140
|
+
if (globalOptions.preloadModules && globalOptions.preloadModules.length) {
|
|
141
|
+
for (const preloadModule of globalOptions.preloadModules) {
|
|
142
|
+
applicationContext.bindClass(preloadModule);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
printStepDebugInfo('Init MidwayConfigService, MidwayAspectService and MidwayDecoratorService');
|
|
146
|
+
// init default config
|
|
147
|
+
const configService = applicationContext.get(_1.MidwayConfigService);
|
|
148
|
+
configService.add([
|
|
149
|
+
{
|
|
150
|
+
default: config_default_1.default,
|
|
151
|
+
},
|
|
152
|
+
]);
|
|
153
|
+
// init aop support
|
|
154
|
+
applicationContext.get(_1.MidwayAspectService, [applicationContext]);
|
|
155
|
+
// init decorator service
|
|
156
|
+
applicationContext.get(_1.MidwayDecoratorService, [applicationContext]);
|
|
157
|
+
printStepDebugInfo('Load imports(component) and user code configuration module');
|
|
158
|
+
applicationContext.load([].concat(globalOptions.imports).concat(globalOptions.configurationModule));
|
|
159
|
+
printStepDebugInfo('Run applicationContext ready method');
|
|
160
|
+
// bind user code module
|
|
161
|
+
await applicationContext.ready();
|
|
162
|
+
if (globalOptions.globalConfig) {
|
|
163
|
+
if (Array.isArray(globalOptions.globalConfig)) {
|
|
164
|
+
configService.add(globalOptions.globalConfig);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
configService.addObject(globalOptions.globalConfig);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
printStepDebugInfo('Load config file');
|
|
171
|
+
// merge config
|
|
172
|
+
configService.load();
|
|
173
|
+
debug('[core]: Current config = %j', configService.getConfiguration());
|
|
174
|
+
// middleware support
|
|
175
|
+
applicationContext.get(_1.MidwayMiddlewareService, [applicationContext]);
|
|
176
|
+
return applicationContext;
|
|
177
|
+
}
|
|
178
|
+
exports.prepareGlobalApplicationContext = prepareGlobalApplicationContext;
|
|
71
179
|
/**
|
|
72
180
|
* prepare applicationContext, it use in egg framework.
|
|
73
181
|
* @param globalOptions
|
|
74
182
|
*/
|
|
75
|
-
function
|
|
183
|
+
function prepareGlobalApplicationContextSync(globalOptions) {
|
|
76
184
|
var _a, _b, _c, _d;
|
|
77
185
|
printStepDebugInfo('Ready to create applicationContext');
|
|
78
186
|
debug('[core]: start "initializeGlobalApplicationContext"');
|
|
@@ -141,31 +249,23 @@ function prepareGlobalApplicationContext(globalOptions) {
|
|
|
141
249
|
}
|
|
142
250
|
applicationContext.load([].concat(globalOptions.imports).concat(globalOptions.configurationModule));
|
|
143
251
|
printStepDebugInfo('Run applicationContext ready method');
|
|
144
|
-
function afterLoad() {
|
|
145
|
-
if (globalOptions.globalConfig) {
|
|
146
|
-
if (Array.isArray(globalOptions.globalConfig)) {
|
|
147
|
-
configService.add(globalOptions.globalConfig);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
configService.addObject(globalOptions.globalConfig);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
printStepDebugInfo('Load config file');
|
|
154
|
-
// merge config
|
|
155
|
-
configService.load();
|
|
156
|
-
debug('[core]: Current config = %j', configService.getConfiguration());
|
|
157
|
-
// middleware support
|
|
158
|
-
applicationContext.get(_1.MidwayMiddlewareService, [applicationContext]);
|
|
159
|
-
return applicationContext;
|
|
160
|
-
}
|
|
161
252
|
// bind user code module
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
253
|
+
applicationContext.ready();
|
|
254
|
+
if (globalOptions.globalConfig) {
|
|
255
|
+
if (Array.isArray(globalOptions.globalConfig)) {
|
|
256
|
+
configService.add(globalOptions.globalConfig);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
configService.addObject(globalOptions.globalConfig);
|
|
260
|
+
}
|
|
168
261
|
}
|
|
262
|
+
printStepDebugInfo('Load config file');
|
|
263
|
+
// merge config
|
|
264
|
+
configService.load();
|
|
265
|
+
debug('[core]: Current config = %j', configService.getConfiguration());
|
|
266
|
+
// middleware support
|
|
267
|
+
applicationContext.get(_1.MidwayMiddlewareService, [applicationContext]);
|
|
268
|
+
return applicationContext;
|
|
169
269
|
}
|
|
170
|
-
exports.
|
|
270
|
+
exports.prepareGlobalApplicationContextSync = prepareGlobalApplicationContextSync;
|
|
171
271
|
//# sourceMappingURL=setup.js.map
|
package/dist/util/index.d.ts
CHANGED
|
@@ -17,6 +17,17 @@ export declare const getCurrentEnvironment: () => string;
|
|
|
17
17
|
* @since 2.0.0
|
|
18
18
|
*/
|
|
19
19
|
export declare const safeRequire: (p: any, enabledCache?: boolean) => any;
|
|
20
|
+
/**
|
|
21
|
+
* load module, and it can be chosen commonjs or esm mode
|
|
22
|
+
* @param p
|
|
23
|
+
* @param options
|
|
24
|
+
* @since 3.12.0
|
|
25
|
+
*/
|
|
26
|
+
export declare const loadModule: (p: string, options?: {
|
|
27
|
+
enableCache?: boolean;
|
|
28
|
+
loadMode?: 'commonjs' | 'esm';
|
|
29
|
+
safeLoad?: boolean;
|
|
30
|
+
}) => Promise<any>;
|
|
20
31
|
/**
|
|
21
32
|
* @example
|
|
22
33
|
* safelyGet(['a','b'],{a: {b: 2}}) // => 2
|
package/dist/util/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Utils = exports.toAsyncFunction = exports.merge = exports.generateRandomId = exports.getParamNames = exports.sleep = exports.wrapAsync = exports.isIncludeProperty = exports.wrapMiddleware = exports.pathMatching = exports.toPathMatch = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.getCurrentDateString = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetPrototypeMethod = exports.joinURLPath = exports.getUserHome = exports.parsePrefix = exports.safelyGet = exports.safeRequire = exports.getCurrentEnvironment = exports.isDevelopmentEnvironment = void 0;
|
|
3
|
+
exports.Utils = exports.toAsyncFunction = exports.merge = exports.generateRandomId = exports.getParamNames = exports.sleep = exports.wrapAsync = exports.isIncludeProperty = exports.wrapMiddleware = exports.pathMatching = exports.toPathMatch = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.getCurrentDateString = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetPrototypeMethod = exports.joinURLPath = exports.getUserHome = exports.parsePrefix = exports.safelyGet = exports.loadModule = exports.safeRequire = exports.getCurrentEnvironment = exports.isDevelopmentEnvironment = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const util_1 = require("util");
|
|
@@ -54,6 +54,48 @@ const safeRequire = (p, enabledCache = true) => {
|
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
exports.safeRequire = safeRequire;
|
|
57
|
+
/**
|
|
58
|
+
* load module, and it can be chosen commonjs or esm mode
|
|
59
|
+
* @param p
|
|
60
|
+
* @param options
|
|
61
|
+
* @since 3.12.0
|
|
62
|
+
*/
|
|
63
|
+
const loadModule = async (p, options = {}) => {
|
|
64
|
+
var _a;
|
|
65
|
+
var _b, _c, _d;
|
|
66
|
+
options.enableCache = (_b = options.enableCache) !== null && _b !== void 0 ? _b : true;
|
|
67
|
+
options.safeLoad = (_c = options.safeLoad) !== null && _c !== void 0 ? _c : false;
|
|
68
|
+
options.loadMode = (_d = options.loadMode) !== null && _d !== void 0 ? _d : 'commonjs';
|
|
69
|
+
if (p.startsWith(`.${path_1.sep}`) || p.startsWith(`..${path_1.sep}`)) {
|
|
70
|
+
p = (0, path_1.resolve)((0, path_1.dirname)(module.parent.filename), p);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
if (options.enableCache) {
|
|
74
|
+
if (options.loadMode === 'commonjs') {
|
|
75
|
+
return require(p);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return await (_a = p, Promise.resolve().then(() => require(_a)));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const content = (0, fs_1.readFileSync)(p, {
|
|
83
|
+
encoding: 'utf-8',
|
|
84
|
+
});
|
|
85
|
+
return JSON.parse(content);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
if (!options.safeLoad) {
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
debug(`[core]: SafeLoadModule Warning\n\n${err.message}\n`);
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
exports.loadModule = loadModule;
|
|
57
99
|
/**
|
|
58
100
|
* @example
|
|
59
101
|
* safelyGet(['a','b'],{a: {b: 2}}) // => 2
|