Skip to content

Commit 4b8f73a

Browse files
authored
Merge pull request #17 from MicrosApp/develop
Develop
2 parents 37ae154 + 72c16dc commit 4b8f73a

31 files changed

+175
-2373
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @zyao89

libs/Config/base/BaseConfig.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ const fs = require('fs-extra');
55
const tryRequire = require('try-require');
66
const _ = require('lodash');
77

8+
const { getPadLength } = require('@micro-app/shared-utils');
9+
810
const Symbols = require('../../Constants/symbols');
911
const CONSTANTS = require('../../Constants');
1012
const logger = require('../../../src/utils/logger');
11-
const getPadLength = require('../../../src/utils/getPadLength');
1213

1314
// 默认配置
1415
// const DEFAULT_CONFIG = require('../../Constants/default');
@@ -17,7 +18,9 @@ const validate = require('../schema');
1718
const SCHEMA = require('../schema/configSchema');
1819

1920
const INIT = Symbol('@BaseConfig#INIT');
20-
const ORIGNAL_CONFIG = Symbol('@BaseConfig#ORIGNAL_CONFIG');
21+
const KEY_ORIGNAL_CONFIG = Symbol('@BaseConfig#KEY_ORIGNAL_CONFIG');
22+
const KEY_PACKAGE = Symbol('@BaseConfig#KEY_PACKAGE');
23+
const KEY_PACKAGE_PATH = Symbol('@BaseConfig#KEY_PACKAGE_PATH');
2124

