Skip to content

Commit 77b8c7d

Browse files
committed
:add: not found hint
1 parent 234d565 commit 77b8c7d

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

libs/Config/base/BaseConfig.js

Lines changed: 24 additions & 12 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('../../Constants/symbols');
8+
const Symbols = require('../../Constants/symbols');
99
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('../../Constants/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,16 +46,23 @@ 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) {
53-
console.warn(config, error);
5466
this._packagePath = '';
5567
this._package = {};
5668
logger.warn(`Not Fount "${CONSTANTS.PACKAGE_JSON}" !`);
@@ -64,12 +76,12 @@ class BaseConfig {
6476

6577
get root() {
6678
const config = this.config;
67-
return config[symbols.ROOT] || '';
79+
return config[Symbols.ROOT] || '';
6880
}
6981

7082
get originalRoot() {
7183
const config = this.config;
72-
return config[symbols.ORIGINAL_ROOT] || this.root || '';
84+
return config[Symbols.ORIGINAL_ROOT] || this.root || '';
7385
}
7486

7587
get hasSoftLink() {
@@ -78,7 +90,7 @@ class BaseConfig {
7890

7991
get path() {
8092
const config = this.config;
81-
return config[symbols.PATH] || '';
93+
return config[Symbols.PATH] || '';
8294
}
8395

8496
get nodeModules() {
@@ -117,7 +129,7 @@ class BaseConfig {
117129
get key() {
118130
const config = this.config;
119131
const reg = new RegExp(`^${CONSTANTS.SCOPE_NAME}\/?`, 'ig');
120-
return config[symbols.KEY] || this.packageName.replace(reg, '') || '';
132+
return config[Symbols.KEY] || this.packageName.replace(reg, '') || '';
121133
}
122134

123135
get name() {

libs/Constants/symbols.js

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
};

src/utils/loadFile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ function load(root, filename) {
2020
return null;
2121
}
2222
const file = tryRequire(filePath);
23-
return file ? extraConfig(file, root, filename) : null;
23+
if (file) {
24+
file[symbols.LOAD_SUCCESS] = true;
25+
return extraConfig(file, root, filename);
26+
}
27+
return null;
2428
}
2529

2630
function extraConfig(file, root, filename) {

0 commit comments

Comments
 (0)