Skip to content

Commit dca0d93

Browse files
committed
🎨 重构部分内容.
1 parent 3688860 commit dca0d93

33 files changed

+1765
-1045
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Pluggable micro application framework.
99
[![NPM Version][npm-img]][npm-url]
1010
[![NPM Download][download-img]][download-url]
1111

12-
[Coverage-img]: https://coveralls.io/repos/github/MicrosApp/MicroApp-Core/badge.svg?branch=master
13-
[Coverage-url]: https://coveralls.io/github/MicrosApp/MicroApp-Core?branch=master
14-
[CircleCI-img]: https://circleci.com/gh/MicrosApp/MicroApp-Core/tree/master.svg?style=svg
15-
[CircleCI-url]: https://circleci.com/gh/MicrosApp/MicroApp-Core/tree/master
12+
[Coverage-img]: https://coveralls.io/repos/github/MicroAppJS/MicroApp-Core/badge.svg?branch=master
13+
[Coverage-url]: https://coveralls.io/github/MicroAppJS/MicroApp-Core?branch=master
14+
[CircleCI-img]: https://circleci.com/gh/MicroAppJS/MicroApp-Core/tree/master.svg?style=svg
15+
[CircleCI-url]: https://circleci.com/gh/MicroAppJS/MicroApp-Core/tree/master
1616
[npm-img]: https://img.shields.io/npm/v/@micro-app/core.svg?style=flat-square
1717
[npm-url]: https://npmjs.org/package/@micro-app/core
1818
[download-img]: https://img.shields.io/npm/dm/@micro-app/core.svg?style=flat-square

micro-app.config.js

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

4646
strict: true,
4747

48-
micros: [ 'test', 'abab' ], // 被注册的容器
48+
micros: [ 'test', 'abab', '@micro/shared-utils',
49+
'git+ssh://git@g.hz.netease.com:22222/ops-fullstack/micro/micro-gportal.git#e718f77fce613a3044c451264e75e9e64b2941f7'
50+
], // 被注册的容器
4951

