Skip to content

Commit 9b3f65f

Browse files
committed
🎨 迁移 server config 至 cli 等.
1 parent abc2bd4 commit 9b3f65f

File tree

10 files changed

+982
-47
lines changed

10 files changed

+982
-47
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ npx micro-app show methods
203203
* onPluginInitDone ( System Build-in )
204204
* beforeMergeConfig ( System Build-in )
205205
* afterMergeConfig ( System Build-in )
206-
* beforeMergeServerConfig ( System Build-in )
207-
* afterMergeServerConfig ( System Build-in )
206+
* modifyDefaultServerConfig ( System Build-in )
208207
* onInitWillDone ( System Build-in )
209208
* onInitDone ( System Build-in )
210209
* modifyCommand ( System Build-in )

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"name": "@micro-app/cli",
3-
"version": "0.2.0-beta.2",
3+
"version": "0.2.0-beta.3",
44
"description": "[CLI] Pluggable micro application framework.",
55
"bin": {
66
"micro-app": "./bin/micro-app.js",
77
"micro-app-dev": "./bin/micro-app-dev.js",
88
"micro-app-start": "./bin/micro-app-start.js"
99
},
1010
"scripts": {
11+
"lint": "eslint .",
12+
"lint:fix": "npm run lint -- --fix",
1113
"test": "jest"
1214
},
1315
"homepage": "https://github.com/MicrosApp/MicroApp-CLI",
@@ -35,22 +37,24 @@
3537
},
3638
"license": "MIT",
3739
"devDependencies": {
38-
"@micro-app/core": "^0.2.0-beta.1",
40+
"@micro-app/core": "^0.2.0-beta.4",
3941
"@micro-app/plugin-koa-static-server": "^0.0.1",
4042
"@micro-app/plugin-koa-webpack-middleware": "^0.0.3",
4143
"@types/jest": "^24.0.18",
4244
"babel-eslint": "^10.0.3",
43-
"coveralls": "^3.0.6",
45+
"coveralls": "^3.0.7",
4446
"eslint": "^5.16.0",
4547
"eslint-config-2o3t": "^1.1.17",
48+
"husky": "^3.0.8",
4649
"jest": "^24.9.0",
50+
"lint-staged": "^9.4.2",
4751
"webpack": "^4.39.2"
4852
},
4953
"peerDependencies": {
5054
"@micro-app/core": ">=0.2.0"
5155
},
5256
"dependencies": {
53-
"koa": "^2.8.1",
57+
"koa": "^2.10.0",
5458
"shelljs": "^0.8.3",
5559
"update-notifier": "^3.0.1",
5660
"yargs-parser": "^13.1.1"

plugins/commands/start/start.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
describe('Command start', () => {
66

7-
let PORTS = 10000;
7+
let PORTS = 22000;
88
function getArgvs() {
99
const port = PORTS++;
1010
return { _: [], port };

plugins/extends/server/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
module.exports = function extendServer(api, opts) {
4+
5+
api.assertVersion('>=0.2.0');
6+
7+
const registerMethods = require('./methods');
8+
registerMethods(api);
9+
10+
const requireMicro = require('@micro-app/core');
11+
const _ = require('lodash');
12+
13+
const logger = api.logger;
14+
15+
api.extendConfig('selfServerConfig', {
16+
cache: true,
17+
description: '当前工程下的服务配置',
18+
}, function() {
19+
return api.self.toServerConfig(true);
20+
});
21+
22+
api.extendConfig('microsServerConfig', {
23+
cache: true,
24+
description: '当前工程下所有依赖的服务配置合集',
25+
}, function() {
26+
const _self = this.self;
27+
const config = {};
28+
const micros = _.cloneDeep([ ...this.micros ]);
29+
micros.forEach(key => {
30+
const microConfig = requireMicro(key);
31+
if (microConfig) {
32+
config[key] = microConfig.toServerConfig(true);
33+
} else {
34+
this.micros.delete(key);
35+
logger.error(`Not Found micros: "${key}"`);
36+
}
37+
});
38+
config[_self.key] = api.selfServerConfig || _self.toServerConfig(true);
39+
return config;
40+
});
41+
42+
api.extendConfig('serverConfig', {
43+
description: '当前工程下服务端配置集合',
44+
}, function() {
45+
return api.applyPluginHooks('modifyDefaultServerConfig', {});
46+
});
47+
48+
// merge server config
49+
api.onInitWillDone(() => {
50+
const serverMerge = require('../../../src/utils/merge-server');
51+
const serverHooksMerge = require('../../../src/utils/merge-server-hooks');
52+
api.modifyDefaultServerConfig(_serverConfig => {
53+
const selfServerConfig = api.selfServerConfig;
54+
const microsServerConfig = api.microsServerConfig;
55+
const micros = api.micros;
56+
const serverEntrys = serverMerge(...micros.map(key => microsServerConfig[key]), selfServerConfig);
57+
const serverHooks = serverHooksMerge(...micros.map(key => microsServerConfig[key]), selfServerConfig);
58+
return Object.assign(_serverConfig, {
59+
..._.pick(selfServerConfig, [
60+
'host',
61+
'port',
62+
]),
63+
entrys: serverEntrys,
64+
hooks: serverHooks,
65+
});
66+
});
67+
});
68+
};

plugins/extends/server/methods.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
// 'modifyDefaultServerConfig',
4+
5+
module.exports = api => {
6+
7+
api.registerMethod('modifyDefaultServerConfig', {
8+
type: api.API_TYPE.MODIFY,
9+
description: '修改服务端配置合并事件',
10+
});
11+
12+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
/* global expect */
4+
5+
describe('server', () => {
6+
7+
it('server adapter', () => {
8+
const { service } = require('../../../bin/base');
9+
10+
const plugin = service.plugins.find(item => item.id === 'cli:plugins-extends-server');
11+
expect(typeof plugin).toEqual('object');
12+
13+
service.init();
14+
15+
expect(plugin._api).not.toBeUndefined();
16+
17+
expect(plugin._api.selfConfig).not.toBeNull();
18+
expect(plugin._api.selfConfig).not.toBeUndefined();
19+
20+
expect(plugin._api.selfServerConfig).not.toBeNull();
21+
expect(plugin._api.selfServerConfig).not.toBeUndefined();
22+
23+
expect(plugin._api.microsServerConfig).not.toBeNull();
24+
expect(plugin._api.microsServerConfig).not.toBeUndefined();
25+
});
26+
27+
});

plugins/register.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
const path = require('path');
44

5+
const extendConfigs = [
6+
{
7+
name: 'server',
8+
description: '针对服务信息进行配置扩展.',
9+
},
10+
];
11+
512
const commands = [
613
{
714
name: 'version',
@@ -27,6 +34,16 @@ const commands = [
2734

2835
module.exports = function(service) {
2936

37+
extendConfigs.forEach(item => {
38+
const name = item.name;
39+
const description = item.description;
40+
service.registerPlugin({
41+
id: `cli:plugins-extends-${name}`,
42+
link: path.resolve(__dirname, './extends', name),
43+
description,
44+
});
45+
});
46+
3047
commands.forEach(item => {
3148
const name = item.name;
3249
const description = item.description;

src/utils/merge-server-hooks.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
const tryRequire = require('try-require');
4+
const path = require('path');
5+
const fs = require('fs-extra');
6+
7+
function adapter(microConfig) {
8+
const microServers = [];
9+
const root = microConfig.root;
10+
const { hooks, options = {}, info } = microConfig;
11+
if (hooks) {
12+
const hooksFile = path.resolve(root, hooks);
13+
if (fs.statSync(hooksFile).isDirectory()) {
14+
const hookFuncs = [];
15+
fs.readdirSync(hooksFile).forEach(filename => {
16+
const fp = path.resolve(hooksFile, filename);
17+
const hooksCallback = tryRequire.resolve(fp);
18+
if (hooksCallback && typeof hooksCallback === 'string') {
19+
hookFuncs.push({
20+
key: filename.replace(/\.js$/, ''),
21+
value: hooksCallback,
22+
});
23+
}
24+
});
25+
if (hookFuncs.length) {
26+
microServers.push({
27+
link: hookFuncs.reduce((obj, item) => {
28+
obj[item.key] = item.value;
29+
return obj;
30+
}, {}),
31+
options,
32+
info,
33+
});
34+
}
35+
} else {
36+
const hooksCallback = tryRequire.resolve(hooksFile);
37+
if (hooksCallback && typeof hooksCallback === 'string') {
38+
microServers.push({
39+
link: hooksCallback,
40+
options,
41+
info,
42+
});
43+
}
44+
}
45+
}
46+
return microServers;
47+
}
48+
49+
function serverHooksMerge(...microConfigs) {
50+
if (!microConfigs || microConfigs.length <= 0) {
51+
return [];
52+
}
53+
const microServers = [];
54+
microConfigs.forEach(microConfig => {
55+
if (microConfig) {
56+
microServers.push(...adapter(microConfig));
57+
}
58+
});
59+
return microServers;
60+
}
61+
62+
serverHooksMerge.adapter = adapter;
63+
module.exports = serverHooksMerge;

src/utils/merge-server.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const tryRequire = require('try-require');
4+
const path = require('path');
5+
6+
function adapter(microConfig) {
7+
const microServers = [];
8+
const root = microConfig.root;
9+
const { entry, options = {}, info } = microConfig;
10+
if (entry) {
11+
const entryFile = path.resolve(root, entry);
12+
const entryCallback = tryRequire.resolve(entryFile);
13+
if (entryCallback && typeof entryCallback === 'string') {
14+
microServers.push({
15+
link: entryCallback,
16+
options,
17+
info,
18+
});
19+
}
20+
}
21+
return microServers;
22+
}
23+
24+
function serverMerge(...microConfigs) {
25+
if (!microConfigs || microConfigs.length <= 0) {
26+
return [];
27+
}
28+
const microServers = [];
29+
microConfigs.forEach(microConfig => {
30+
microServers.push(...adapter(microConfig));
31+
});
32+
return microServers;
33+
}
34+
35+
serverMerge.adapter = adapter;
36+
module.exports = serverMerge;

0 commit comments

Comments
 (0)