@midwayjs/mock 3.11.15 → 3.12.0
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.js +33 -0
- package/dist/creator.js +77 -29
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +5 -2
- package/function.js +33 -0
- package/package.json +9 -7
package/app.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const { createApp, close } = require('./dist');
|
|
2
|
+
const { join } = require('path');
|
|
3
|
+
|
|
4
|
+
(async () => {
|
|
5
|
+
process.env.MIDWAY_TS_MODE = 'false';
|
|
6
|
+
// 查找 process.argv 中的 --port 参数
|
|
7
|
+
const portIndex = process.argv.findIndex((item) => item === '--port');
|
|
8
|
+
if (portIndex !== -1) {
|
|
9
|
+
process.env.MIDWAY_HTTP_PORT = process.argv[portIndex + 1];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
process.once('SIGINT', onSignal);
|
|
13
|
+
// kill(3) Ctrl-\
|
|
14
|
+
process.once('SIGQUIT', onSignal);
|
|
15
|
+
// kill(15) default
|
|
16
|
+
process.once('SIGTERM', onSignal);
|
|
17
|
+
|
|
18
|
+
const app = await createApp({
|
|
19
|
+
appDir: process.cwd(),
|
|
20
|
+
baseDir: join(process.cwd(), 'dist'),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
process.send({
|
|
24
|
+
title: 'server-ready',
|
|
25
|
+
port: process.env.MIDWAY_HTTP_PORT,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function onSignal() {
|
|
29
|
+
close(app).then(() => {
|
|
30
|
+
process.exit(0);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
})();
|
package/dist/creator.js
CHANGED
|
@@ -20,13 +20,17 @@ function formatPath(baseDir, p) {
|
|
|
20
20
|
return (0, path_1.resolve)(baseDir, p);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
+
function getFileNameWithSuffix(fileName) {
|
|
24
|
+
return (0, core_1.isTypeScriptEnvironment)() ? `${fileName}.ts` : `${fileName}.js`;
|
|
25
|
+
}
|
|
23
26
|
async function create(appDir, options = {}, customFramework) {
|
|
24
|
-
|
|
25
|
-
process.env.MIDWAY_TS_MODE = 'true';
|
|
27
|
+
var _a;
|
|
28
|
+
process.env.MIDWAY_TS_MODE = (_a = process.env.MIDWAY_TS_MODE) !== null && _a !== void 0 ? _a : 'true';
|
|
26
29
|
if (typeof appDir === 'object') {
|
|
27
30
|
options = appDir;
|
|
28
31
|
appDir = options.appDir || '';
|
|
29
32
|
}
|
|
33
|
+
debug(`[mock]: Create app, appDir="${appDir}"`);
|
|
30
34
|
try {
|
|
31
35
|
if (appDir) {
|
|
32
36
|
// 处理测试的 fixtures
|
|
@@ -42,13 +46,6 @@ async function create(appDir, options = {}, customFramework) {
|
|
|
42
46
|
}
|
|
43
47
|
(0, logger_1.clearAllLoggers)();
|
|
44
48
|
options = options || {};
|
|
45
|
-
if (options.baseDir) {
|
|
46
|
-
(0, core_1.safeRequire)((0, path_1.join)(`${options.baseDir}`, 'interface'));
|
|
47
|
-
}
|
|
48
|
-
else if (appDir) {
|
|
49
|
-
options.baseDir = `${appDir}/src`;
|
|
50
|
-
(0, core_1.safeRequire)((0, path_1.join)(`${options.baseDir}`, 'interface'));
|
|
51
|
-
}
|
|
52
49
|
if (options.entryFile) {
|
|
53
50
|
// start from entry file, like bootstrap.js
|
|
54
51
|
options.entryFile = formatPath(appDir, options.entryFile);
|
|
@@ -73,8 +70,28 @@ async function create(appDir, options = {}, customFramework) {
|
|
|
73
70
|
});
|
|
74
71
|
return;
|
|
75
72
|
}
|
|
73
|
+
if (!options.moduleLoadType) {
|
|
74
|
+
const pkgJSON = await (0, core_1.loadModule)((0, path_1.join)(appDir, 'package.json'), {
|
|
75
|
+
safeLoad: true,
|
|
76
|
+
enableCache: false,
|
|
77
|
+
});
|
|
78
|
+
options.moduleLoadType = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
|
|
79
|
+
}
|
|
80
|
+
if (options.baseDir) {
|
|
81
|
+
await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
|
|
82
|
+
safeLoad: true,
|
|
83
|
+
loadMode: options.moduleLoadType,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (appDir) {
|
|
87
|
+
options.baseDir = `${appDir}/src`;
|
|
88
|
+
await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
|
|
89
|
+
safeLoad: true,
|
|
90
|
+
loadMode: options.moduleLoadType,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
76
93
|
if (!options.imports && customFramework) {
|
|
77
|
-
options.imports = (0, utils_1.transformFrameworkToConfiguration)(customFramework);
|
|
94
|
+
options.imports = await (0, utils_1.transformFrameworkToConfiguration)(customFramework, options.moduleLoadType);
|
|
78
95
|
}
|
|
79
96
|
if (customFramework === null || customFramework === void 0 ? void 0 : customFramework['Configuration']) {
|
|
80
97
|
options.imports = customFramework;
|
|
@@ -125,10 +142,11 @@ async function create(appDir, options = {}, customFramework) {
|
|
|
125
142
|
...options,
|
|
126
143
|
appDir,
|
|
127
144
|
asyncContextManager: (0, async_hooks_context_manager_1.createContextManager)(),
|
|
128
|
-
imports: []
|
|
129
|
-
.
|
|
130
|
-
|
|
131
|
-
|
|
145
|
+
imports: [].concat(options.imports).concat(options.baseDir
|
|
146
|
+
? await (0, core_1.loadModule)((0, path_1.join)(options.baseDir, getFileNameWithSuffix('configuration')), {
|
|
147
|
+
safeLoad: true,
|
|
148
|
+
loadMode: options.moduleLoadType,
|
|
149
|
+
})
|
|
132
150
|
: []),
|
|
133
151
|
});
|
|
134
152
|
if (customFramework) {
|
|
@@ -141,7 +159,7 @@ async function create(appDir, options = {}, customFramework) {
|
|
|
141
159
|
return mainFramework;
|
|
142
160
|
}
|
|
143
161
|
else {
|
|
144
|
-
throw new Error(
|
|
162
|
+
throw new Error(`Can not get main framework, please check your ${getFileNameWithSuffix('configuration')}.`);
|
|
145
163
|
}
|
|
146
164
|
}
|
|
147
165
|
}
|
|
@@ -192,7 +210,8 @@ async function close(app, options) {
|
|
|
192
210
|
}
|
|
193
211
|
exports.close = close;
|
|
194
212
|
async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
|
|
195
|
-
var _a, _b, _c, _d;
|
|
213
|
+
var _a, _b, _c, _d, _e;
|
|
214
|
+
process.env.MIDWAY_TS_MODE = (_a = process.env.MIDWAY_TS_MODE) !== null && _a !== void 0 ? _a : 'true';
|
|
196
215
|
if (typeof baseDir === 'object') {
|
|
197
216
|
options = baseDir;
|
|
198
217
|
baseDir = options.appDir || '';
|
|
@@ -211,9 +230,9 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
|
|
|
211
230
|
// load yaml
|
|
212
231
|
try {
|
|
213
232
|
const doc = yaml.load((0, fs_1.readFileSync)((0, path_1.join)(baseDir, 'f.yml'), 'utf8'));
|
|
214
|
-
starterName = (
|
|
233
|
+
starterName = (_b = doc === null || doc === void 0 ? void 0 : doc['provider']) === null || _b === void 0 ? void 0 : _b['starter'];
|
|
215
234
|
if (starterName) {
|
|
216
|
-
const m = (0, core_1.
|
|
235
|
+
const m = await (0, core_1.loadModule)(starterName);
|
|
217
236
|
if (m && m['BootstrapStarter']) {
|
|
218
237
|
options.starter = new m['BootstrapStarter']();
|
|
219
238
|
}
|
|
@@ -227,7 +246,6 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
|
|
|
227
246
|
if (options.starter) {
|
|
228
247
|
options.appDir = baseDir;
|
|
229
248
|
debug(`[mock]: Create app, appDir="${options.appDir}"`);
|
|
230
|
-
process.env.MIDWAY_TS_MODE = 'true';
|
|
231
249
|
if (options.appDir) {
|
|
232
250
|
// 处理测试的 fixtures
|
|
233
251
|
if (!(0, path_1.isAbsolute)(options.appDir)) {
|
|
@@ -238,13 +256,24 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
|
|
|
238
256
|
}
|
|
239
257
|
}
|
|
240
258
|
(0, logger_1.clearAllLoggers)();
|
|
259
|
+
const pkgJSON = await (0, core_1.loadModule)((0, path_1.join)(options.appDir, 'package.json'), {
|
|
260
|
+
safeLoad: true,
|
|
261
|
+
enableCache: false,
|
|
262
|
+
});
|
|
263
|
+
options.moduleLoadType = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
|
|
241
264
|
options = options || {};
|
|
242
265
|
if (options.baseDir) {
|
|
243
|
-
(0, core_1.
|
|
266
|
+
await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
|
|
267
|
+
safeLoad: true,
|
|
268
|
+
loadMode: options.moduleLoadType,
|
|
269
|
+
});
|
|
244
270
|
}
|
|
245
271
|
else if (options.appDir) {
|
|
246
272
|
options.baseDir = `${options.appDir}/src`;
|
|
247
|
-
(0, core_1.
|
|
273
|
+
await (0, core_1.loadModule)((0, path_1.join)(`${options.baseDir}`, getFileNameWithSuffix('interface')), {
|
|
274
|
+
safeLoad: true,
|
|
275
|
+
loadMode: options.moduleLoadType,
|
|
276
|
+
});
|
|
248
277
|
}
|
|
249
278
|
// new mode
|
|
250
279
|
const exports = options.starter.start(options);
|
|
@@ -255,8 +284,8 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
|
|
|
255
284
|
const framework = frameworkService.getMainFramework();
|
|
256
285
|
const appManager = appCtx.get(core_1.MidwayApplicationManager);
|
|
257
286
|
const app = appManager.getApplication(core_1.MidwayFrameworkType.FAAS);
|
|
258
|
-
const faasConfig = (
|
|
259
|
-
const customPort = (
|
|
287
|
+
const faasConfig = (_c = configService.getConfiguration('faas')) !== null && _c !== void 0 ? _c : {};
|
|
288
|
+
const customPort = (_e = (_d = process.env.MIDWAY_HTTP_PORT) !== null && _d !== void 0 ? _d : faasConfig['port']) !== null && _e !== void 0 ? _e : options['port'];
|
|
260
289
|
if (options.starter.callback2) {
|
|
261
290
|
app.callback2 = options.starter.callback2.bind(options.starter);
|
|
262
291
|
}
|
|
@@ -364,7 +393,7 @@ async function createFunctionApp(baseDir, options = {}, customFrameworkModule) {
|
|
|
364
393
|
'@ali/serverless-app',
|
|
365
394
|
'@midwayjs/serverless-app',
|
|
366
395
|
]);
|
|
367
|
-
const serverlessModule = (0, utils_1.transformFrameworkToConfiguration)(customFramework);
|
|
396
|
+
const serverlessModule = await (0, utils_1.transformFrameworkToConfiguration)(customFramework, options.moduleLoadType);
|
|
368
397
|
if (serverlessModule) {
|
|
369
398
|
if (options && options.imports) {
|
|
370
399
|
options.imports.unshift(serverlessModule);
|
|
@@ -401,6 +430,9 @@ class LightFramework extends core_1.BaseFramework {
|
|
|
401
430
|
}
|
|
402
431
|
}
|
|
403
432
|
class BootstrapAppStarter {
|
|
433
|
+
constructor(options) {
|
|
434
|
+
this.options = options;
|
|
435
|
+
}
|
|
404
436
|
getApp(type) {
|
|
405
437
|
const applicationContext = (0, core_1.getCurrentApplicationContext)();
|
|
406
438
|
const applicationManager = applicationContext.get(core_1.MidwayApplicationManager);
|
|
@@ -408,7 +440,10 @@ class BootstrapAppStarter {
|
|
|
408
440
|
}
|
|
409
441
|
async close(options = {}) {
|
|
410
442
|
// eslint-disable-next-line node/no-extraneous-require
|
|
411
|
-
const BootstrapModule = (0, core_1.
|
|
443
|
+
const BootstrapModule = await (0, core_1.loadModule)('@midwayjs/bootstrap', {
|
|
444
|
+
loadMode: this.options.moduleLoadType,
|
|
445
|
+
safeLoad: true,
|
|
446
|
+
});
|
|
412
447
|
if (BootstrapModule === null || BootstrapModule === void 0 ? void 0 : BootstrapModule.Bootstrap) {
|
|
413
448
|
await BootstrapModule.Bootstrap.stop();
|
|
414
449
|
}
|
|
@@ -436,19 +471,32 @@ async function createLightApp(baseDir = '', options = {}) {
|
|
|
436
471
|
},
|
|
437
472
|
},
|
|
438
473
|
}, (_a = options.globalConfig) !== null && _a !== void 0 ? _a : {});
|
|
474
|
+
if (!options.moduleLoadType) {
|
|
475
|
+
const cwd = process.cwd();
|
|
476
|
+
const pkgJSON = await (0, core_1.loadModule)((0, path_1.join)(cwd, 'package.json'), {
|
|
477
|
+
safeLoad: true,
|
|
478
|
+
enableCache: false,
|
|
479
|
+
});
|
|
480
|
+
options.moduleLoadType = (pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.type) === 'module' ? 'esm' : 'commonjs';
|
|
481
|
+
}
|
|
439
482
|
return createApp(baseDir, {
|
|
440
483
|
...options,
|
|
441
|
-
imports: [
|
|
484
|
+
imports: [
|
|
485
|
+
await (0, utils_1.transformFrameworkToConfiguration)(LightFramework, options.moduleLoadType),
|
|
486
|
+
].concat(options === null || options === void 0 ? void 0 : options.imports),
|
|
442
487
|
});
|
|
443
488
|
}
|
|
444
489
|
exports.createLightApp = createLightApp;
|
|
445
490
|
async function createBootstrap(entryFile, options = {}) {
|
|
491
|
+
const cwd = process.cwd();
|
|
446
492
|
if (!options.bootstrapMode) {
|
|
447
|
-
options.bootstrapMode = (0,
|
|
493
|
+
options.bootstrapMode = (0, fs_1.existsSync)((0, path_1.join)(cwd, 'node_modules/@midwayjs/faas'))
|
|
494
|
+
? 'faas'
|
|
495
|
+
: 'app';
|
|
448
496
|
}
|
|
449
497
|
if (options.bootstrapMode === 'faas') {
|
|
450
498
|
options.entryFile = entryFile;
|
|
451
|
-
const app = await createFunctionApp(
|
|
499
|
+
const app = await createFunctionApp(cwd, options);
|
|
452
500
|
return {
|
|
453
501
|
close: async () => {
|
|
454
502
|
return close(app);
|
|
@@ -459,7 +507,7 @@ async function createBootstrap(entryFile, options = {}) {
|
|
|
459
507
|
await create(undefined, {
|
|
460
508
|
entryFile,
|
|
461
509
|
});
|
|
462
|
-
return new BootstrapAppStarter();
|
|
510
|
+
return new BootstrapAppStarter(options);
|
|
463
511
|
}
|
|
464
512
|
}
|
|
465
513
|
exports.createBootstrap = createBootstrap;
|
package/dist/utils.d.ts
CHANGED
|
@@ -7,10 +7,10 @@ export declare function findFirstExistModule(moduleList: any): ComponentModule;
|
|
|
7
7
|
* transform a framework component or framework module to configuration class
|
|
8
8
|
* @param Framework
|
|
9
9
|
*/
|
|
10
|
-
export declare function transformFrameworkToConfiguration<T extends IMidwayFramework<any, any, any>>(Framework: any): {
|
|
10
|
+
export declare function transformFrameworkToConfiguration<T extends IMidwayFramework<any, any, any>>(Framework: any, loadMode: 'commonjs' | 'esm'): Promise<{
|
|
11
11
|
[key: string]: any;
|
|
12
12
|
Configuration: any;
|
|
13
|
-
}
|
|
13
|
+
}>;
|
|
14
14
|
export declare function removeFile(file: string): Promise<void>;
|
|
15
15
|
export declare function mergeGlobalConfig(globalConfig: any, newConfigObject: Record<string, any>): any;
|
|
16
16
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
CHANGED
|
@@ -39,12 +39,15 @@ exports.findFirstExistModule = findFirstExistModule;
|
|
|
39
39
|
* transform a framework component or framework module to configuration class
|
|
40
40
|
* @param Framework
|
|
41
41
|
*/
|
|
42
|
-
function transformFrameworkToConfiguration(Framework) {
|
|
42
|
+
async function transformFrameworkToConfiguration(Framework, loadMode) {
|
|
43
43
|
if (!Framework)
|
|
44
44
|
return null;
|
|
45
45
|
let CustomFramework = Framework;
|
|
46
46
|
if (typeof Framework === 'string') {
|
|
47
|
-
Framework = (0, core_1.
|
|
47
|
+
Framework = await (0, core_1.loadModule)(Framework, {
|
|
48
|
+
loadMode,
|
|
49
|
+
safeLoad: true,
|
|
50
|
+
});
|
|
48
51
|
}
|
|
49
52
|
if (Framework.Configuration) {
|
|
50
53
|
return Framework;
|
package/function.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const { createFunctionApp, close } = require('./dist');
|
|
2
|
+
const { join } = require('path');
|
|
3
|
+
|
|
4
|
+
(async () => {
|
|
5
|
+
process.env.MIDWAY_TS_MODE = 'false';
|
|
6
|
+
// 查找 process.argv 中的 --port 参数
|
|
7
|
+
const portIndex = process.argv.findIndex(item => item === '--port');
|
|
8
|
+
if (portIndex !== -1) {
|
|
9
|
+
process.env.MIDWAY_HTTP_PORT = process.argv[portIndex + 1];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
process.once('SIGINT', onSignal);
|
|
13
|
+
// kill(3) Ctrl-\
|
|
14
|
+
process.once('SIGQUIT', onSignal);
|
|
15
|
+
// kill(15) default
|
|
16
|
+
process.once('SIGTERM', onSignal);
|
|
17
|
+
|
|
18
|
+
const app = await createFunctionApp({
|
|
19
|
+
appDir: process.cwd(),
|
|
20
|
+
baseDir: join(process.cwd(), 'dist'),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
process.send({
|
|
24
|
+
title: 'server-ready',
|
|
25
|
+
port: process.env.MIDWAY_HTTP_PORT,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function onSignal() {
|
|
29
|
+
close(app).then(() => {
|
|
30
|
+
process.exit(0);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/mock",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.12.0",
|
|
4
4
|
"description": "create your test app from midway framework",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -20,24 +20,26 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"dist/**/*.js",
|
|
22
22
|
"dist/**/*.d.ts",
|
|
23
|
-
"ssl"
|
|
23
|
+
"ssl",
|
|
24
|
+
"app.js",
|
|
25
|
+
"function.js"
|
|
24
26
|
],
|
|
25
27
|
"engines": {
|
|
26
28
|
"node": ">=12"
|
|
27
29
|
},
|
|
28
30
|
"license": "MIT",
|
|
29
31
|
"devDependencies": {
|
|
30
|
-
"@midwayjs/core": "^3.
|
|
32
|
+
"@midwayjs/core": "^3.12.0",
|
|
31
33
|
"@midwayjs/logger": "^2.15.0",
|
|
32
34
|
"@types/amqplib": "0.10.1",
|
|
33
35
|
"amqplib": "0.10.3",
|
|
34
36
|
"kafkajs": "2.2.4",
|
|
35
|
-
"socket.io": "4.7.
|
|
36
|
-
"socket.io-client": "4.7.
|
|
37
|
+
"socket.io": "4.7.2",
|
|
38
|
+
"socket.io-client": "4.7.2",
|
|
37
39
|
"ws": "8.13.0"
|
|
38
40
|
},
|
|
39
41
|
"dependencies": {
|
|
40
|
-
"@midwayjs/async-hooks-context-manager": "^3.
|
|
42
|
+
"@midwayjs/async-hooks-context-manager": "^3.12.0",
|
|
41
43
|
"@types/superagent": "4.1.14",
|
|
42
44
|
"@types/supertest": "2.0.12",
|
|
43
45
|
"js-yaml": "4.1.0",
|
|
@@ -49,5 +51,5 @@
|
|
|
49
51
|
"type": "git",
|
|
50
52
|
"url": "https://github.com/midwayjs/midway.git"
|
|
51
53
|
},
|
|
52
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "0e9a08cd078c4c4dedfa753a8c1025806cc0b0a2"
|
|
53
55
|
}
|