@@ -5,13 +5,13 @@ const fs = require('fs-extra');
55const tryRequire = require ( 'try-require' ) ;
66const _ = 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 ' ) ;
1010const logger = require ( '../../../src/utils/logger' ) ;
1111const getPadLength = require ( '../../../src/utils/getPadLength' ) ;
1212
1313// 默认配置
14- const DEFAULT_CONFIG = require ( '../../../config /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,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 ( ) {
0 commit comments