5052
// 服务配置
5153
server: {

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
"lint:fix": "npm run lint -- --fix",
1010
"test": "jest"
1111
},
12-
"homepage": "https://github.com/MicrosApp/MicroApp-Core",
12+
"homepage": "https://github.com/MicroAppJS/MicroApp-Core",
1313
"repository": {
1414
"type": "git",
15-
"url": "git+https://github.com/MicrosApp/MicroApp-Core.git"
15+
"url": "git+https://github.com/MicroAppJS/MicroApp-Core.git"
1616
},
1717
"bugs": {
18-
"url": "https://github.com/MicrosApp/MicroApp-Core/issues"
18+
"url": "https://github.com/MicroAppJS/MicroApp-Core/issues"
1919
},
2020
"files": [
2121
"src",
@@ -51,13 +51,13 @@
5151
"yargs-parser": "^13.1.1"
5252
},
5353
"dependencies": {
54-
"@micro-app/shared-utils": "^0.0.4",
54+
"@micro-app/shared-utils": "file:../MicroApp-Shared-Utils",
5555
"ajv": "^6.10.2",
5656
"ajv-keywords": "^3.4.1",
5757
"dotenv": "^8.2.0"
5858
},
5959
"peerDependencies": {
60-
"@micro-app/shared-utils": ">=0.0.4"
60+
"@micro-app/shared-utils": ">=0.0.5"
6161
},
6262
"engines": {
6363
"node": ">=8"

src/core/Config/index.test.js renamed to src/core/Config/Config.test.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ describe('MicroAppConfig', () => {
4343
expect(config.toJSON(true)).not.toBeUndefined();
4444
expect(config.toJSON(true)).not.toBeNull();
4545

46-
expect(config.toConfig(true)).not.toBeUndefined();
47-
expect(config.toConfig(true)).not.toBeNull();
46+
expect(config).not.toBeUndefined();
47+
expect(config).not.toBeNull();
4848

4949
// 已迁移
5050
expect(config.webpack).toBeUndefined();
@@ -77,8 +77,8 @@ describe('MicroAppConfig', () => {
7777
expect(config.toJSON(true)).not.toBeUndefined();
7878
expect(config.toJSON(true)).not.toBeNull();
7979

80-
expect(config.toConfig(true)).not.toBeUndefined();
81-
expect(config.toConfig(true)).not.toBeNull();
80+
expect(config).not.toBeUndefined();
81+
expect(config).not.toBeNull();
8282

8383
// 已迁移
8484
expect(config.webpack).toBeUndefined();
@@ -91,4 +91,24 @@ describe('MicroAppConfig', () => {
9191
expect(config.server).toBeUndefined();
9292
expect(config.toServerConfig).toBeUndefined();
9393
});
94+
95+
it('manifest', () => {
96+
const defaultConfig = loadFile(process.cwd(), 'micro-app.config.js');
97+
const config = new MicroAppConfig(Object.assign({}, defaultConfig, {
98+
99+
}));
100+
101+
expect(config.manifest).not.toBeUndefined();
102+
expect(config.manifest).not.toBeNull();
103+
});
104+
105+
it('licensePath', () => {
106+
const defaultConfig = loadFile(process.cwd(), 'micro-app.config.js');
107+
const config = new MicroAppConfig(Object.assign({}, defaultConfig, {
108+
109+
}));
110+
111+
expect(config.licensePath).not.toBeUndefined();
112+
expect(config.licensePath).not.toBeNull();
113+
});
94114
});

src/core/Config/index.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,70 @@
11
'use strict';
22

3-
const BaseConfig = require('./base/BaseConfig');
3+
const path = require('path');
4+
const { globby } = require('@micro-app/shared-utils');
5+
6+
const BaseConfig = require('./libs/BaseConfig');
7+
const Package = require('../Package');
48

59
class MicroAppConfig extends BaseConfig {
610

11+
static get LICENSE_GLOB() {
12+
return 'LICEN{S,C}E{,.*}';
13+
}
14+
15+
get __isMicroAppConfig() {
16+
return true;
17+
}
18+
19+
get manifest() {
20+
let manifest;
21+
22+
try {
23+
const packageJson = this.package;
24+
// Encapsulate raw JSON in Package instance
25+
manifest = new Package(packageJson, this.root);
26+
27+
// redefine getter to lazy-loaded value
28+
Object.defineProperty(this, 'manifest', {
29+
value: manifest,
30+
});
31+
} catch (err) {
32+
// try again next time
33+
}
34+
35+
return manifest;
36+
}
37+
38+
get licensePath() {
39+
let licensePath;
40+
41+
try {
42+
const search = globby.sync(MicroAppConfig.LICENSE_GLOB, {
43+
cwd: this.root,
44+
absolute: true,
45+
case: false,
46+
// Project license is always a sibling of the root manifest
47+
deep: false,
48+
// POSIX results always need to be normalized
49+
transform: fp => {
50+
return path.normalize(fp);
51+
},
52+
});
53+
54+
licensePath = search.shift();
55+
56+
if (licensePath) {
57+
// redefine getter to lazy-loaded value
58+
Object.defineProperty(this, 'licensePath', {
59+
value: licensePath,
60+
});
61+
}
62+
} catch (err) {
63+
throw err;
64+
}
65+
66+
return licensePath;
67+
}
768
}
869

970
module.exports = MicroAppConfig;

src/core/Config/base/BaseConfig.js renamed to src/core/Config/libs/BaseConfig.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict';
22

33
const path = require('path');
4-
const fs = require('fs-extra');
5-
const tryRequire = require('try-require');
6-
const _ = require('lodash');
7-
8-
const { getPadLength } = require('@micro-app/shared-utils');
4+
const { getPadLength, _, tryRequire, isGlob } = require('@micro-app/shared-utils');
95

106
const Symbols = require('../../Constants/symbols');
117
const CONSTANTS = require('../../Constants');
128
const logger = require('../../../utils/logger');
9+
const loadFile = require('../../../utils/loadFile');
1310

1411
// 默认配置
1512
// const DEFAULT_CONFIG = require('../../Constants/default');
@@ -57,15 +54,12 @@ class BaseConfig {
5754
}
5855
if (this.root) {
5956
try {
60-
const packagePath = path.resolve(this.root, CONSTANTS.PACKAGE_JSON);
61-
if (fs.existsSync(packagePath)) {
62-
this[KEY_PACKAGE_PATH] = packagePath;
63-
this[KEY_PACKAGE] = require(packagePath);
64-
if (!this.config[Symbols.LOAD_SUCCESS]) {
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-
}
57+
this[KEY_PACKAGE_PATH] = path.resolve(this.root, CONSTANTS.PACKAGE_JSON);
58+
this[KEY_PACKAGE] = loadFile(this.root, CONSTANTS.PACKAGE_JSON);
59+
if (!this.config[Symbols.LOAD_SUCCESS]) {
60+
// 文件未加载成功. 可以从 package.json 中查询配置文件
61+
if (this[KEY_PACKAGE] && this[KEY_PACKAGE]['micro-app'] && _.isPlainObject(this[KEY_PACKAGE]['micro-app'])) {
62+
Object.assign(this[KEY_ORIGNAL_CONFIG], this[KEY_PACKAGE]['micro-app']);
6963
}
7064
}
7165
} catch (error) {
@@ -129,13 +123,13 @@ class BaseConfig {
129123
}
130124

131125
get package() {
132-
return Object.freeze(JSON.parse(JSON.stringify(this[KEY_PACKAGE] || {})));
126+
return _.cloneDeep(this[KEY_PACKAGE] || {});
133127
}
134128

129+
// 唯一标识
135130
get key() {
136131
const config = this.config;
137-
const reg = new RegExp(`^${CONSTANTS.SCOPE_NAME}\/?`, 'ig');
138-
return config[Symbols.KEY] || this.packageName.replace(reg, '') || '';
132+
return config[Symbols.KEY] || this.packageName || path.basename(path.dirname(this.root)) || '';
139133
}
140134

141135
get name() {
@@ -150,7 +144,7 @@ class BaseConfig {
150144
get aliasName() {
151145
let aliasName = this.name || '';
152146
if (!aliasName.startsWith(CONSTANTS.SCOPE_NAME)) {
153-
aliasName = `${CONSTANTS.SCOPE_NAME}/${aliasName}`;
147+
aliasName = `${CONSTANTS.SCOPE_NAME}/${aliasName.replace(/^@/ig, '')}`;
154148
}
155149
return aliasName[0] !== '@' ? `@${aliasName}` : aliasName;
156150
}
@@ -169,6 +163,7 @@ class BaseConfig {
169163
const config = this.config;
170164
return config.type || '';
171165
}
166+
172167
get micros() {
173168
const config = this.config;
174169
if (config.micros && Array.isArray(config.micros)) {
@@ -326,7 +321,7 @@ class BaseConfig {
326321
}
327322

328323
inspect() {
329-
return this.toConfig(true);
324+
return this.toJSON();
330325
}
331326

332327
toJSON(notSimple = false) {
@@ -343,9 +338,9 @@ class BaseConfig {
343338
originalRoot: this.originalRoot,
344339
hasSoftLink: this.hasSoftLink,
345340
nodeModules: this.nodeModules,
341+
strict: this.strict,
346342
};
347343
if (notSimple) {
348-
json.strict = this.strict;
349344
json.path = this.path;
350345
json.micros = this.micros;
351346
json.packagePath = this.packagePath;
@@ -354,23 +349,6 @@ class BaseConfig {
354349
}
355350
return json;
356351
}
357-
358-
toConfig(notSimple = false) {
359-
const json = {
360-
...this.toJSON(notSimple),
361-
alias: this.alias,
362-
aliasObj: this.aliasObj,
363-
resolveAlias: this.resolveAlias,
364-
shared: this.shared,
365-
sharedObj: this.sharedObj,
366-
resolveShared: this.resolveShared,
367-
};
368-
if (notSimple) {
369-
json.plugins = this.plugins;
370-
json.originalConfig = this.config;
371-
}
372-
return json;
373-
}
374352
}
375353

376354
module.exports = BaseConfig;
File renamed without changes.

src/core/Constants/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
'use strict';
22

33
const pkg = require('../../../package.json');
4+
const Symbols = require('./symbols');
45

56
module.exports = {
7+
Symbols,
68
NAME: 'Micro App',
79
VERSION: pkg.version,
810
ROOT: process.env.MICRO_APP_ROOT || process.cwd(),
911
NODE_MODULES_NAME: 'node_modules',
12+
PACKAGE_JSON: 'package.json',
1013
SCOPE_NAME: '@micro-app', // namespace
1114
CONFIG_NAME: 'micro-app.config.js',
1215
EXTRAL_CONFIG_NAME: 'micro-app.extra.config.js',
1316
TYPES: [], // support types
1417
INJECT_ID: '_MICRO_APP_INJECT_',
15-
PACKAGE_JSON: 'package.json',
1618
};

src/core/Constants/symbols.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module.exports = {
44
KEY: Symbol('@MicroAppConfig#KEY'),
55
PATH: Symbol('@MicroAppConfig#PATH'),
66
ROOT: Symbol('@MicroAppConfig#ROOT'),
7+
FILENAME: Symbol('@MicroAppConfig#FILENAME'),
8+
DIRNAME: Symbol('@MicroAppConfig#DIRNAME'),
79
ORIGINAL_ROOT: Symbol('@MicroAppConfig#ORIGINAL_ROOT'),
810
LOAD_SUCCESS: Symbol('@MicroAppConfig#LOAD_SUCCESS'), // 是否加载 config 成功
911
};

0 commit comments

Comments
 (0)