2225
class BaseConfig {
2326

@@ -29,7 +32,7 @@ class BaseConfig {
2932
constructor(config /* , opts = {} */) {
3033
// 校验 config
3134
this._validateSchema(config);
32-
this[ORIGNAL_CONFIG] = config;
35+
this[KEY_ORIGNAL_CONFIG] = config;
3336
this[INIT]();
3437
}
3538

@@ -49,29 +52,32 @@ class BaseConfig {
4952
[INIT]() {
5053
if (!this.config[Symbols.LOAD_SUCCESS]) {
5154
// 文件未加载成功.
52-
logger.error(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
55+
logger.warn(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
56+
logger.warn(`You must be to create "${CONSTANTS.CONFIG_NAME}" in "${this.root}"`);
5357
}
5458
if (this.root) {
5559
try {
5660
const packagePath = path.resolve(this.root, CONSTANTS.PACKAGE_JSON);
5761
if (fs.existsSync(packagePath)) {
58-
this._packagePath = packagePath;
59-
this._package = require(packagePath);
62+
this[KEY_PACKAGE_PATH] = packagePath;
63+
this[KEY_PACKAGE] = require(packagePath);
6064
if (!this.config[Symbols.LOAD_SUCCESS]) {
61-
// 文件未加载成功.
62-
// TODO 可以从 package.json 中查询配置文件
65+
// 文件未加载成功. 可以从 package.json 中查询配置文件
66+
if (this[KEY_PACKAGE] && this[KEY_PACKAGE]['micro-app'] && _.isPlainObject(this[KEY_PACKAGE]['micro-app'])) {
67+
Object.assign(this[KEY_ORIGNAL_CONFIG], this[KEY_PACKAGE]['micro-app']);
68+
}
6369
}
6470
}
6571
} catch (error) {
66-
this._packagePath = '';
67-
this._package = {};
72+
this[KEY_PACKAGE_PATH] = '';
73+
this[KEY_PACKAGE] = {};
6874
logger.warn(`Not Fount "${CONSTANTS.PACKAGE_JSON}" !`);
6975
}
7076
}
7177
}
7278

7379
get config() {
74-
return this[ORIGNAL_CONFIG] || {};
80+
return this[KEY_ORIGNAL_CONFIG] || {};
7581
}
7682

7783
get root() {
@@ -119,11 +125,11 @@ class BaseConfig {
119125
}
120126

121127
get packagePath() {
122-
return this._packagePath;
128+
return this[KEY_PACKAGE_PATH] || '';
123129
}
124130

125131
get package() {
126-
return Object.freeze(JSON.parse(JSON.stringify(this._package || {})));
132+
return Object.freeze(JSON.parse(JSON.stringify(this[KEY_PACKAGE] || {})));
127133
}
128134

129135
get key() {
@@ -178,13 +184,13 @@ class BaseConfig {
178184
if (currShared) { // 兼容旧版
179185
return Object.keys(currShared).reduce((obj, key) => {
180186
const aliasObj = currShared[key];
181-
if (aliasObj && typeof aliasObj === 'string') {
187+
if (aliasObj && _.isString(aliasObj)) {
182188
obj[key] = {
183189
link: aliasObj,
184190
};
185-
} else if (aliasObj && typeof aliasObj === 'object') {
191+
} else if (aliasObj && _.isPlainObject(aliasObj)) {
186192
const link = aliasObj.link;
187-
if (link && typeof link === 'string') {
193+
if (link && _.isString(link)) {
188194
obj[key] = aliasObj;
189195
}
190196
}
@@ -194,13 +200,13 @@ class BaseConfig {
194200
const currAlias = config.alias || {};
195201
return Object.keys(currAlias).reduce((obj, key) => {
196202
const aliasObj = currAlias[key];
197-
if (aliasObj && typeof aliasObj === 'string') {
203+
if (aliasObj && _.isString(aliasObj)) {
198204
obj[key] = {
199205
link: aliasObj,
200206
};
201-
} else if (aliasObj && typeof aliasObj === 'object') {
207+
} else if (aliasObj && _.isPlainObject(aliasObj)) {
202208
const link = aliasObj.link;
203-
if (link && typeof link === 'string') {
209+
if (link && _.isString(link)) {
204210
obj[key] = aliasObj;
205211
}
206212
}
@@ -226,7 +232,7 @@ class BaseConfig {
226232
Object.keys(currShared).forEach(k => {
227233
const p = currShared[k];
228234
const aliasKey = `${aliasName}/${k}`;
229-
if (!alias[aliasKey] && typeof p === 'string') {
235+
if (!alias[aliasKey] && _.isString(p)) {
230236
const filePath = path.resolve(this.root, p);
231237
alias[aliasKey] = filePath;
232238
}
@@ -241,17 +247,17 @@ class BaseConfig {
241247
const currAlias = config.alias || {};
242248
return Object.keys(currAlias).reduce((obj, key) => {
243249
const aliasObj = currAlias[key];
244-
if (aliasObj && typeof aliasObj === 'string') {
250+
if (aliasObj && _.isString(aliasObj)) {
245251
obj[key] = {
246252
link: aliasObj,
247253
};
248254
} else if (aliasObj && _.isPlainObject(aliasObj)) {
249-
if (aliasObj.server === true || typeof aliasObj.type === 'string' && aliasObj.type.toUpperCase() === 'SERVER') {
255+
if (aliasObj.server === true || _.isString(aliasObj.type) && aliasObj.type.toUpperCase() === 'SERVER') {
250256
// server ?
251257
return obj;
252258
}
253259
const link = aliasObj.link;
254-
if (link && typeof link === 'string') {
260+
if (link && _.isString(link)) {
255261
obj[key] = aliasObj;
256262
}
257263
}
@@ -276,7 +282,7 @@ class BaseConfig {
276282
Object.keys(currAlias).forEach(key => {
277283
const p = currAlias[key];
278284
const aliasKey = `${aliasName}/${key}`;
279-
if (!alias[aliasKey] && typeof p === 'string') {
285+
if (!alias[aliasKey] && _.isString(p)) {
280286
const filePath = path.resolve(this.root, p);
281287
alias[aliasKey] = filePath;
282288
}

libs/Service/base/BaseService.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const loadFile = require('../../../src/utils/loadFile');
1111
const logger = require('../../../src/utils/logger');
1212

1313
const { SharedProps } = require('../constants');
14-
const MICROS_EXTRAL_CONFIG_KEY = Symbol('MICROS_EXTRAL_CONFIG_KEY');
14+
const MICROS_EXTRA_CONFIG_KEY = Symbol('MICROS_EXTRA_CONFIG_KEY');
1515

1616
// 全局状态集
1717
const GLOBAL_STATE = {};
@@ -73,7 +73,7 @@ class BaseService {
7373
*/
7474
__initGlobalMicroAppConfig__() {
7575
// 加载高级配置
76-
const extraConfig = this[MICROS_EXTRAL_CONFIG_KEY] = loadFile(this.root, CONSTANTS.EXTRAL_CONFIG_NAME);
76+
const extraConfig = this[MICROS_EXTRA_CONFIG_KEY] = loadFile(this.root, CONSTANTS.EXTRAL_CONFIG_NAME);
7777

7878
if (extraConfig && _.isPlainObject(extraConfig)) {
7979
Object.keys(extraConfig).forEach(key => {
@@ -113,9 +113,7 @@ class BaseService {
113113
get self() {
114114
const _self = requireMicro.self();
115115
if (!_self) {
116-
logger.error(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
117-
logger.warn(`must be to create "${CONSTANTS.CONFIG_NAME}" in "${this.root}"`);
118-
process.exit(1);
116+
logger.throw(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
119117
}
120118
return _self;
121119
}
@@ -124,11 +122,17 @@ class BaseService {
124122
return this.microsConfig[this.self.key] || this.self.toConfig(true) || {};
125123
}
126124

125+
get extraConfig() {
126+
return this[MICROS_EXTRA_CONFIG_KEY] || {};
127+
}
128+
127129
get microsExtraConfig() {
128-
const microsExtral = this[MICROS_EXTRAL_CONFIG_KEY] || {};
130+
const extraConfig = this.extraConfig || {};
131+
// 兼容旧版本
132+
const microsExtra = extraConfig.micro || extraConfig || {};
129133
const result = {};
130134
Array.from(this.micros).forEach(key => {
131-
result[key] = Object.assign({}, microsExtral[key] || {
135+
result[key] = Object.assign({}, microsExtra[key] || {
132136
disabled: false, // 禁用入口
133137
disable: false,
134138
link: false,
@@ -143,7 +147,7 @@ class BaseService {
143147
result[key].disable = false;
144148
}
145149
});
146-
return Object.assign({}, microsExtral, result);
150+
return Object.assign({}, microsExtra, result);
147151
}
148152

149153
_initMicrosConfig() {

libs/Service/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const contants = {
1212
'env',
1313
'version',
1414
'pkg',
15+
'extraConfig',
1516
'microsExtraConfig',
1617
'applyPluginHooks',
1718
'applyPluginHooksAsync',

libs/Service/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ const tryRequire = require('try-require');
44
const assert = require('assert');
55
const _ = require('lodash');
66

7-
const BaseService = require('./base/BaseService');
7+
const { moduleAlias, smartMerge, virtualFile } = require('@micro-app/shared-utils');
88

9+
const BaseService = require('./base/BaseService');
910
const logger = require('../../src/utils/logger');
10-
const virtualFile = require('../../src/utils/virtualFile');
11-
const smartMerge = require('../../src/utils/smartMerge');
12-
13-
const moduleAlias = require('../../src/utils/injectModuleAlias');
14-
1511
const PluginAPI = require('./PluginAPI');
16-
1712
const { PreLoadPlugins } = require('./constants');
1813

1914
class Service extends BaseService {
@@ -373,7 +368,7 @@ e.g.
373368

374369
runCommand(rawName, rawArgs = { _: [] }) {
375370
logger.debug(`[Plugin] raw command name: ${rawName}, args: `, rawArgs);
376-
const { name = rawName, args } = this.applyPluginHooks('modifyCommand', {
371+
const { name = rawName, args = rawArgs } = this.applyPluginHooks('modifyCommand', {
377372
name: rawName,
378373
args: rawArgs,
379374
});

micro-app.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545

4646
strict: true,
4747

48-
micros: [ 'test' ], // 被注册的容器
48+
micros: [ 'test', 'abab' ], // 被注册的容器
4949

5050
// 服务配置
5151
server: {

package.json

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/core",
3-
"version": "0.2.0-beta.6",
3+
"version": "0.2.0",
44
"description": "[Core] Pluggable micro application framework.",
55
"main": "src/index.js",
66
"scripts": {
@@ -17,7 +17,6 @@
1717
"url": "https://github.com/MicrosApp/MicroApp-Core/issues"
1818
},
1919
"files": [
20-
"config",
2120
"libs",
2221
"src",
2322
"plugins",
@@ -42,33 +41,21 @@
4241
]
4342
},
4443
"devDependencies": {
45-
"@types/jest": "^24.0.18",
44+
"@types/jest": "^24.0.19",
4645
"babel-eslint": "^10.0.3",
4746
"coveralls": "^3.0.7",
4847
"eslint": "^5.16.0",
4948
"eslint-config-2o3t": "^1.1.17",
50-
"html-webpack-plugin": "^3.2.0",
5149
"husky": "^3.0.9",
5250
"jest": "^24.9.0",
5351
"lint-staged": "^9.4.2",
54-
"webpack": "^4.39.2",
55-
"webpack-merge": "^4.2.2",
5652
"yargs-parser": "^13.1.1"
5753
},
5854
"dependencies": {
55+
"@micro-app/shared-utils": "^0.0.3",
5956
"ajv": "^6.10.2",
6057
"ajv-keywords": "^3.4.1",
61-
"assert": "^2.0.0",
62-
"chalk": "^2.4.2",
63-
"cheerio": "^1.0.0-rc.3",
64-
"dotenv": "^8.1.0",
65-
"fs-extra": "^8.1.0",
66-
"lodash": "^4.17.15",
67-
"ora": "^3.4.0",
68-
"semver": "^6.3.0",
69-
"semver-regex": "^3.1.0",
70-
"stream-to-string": "^1.2.0",
71-
"try-require": "^1.2.1"
58+
"dotenv": "^8.2.0"
7259
},
7360
"engines": {
7461
"node": ">=8"

plugins/commands/check/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
module.exports = function(api) {
44

5-
const { padEnd } = require('lodash');
65
const chalk = require('chalk');
76
const _ = require('lodash');
8-
const getPadLength = require('../../../src/utils/getPadLength');
7+
const { getPadLength } = require('@micro-app/shared-utils');
98

109
const details = `
1110
Examples:
@@ -60,11 +59,11 @@ Examples:
6059
const padLength = getPadLength(arrs.concat(micros).map(key => ({ name: key })));
6160
arrs.forEach(key => {
6261
const _version = selfDeps[key];
63-
const textStrs = [ ` * ${chalk.yellow(padEnd(key, padLength))} ${chalk.gray(`[ ${_version} ]`)}` ];
62+
const textStrs = [ ` * ${chalk.yellow(_.padEnd(key, padLength))} ${chalk.gray(`[ ${_version} ]`)}` ];
6463
const _names = dependenciesMap[key] || false;
6564
if (_names && Array.isArray(_names) && _names.length > 0) {
6665
_names.forEach(_name => {
67-
const _microName = chalk.blue(padEnd(_name, padLength - 2));
66+
const _microName = chalk.blue(_.padEnd(_name, padLength - 2));
6867
const _microVersion = microsDeps[_name][key];
6968
let color = 'gray';
7069
try {

plugins/commands/help/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
module.exports = function(api) {
44

5-
const { padEnd } = require('lodash');
5+
const _ = require('lodash');
66
const chalk = require('chalk');
7-
const getPadLength = require('../../../src/utils/getPadLength');
7+
const { getPadLength } = require('@micro-app/shared-utils');
88

99
api.registerCommand('help', {
1010
hide: true,
@@ -31,7 +31,7 @@ module.exports = function(api) {
3131
for (const name in commands) {
3232
const opts = commands[name].opts || {};
3333
if (opts.hide !== true) {
34-
api.logger.logo(` * ${chalk.yellow(padEnd(name, padLength))}${opts.description ? ` ( ${chalk.gray(opts.description)} )` : ''}`);
34+
api.logger.logo(` * ${chalk.yellow(_.padEnd(name, padLength))}${opts.description ? ` ( ${chalk.gray(opts.description)} )` : ''}`);
3535
}
3636
}
3737
api.logger.logo(
@@ -54,7 +54,7 @@ module.exports = function(api) {
5454
api.logger.logo(`${chalk.green('Options')}:`);
5555
const padLength = getPadLength(opts.options);
5656
for (const name in opts.options) {
57-
api.logger.logo(` * ${chalk.yellow(padEnd(name, padLength))} ( ${chalk.gray(opts.options[name])} )`);
57+
api.logger.logo(` * ${chalk.yellow(_.padEnd(name, padLength))} ( ${chalk.gray(opts.options[name])} )`);
5858
}
5959
}
6060
if (opts.details) {

plugins/commands/show/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
module.exports = function(api) {
44

5-
const { padEnd } = require('lodash');
5+
const _ = require('lodash');
66
const chalk = require('chalk');
7-
const getPadLength = require('../../../src/utils/getPadLength');
7+
const { getPadLength } = require('@micro-app/shared-utils');
88
const aliasMerge = require('../../../src/utils/merge-alias');
99

1010
const details = `
@@ -146,7 +146,7 @@ Examples:
146146
const arrs = Object.keys(obj);
147147
const padLength = getPadLength(arrs.map(key => ({ name: key })));
148148
arrs.forEach(key => {
149-
const textStrs = [ ` * ${chalk.yellow(padEnd(key, padLength))}` ];
149+
const textStrs = [ ` * ${chalk.yellow(_.padEnd(key, padLength))}` ];
150150
const alias = obj[key] && obj[key].alias || false;
151151
if (alias && typeof alias === 'string') {
152152
textStrs.push(`[ ${chalk.blue(alias)} ]`);

0 commit comments

Comments
 (0)