Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @zyao89
54 changes: 30 additions & 24 deletions libs/Config/base/BaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const fs = require('fs-extra');
const tryRequire = require('try-require');
const _ = require('lodash');

const { getPadLength } = require('@micro-app/shared-utils');

const Symbols = require('../../Constants/symbols');
const CONSTANTS = require('../../Constants');
const logger = require('../../../src/utils/logger');
const getPadLength = require('../../../src/utils/getPadLength');

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

const INIT = Symbol('@BaseConfig#INIT');
const ORIGNAL_CONFIG = Symbol('@BaseConfig#ORIGNAL_CONFIG');
const KEY_ORIGNAL_CONFIG = Symbol('@BaseConfig#KEY_ORIGNAL_CONFIG');
const KEY_PACKAGE = Symbol('@BaseConfig#KEY_PACKAGE');
const KEY_PACKAGE_PATH = Symbol('@BaseConfig#KEY_PACKAGE_PATH');

class BaseConfig {

Expand All @@ -29,7 +32,7 @@ class BaseConfig {
constructor(config /* , opts = {} */) {
// 校验 config
this._validateSchema(config);
this[ORIGNAL_CONFIG] = config;
this[KEY_ORIGNAL_CONFIG] = config;
this[INIT]();
}

Expand All @@ -49,29 +52,32 @@ class BaseConfig {
[INIT]() {
if (!this.config[Symbols.LOAD_SUCCESS]) {
// 文件未加载成功.
logger.error(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
logger.warn(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
logger.warn(`You must be to create "${CONSTANTS.CONFIG_NAME}" in "${this.root}"`);
}
if (this.root) {
try {
const packagePath = path.resolve(this.root, CONSTANTS.PACKAGE_JSON);
if (fs.existsSync(packagePath)) {
this._packagePath = packagePath;
this._package = require(packagePath);
this[KEY_PACKAGE_PATH] = packagePath;
this[KEY_PACKAGE] = require(packagePath);
if (!this.config[Symbols.LOAD_SUCCESS]) {
// 文件未加载成功.
// TODO 可以从 package.json 中查询配置文件
// 文件未加载成功. 可以从 package.json 中查询配置文件
if (this[KEY_PACKAGE] && this[KEY_PACKAGE]['micro-app'] && _.isPlainObject(this[KEY_PACKAGE]['micro-app'])) {
Object.assign(this[KEY_ORIGNAL_CONFIG], this[KEY_PACKAGE]['micro-app']);
}
}
}
} catch (error) {
this._packagePath = '';
this._package = {};
this[KEY_PACKAGE_PATH] = '';
this[KEY_PACKAGE] = {};
logger.warn(`Not Fount "${CONSTANTS.PACKAGE_JSON}" !`);
}
}
}

get config() {
return this[ORIGNAL_CONFIG] || {};
return this[KEY_ORIGNAL_CONFIG] || {};
}

get root() {
Expand Down Expand Up @@ -119,11 +125,11 @@ class BaseConfig {
}

get packagePath() {
return this._packagePath;
return this[KEY_PACKAGE_PATH] || '';
}

get package() {
return Object.freeze(JSON.parse(JSON.stringify(this._package || {})));
return Object.freeze(JSON.parse(JSON.stringify(this[KEY_PACKAGE] || {})));
}

get key() {
Expand Down Expand Up @@ -178,13 +184,13 @@ class BaseConfig {
if (currShared) { // 兼容旧版
return Object.keys(currShared).reduce((obj, key) => {
const aliasObj = currShared[key];
if (aliasObj && typeof aliasObj === 'string') {
if (aliasObj && _.isString(aliasObj)) {
obj[key] = {
link: aliasObj,
};
} else if (aliasObj && typeof aliasObj === 'object') {
} else if (aliasObj && _.isPlainObject(aliasObj)) {
const link = aliasObj.link;
if (link && typeof link === 'string') {
if (link && _.isString(link)) {
obj[key] = aliasObj;
}
}
Expand All @@ -194,13 +200,13 @@ class BaseConfig {
const currAlias = config.alias || {};
return Object.keys(currAlias).reduce((obj, key) => {
const aliasObj = currAlias[key];
if (aliasObj && typeof aliasObj === 'string') {
if (aliasObj && _.isString(aliasObj)) {
obj[key] = {
link: aliasObj,
};
} else if (aliasObj && typeof aliasObj === 'object') {
} else if (aliasObj && _.isPlainObject(aliasObj)) {
const link = aliasObj.link;
if (link && typeof link === 'string') {
if (link && _.isString(link)) {
obj[key] = aliasObj;
}
}
Expand All @@ -226,7 +232,7 @@ class BaseConfig {
Object.keys(currShared).forEach(k => {
const p = currShared[k];
const aliasKey = `${aliasName}/${k}`;
if (!alias[aliasKey] && typeof p === 'string') {
if (!alias[aliasKey] && _.isString(p)) {
const filePath = path.resolve(this.root, p);
alias[aliasKey] = filePath;
}
Expand All @@ -241,17 +247,17 @@ class BaseConfig {
const currAlias = config.alias || {};
return Object.keys(currAlias).reduce((obj, key) => {
const aliasObj = currAlias[key];
if (aliasObj && typeof aliasObj === 'string') {
if (aliasObj && _.isString(aliasObj)) {
obj[key] = {
link: aliasObj,
};
} else if (aliasObj && _.isPlainObject(aliasObj)) {
if (aliasObj.server === true || typeof aliasObj.type === 'string' && aliasObj.type.toUpperCase() === 'SERVER') {
if (aliasObj.server === true || _.isString(aliasObj.type) && aliasObj.type.toUpperCase() === 'SERVER') {
// server ?
return obj;
}
const link = aliasObj.link;
if (link && typeof link === 'string') {
if (link && _.isString(link)) {
obj[key] = aliasObj;
}
}
Expand All @@ -276,7 +282,7 @@ class BaseConfig {
Object.keys(currAlias).forEach(key => {
const p = currAlias[key];
const aliasKey = `${aliasName}/${key}`;
if (!alias[aliasKey] && typeof p === 'string') {
if (!alias[aliasKey] && _.isString(p)) {
const filePath = path.resolve(this.root, p);
alias[aliasKey] = filePath;
}
Expand Down
20 changes: 12 additions & 8 deletions libs/Service/base/BaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const loadFile = require('../../../src/utils/loadFile');
const logger = require('../../../src/utils/logger');

const { SharedProps } = require('../constants');
const MICROS_EXTRAL_CONFIG_KEY = Symbol('MICROS_EXTRAL_CONFIG_KEY');
const MICROS_EXTRA_CONFIG_KEY = Symbol('MICROS_EXTRA_CONFIG_KEY');

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

if (extraConfig && _.isPlainObject(extraConfig)) {
Object.keys(extraConfig).forEach(key => {
Expand Down Expand Up @@ -113,9 +113,7 @@ class BaseService {
get self() {
const _self = requireMicro.self();
if (!_self) {
logger.error(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
logger.warn(`must be to create "${CONSTANTS.CONFIG_NAME}" in "${this.root}"`);
process.exit(1);
logger.throw(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
}
return _self;
}
Expand All @@ -124,11 +122,17 @@ class BaseService {
return this.microsConfig[this.self.key] || this.self.toConfig(true) || {};
}

get extraConfig() {
return this[MICROS_EXTRA_CONFIG_KEY] || {};
}

get microsExtraConfig() {
const microsExtral = this[MICROS_EXTRAL_CONFIG_KEY] || {};
const extraConfig = this.extraConfig || {};
// 兼容旧版本
const microsExtra = extraConfig.micro || extraConfig || {};
const result = {};
Array.from(this.micros).forEach(key => {
result[key] = Object.assign({}, microsExtral[key] || {
result[key] = Object.assign({}, microsExtra[key] || {
disabled: false, // 禁用入口
disable: false,
link: false,
Expand All @@ -143,7 +147,7 @@ class BaseService {
result[key].disable = false;
}
});
return Object.assign({}, microsExtral, result);
return Object.assign({}, microsExtra, result);
}

_initMicrosConfig() {
Expand Down
1 change: 1 addition & 0 deletions libs/Service/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const contants = {
'env',
'version',
'pkg',
'extraConfig',
'microsExtraConfig',
'applyPluginHooks',
'applyPluginHooksAsync',
Expand Down
11 changes: 3 additions & 8 deletions libs/Service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ const tryRequire = require('try-require');
const assert = require('assert');
const _ = require('lodash');

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

const BaseService = require('./base/BaseService');
const logger = require('../../src/utils/logger');
const virtualFile = require('../../src/utils/virtualFile');
const smartMerge = require('../../src/utils/smartMerge');

const moduleAlias = require('../../src/utils/injectModuleAlias');

const PluginAPI = require('./PluginAPI');

const { PreLoadPlugins } = require('./constants');

class Service extends BaseService {
Expand Down Expand Up @@ -373,7 +368,7 @@ e.g.

runCommand(rawName, rawArgs = { _: [] }) {
logger.debug(`[Plugin] raw command name: ${rawName}, args: `, rawArgs);
const { name = rawName, args } = this.applyPluginHooks('modifyCommand', {
const { name = rawName, args = rawArgs } = this.applyPluginHooks('modifyCommand', {
name: rawName,
args: rawArgs,
});
Expand Down
2 changes: 1 addition & 1 deletion micro-app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {

strict: true,

micros: [ 'test' ], // 被注册的容器
micros: [ 'test', 'abab' ], // 被注册的容器

// 服务配置
server: {
Expand Down
21 changes: 4 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@micro-app/core",
"version": "0.2.0-beta.6",
"version": "0.2.0",
"description": "[Core] Pluggable micro application framework.",
"main": "src/index.js",
"scripts": {
Expand All @@ -17,7 +17,6 @@
"url": "https://github.com/MicrosApp/MicroApp-Core/issues"
},
"files": [
"config",
"libs",
"src",
"plugins",
Expand All @@ -42,33 +41,21 @@
]
},
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/jest": "^24.0.19",
"babel-eslint": "^10.0.3",
"coveralls": "^3.0.7",
"eslint": "^5.16.0",
"eslint-config-2o3t": "^1.1.17",
"html-webpack-plugin": "^3.2.0",
"husky": "^3.0.9",
"jest": "^24.9.0",
"lint-staged": "^9.4.2",
"webpack": "^4.39.2",
"webpack-merge": "^4.2.2",
"yargs-parser": "^13.1.1"
},
"dependencies": {
"@micro-app/shared-utils": "^0.0.3",
"ajv": "^6.10.2",
"ajv-keywords": "^3.4.1",
"assert": "^2.0.0",
"chalk": "^2.4.2",
"cheerio": "^1.0.0-rc.3",
"dotenv": "^8.1.0",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15",
"ora": "^3.4.0",
"semver": "^6.3.0",
"semver-regex": "^3.1.0",
"stream-to-string": "^1.2.0",
"try-require": "^1.2.1"
"dotenv": "^8.2.0"
},
"engines": {
"node": ">=8"
Expand Down
7 changes: 3 additions & 4 deletions plugins/commands/check/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

module.exports = function(api) {

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

const details = `
Examples:
Expand Down Expand Up @@ -60,11 +59,11 @@ Examples:
const padLength = getPadLength(arrs.concat(micros).map(key => ({ name: key })));
arrs.forEach(key => {
const _version = selfDeps[key];
const textStrs = [ ` * ${chalk.yellow(padEnd(key, padLength))} ${chalk.gray(`[ ${_version} ]`)}` ];
const textStrs = [ ` * ${chalk.yellow(_.padEnd(key, padLength))} ${chalk.gray(`[ ${_version} ]`)}` ];
const _names = dependenciesMap[key] || false;
if (_names && Array.isArray(_names) && _names.length > 0) {
_names.forEach(_name => {
const _microName = chalk.blue(padEnd(_name, padLength - 2));
const _microName = chalk.blue(_.padEnd(_name, padLength - 2));
const _microVersion = microsDeps[_name][key];
let color = 'gray';
try {
Expand Down
8 changes: 4 additions & 4 deletions plugins/commands/help/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module.exports = function(api) {

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

api.registerCommand('help', {
hide: true,
Expand All @@ -31,7 +31,7 @@ module.exports = function(api) {
for (const name in commands) {
const opts = commands[name].opts || {};
if (opts.hide !== true) {
api.logger.logo(` * ${chalk.yellow(padEnd(name, padLength))}${opts.description ? ` ( ${chalk.gray(opts.description)} )` : ''}`);
api.logger.logo(` * ${chalk.yellow(_.padEnd(name, padLength))}${opts.description ? ` ( ${chalk.gray(opts.description)} )` : ''}`);
}
}
api.logger.logo(
Expand All @@ -54,7 +54,7 @@ module.exports = function(api) {
api.logger.logo(`${chalk.green('Options')}:`);
const padLength = getPadLength(opts.options);
for (const name in opts.options) {
api.logger.logo(` * ${chalk.yellow(padEnd(name, padLength))} ( ${chalk.gray(opts.options[name])} )`);
api.logger.logo(` * ${chalk.yellow(_.padEnd(name, padLength))} ( ${chalk.gray(opts.options[name])} )`);
}
}
if (opts.details) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/commands/show/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module.exports = function(api) {

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

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