Skip to content

Commit 37ae154

Browse files
authored
Merge pull request #16 from MicrosApp/develop
Develop
2 parents 5097f45 + cf2d2aa commit 37ae154

19 files changed

Lines changed: 172 additions & 134 deletions

File tree

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ jobs:
2323
paths:
2424
- ./node_modules
2525
- run: yarn run test --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
26+
- run: rm -rf ./micro-app.config.js
27+
- run: yarn run test --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
2628

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 zyao89
3+
Copyright (c) 2019-present, Zyao89
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

config/default.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

libs/Config/base/BaseConfig.js

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

8-
const symbols = require('../../../config/symbols');
9-
const CONSTANTS = require('../../../config/constants');
8+
const Symbols = require('../../Constants/symbols');
9+
const CONSTANTS = require('../../Constants');
1010
const logger = require('../../../src/utils/logger');
1111
const getPadLength = require('../../../src/utils/getPadLength');
1212

1313
// 默认配置
14-
const DEFAULT_CONFIG = require('../../../config/default');
14+
// const DEFAULT_CONFIG = require('../../Constants/default');
1515

1616
const validate = require('../schema');
1717
const SCHEMA = require('../schema/configSchema');
@@ -21,11 +21,16 @@ const ORIGNAL_CONFIG = Symbol('@BaseConfig#ORIGNAL_CONFIG');
2121

