@@ -5,13 +5,13 @@ const fs = require('fs-extra');
55const tryRequire = require ( 'try-require' ) ;
66const _ = require ( 'lodash' ) ;
77
8- const symbols = require ( '../../Constants/symbols' ) ;
8+ const Symbols = require ( '../../Constants/symbols' ) ;
99const CONSTANTS = require ( '../../Constants' ) ;
1010const logger = require ( '../../../src/utils/logger' ) ;
1111const getPadLength = require ( '../../../src/utils/getPadLength' ) ;
1212
1313// 默认配置
14- const DEFAULT_CONFIG = require ( '../../Constants/default' ) ;
14+ // const DEFAULT_CONFIG = require('../../Constants/default');
1515
1616const validate = require ( '../schema' ) ;
1717const SCHEMA = require ( '../schema/configSchema' ) ;
@@ -21,11 +21,16 @@ const ORIGNAL_CONFIG = Symbol('@BaseConfig#ORIGNAL_CONFIG');
2121
2222class 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 ( ) {
0 commit comments