2222
class BaseConfig {
2323

24+
/**
25+
* Creates an instance of BaseConfig.
26+
* @param {DEFAULT_CONFIG} config config
27+
* @memberof BaseConfig
28+
*/
2429
constructor(config /* , opts = {} */) {
2530
// 校验 config
2631
this._validateSchema(config);
27-
this[INIT](config);
28-
this[ORIGNAL_CONFIG] = config || DEFAULT_CONFIG;
32+
this[ORIGNAL_CONFIG] = config;
33+
this[INIT]();
2934
}
3035

3136
_validateSchema(config) {
@@ -41,18 +46,26 @@ class BaseConfig {
4146
}
4247
}
4348

44-
[INIT](config) {
45-
if (config) {
49+
[INIT]() {
50+
if (!this.config[Symbols.LOAD_SUCCESS]) {
51+
// 文件未加载成功.
52+
logger.error(`Not Found "${CONSTANTS.CONFIG_NAME}"`);
53+
}
54+
if (this.root) {
4655
try {
47-
const packagePath = path.join(config[symbols.ROOT], CONSTANTS.PACKAGE_JSON);
56+
const packagePath = path.resolve(this.root, CONSTANTS.PACKAGE_JSON);
4857
if (fs.existsSync(packagePath)) {
4958
this._packagePath = packagePath;
5059
this._package = require(packagePath);
60+
if (!this.config[Symbols.LOAD_SUCCESS]) {
61+
// 文件未加载成功.
62+
// TODO 可以从 package.json 中查询配置文件
63+
}
5164
}
5265
} catch (error) {
5366
this._packagePath = '';
5467
this._package = {};
55-
logger.warn('Not Fount "package.json" !');
68+
logger.warn(`Not Fount "${CONSTANTS.PACKAGE_JSON}" !`);
5669
}
5770
}
5871
}
@@ -63,12 +76,12 @@ class BaseConfig {
6376

6477
get root() {
6578
const config = this.config;
66-
return config[symbols.ROOT] || '';
79+
return config[Symbols.ROOT] || '';
6780
}
6881

6982
get originalRoot() {
7083
const config = this.config;
71-
return config[symbols.ORIGINAL_ROOT] || this.root || '';
84+
return config[Symbols.ORIGINAL_ROOT] || this.root || '';
7285
}
7386

7487
get hasSoftLink() {
@@ -77,7 +90,7 @@ class BaseConfig {
7790

7891
get path() {
7992
const config = this.config;
80-
return config[symbols.PATH] || '';
93+
return config[Symbols.PATH] || '';
8194
}
8295

8396
get nodeModules() {
@@ -116,7 +129,7 @@ class BaseConfig {
116129
get key() {
117130
const config = this.config;
118131
const reg = new RegExp(`^${CONSTANTS.SCOPE_NAME}\/?`, 'ig');
119-
return config[symbols.KEY] || this.packageName.replace(reg, '') || '';
132+
return config[Symbols.KEY] || this.packageName.replace(reg, '') || '';
120133
}
121134

122135
get name() {

libs/Config/base/BaseConfig.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* global expect */
44

55
const BaseConfig = require('./BaseConfig');
6-
const defaultConfig = require('../../../micro-app.config');
6+
const loadFile = require('../../../src/utils/loadFile');
77

88
const testConfig = {
99
shared: {
@@ -28,7 +28,8 @@ const testConfig = {
2828
describe('BaseConfig', () => {
2929

3030
it('new constructor', () => {
31-
const config = new BaseConfig(Object.assign({}, defaultConfig, testConfig));
31+
const defaultConfig = loadFile(__dirname, '../../Constants/default.js');
32+
const config = new BaseConfig(Object.assign(defaultConfig, testConfig));
3233

3334
expect(config.config).not.toBeNull();
3435
expect(config.root).not.toBeUndefined();
@@ -52,7 +53,8 @@ describe('BaseConfig', () => {
5253
});
5354

5455
it('config inspect', () => {
55-
const config = new BaseConfig(Object.assign({}, defaultConfig, testConfig));
56+
const defaultConfig = loadFile(__dirname, '../../Constants/default.js');
57+
const config = new BaseConfig(Object.assign(defaultConfig, testConfig));
5658

5759
expect(config.inspect).not.toBeNull();
5860
expect(config.inspect).not.toBeUndefined();

libs/Config/index.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
/* global expect */
44

55
const MicroAppConfig = require('./index');
6-
const defaultConfig = require('../../micro-app.config');
6+
const loadFile = require('../../src/utils/loadFile');
77

88
describe('MicroAppConfig', () => {
99

1010
it('new constructor', () => {
11+
const defaultConfig = loadFile(__dirname, '../Constants/default.js');
1112
const config = new MicroAppConfig(Object.assign({}, defaultConfig, {
1213

1314
}));
@@ -34,6 +35,7 @@ describe('MicroAppConfig', () => {
3435
});
3536

3637
it('new constructor Config', () => {
38+
const defaultConfig = loadFile(__dirname, '../Constants/default.js');
3739
const config = new MicroAppConfig(Object.assign({}, defaultConfig, {
3840

3941
}));
@@ -55,6 +57,7 @@ describe('MicroAppConfig', () => {
5557
});
5658

5759
it('new constructor others', () => {
60+
const defaultConfig = loadFile(__dirname, '../Constants/default.js');
5861
const config = new MicroAppConfig(Object.assign({}, defaultConfig, {
5962
entry: {
6063
main: [ './test/index.js' ],

libs/Constants/default.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
module.exports = {
4+
name: '', // 名称, 为空则使用 package.json 中的 name
5+
description: '', // 描述
6+
version: '0.0.1', // 版本
7+
type: '', // types 类型
8+
strict: true, // 是否为严格模式? 默认为 true - 强依赖
9+
10+
// entry: { // 入口
11+
// },
12+
13+
// htmls: [
14+
// {
15+
// template: '',
16+
// },
17+
// ], // 模版
18+
19+
// staticPath: [], // String | Array
20+
21+
// dlls: [
22+
// { // dll 基本配置, [ 只支持子模块中使用 ]
23+
// disabled: false,
24+
// context: '',
25+
// manifest: '',
26+
// filepath: '',
27+
// },
28+
// ],
29+
30+
alias: { // 共享别名
31+
// api: './client/api.js', // 默认为前后端通用
32+
// service: {
33+
// link: './server/service.js',
34+
// type: 'server', // 只支持后端
35+
// },
36+
},
37+
38+
// micros: [ // 需要注入的子模块
39+
// 'test'
40+
// ],
41+
42+
// 服务端配置
43+
// server: {
44+
// hooks: '', // 服务端 hook 路径
45+
// entry: '', // 服务端入口
46+
// host: ‘’, // 服务 IP
47+
// port: 8888, // 服务端口号
48+
// options: { }, // 服务端注入附加参数
49+
50+
// proxy: { // 服务代理
51+
// '/api': {
52+
// target: 'http://127.0.0.1', // target host
53+
// changeOrigin: true, // needed for virtual hosted sites
54+
// ws: true, // proxy websockets
55+
// },
56+
// },
57+
// },
58+
59+
// 一些插件配置项 (弃用)
60+
// plugin: {
61+
// ReplaceFileNotExists: {
62+
// debug: false, // 开启log
63+
// warnHint: 'Not Found',
64+
// loader: '', // 路径
65+
// resource: '', // 路径
66+
// test: /^@micros\//i, // 匹配规则
67+
// },
68+
// SpeedMeasurePlugin: {
69+
// disabled: true,
70+
// },
71+
// HappyPack: {
72+
// disabled: true,
73+
// },
74+
// },
75+
76+
// plugins: [ // 插件集
77+
// [
78+
// '', {},
79+
// ],
80+
// ],
81+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const pkg = require('../package.json');
3+
const pkg = require('../../package.json');
44

55
module.exports = {
66
NAME: 'Micro App',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports = {
55
PATH: Symbol('@MicroAppConfig#PATH'),
66
ROOT: Symbol('@MicroAppConfig#ROOT'),
77
ORIGINAL_ROOT: Symbol('@MicroAppConfig#ORIGINAL_ROOT'),
8+
LOAD_SUCCESS: Symbol('@MicroAppConfig#LOAD_SUCCESS'), // 是否加载 config 成功
89
};

libs/Service/base/BaseAPI.js

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

33
const semver = require('semver');
44
const logger = require('../../../src/utils/logger');
5-
const CONSTANTS = require('../../../config/constants');
5+
const CONSTANTS = require('../../../libs/Constants');
66

77
class BaseAPI {
88

0 commit comments

Comments
 (0)