@@ -5,10 +5,11 @@ const fs = require('fs-extra');
55const tryRequire = require ( 'try-require' ) ;
66const _ = require ( 'lodash' ) ;
77
8+ const { getPadLength } = require ( '@micro-app/shared-utils' ) ;
9+
810const Symbols = require ( '../../Constants/symbols' ) ;
911const CONSTANTS = require ( '../../Constants' ) ;
1012const logger = require ( '../../../src/utils/logger' ) ;
11- const getPadLength = require ( '../../../src/utils/getPadLength' ) ;
1213
1314// 默认配置
1415// const DEFAULT_CONFIG = require('../../Constants/default');
@@ -17,7 +18,9 @@ const validate = require('../schema');
1718const SCHEMA = require ( '../schema/configSchema' ) ;
1819
1920const INIT = Symbol ( '@BaseConfig#INIT' ) ;
20- const ORIGNAL_CONFIG = Symbol ( '@BaseConfig#ORIGNAL_CONFIG' ) ;
21+ const KEY_ORIGNAL_CONFIG = Symbol ( '@BaseConfig#KEY_ORIGNAL_CONFIG' ) ;
22+ const KEY_PACKAGE = Symbol ( '@BaseConfig#KEY_PACKAGE' ) ;
23+ const KEY_PACKAGE_PATH = Symbol ( '@BaseConfig#KEY_PACKAGE_PATH' ) ;
2124
2225class BaseConfig {
2326
@@ -29,7 +32,7 @@ class BaseConfig {
2932 constructor ( config /* , opts = {} */ ) {
3033 // 校验 config
3134 this . _validateSchema ( config ) ;
32- this [ ORIGNAL_CONFIG ] = config ;
35+ this [ KEY_ORIGNAL_CONFIG ] = config ;
3336 this [ INIT ] ( ) ;
3437 }
3538
@@ -49,29 +52,32 @@ class BaseConfig {
4952 [ INIT ] ( ) {
5053 if ( ! this . config [ Symbols . LOAD_SUCCESS ] ) {
5154 // 文件未加载成功.
52- logger . error ( `Not Found "${ CONSTANTS . CONFIG_NAME } "` ) ;
55+ logger . warn ( `Not Found "${ CONSTANTS . CONFIG_NAME } "` ) ;
56+ logger . warn ( `You must be to create "${ CONSTANTS . CONFIG_NAME } " in "${ this . root } "` ) ;
5357 }
5458 if ( this . root ) {
5559 try {
5660 const packagePath = path . resolve ( this . root , CONSTANTS . PACKAGE_JSON ) ;
5761 if ( fs . existsSync ( packagePath ) ) {
58- this . _packagePath = packagePath ;
59- this . _package = require ( packagePath ) ;
62+ this [ KEY_PACKAGE_PATH ] = packagePath ;
63+ this [ KEY_PACKAGE ] = require ( packagePath ) ;
6064 if ( ! this . config [ Symbols . LOAD_SUCCESS ] ) {
61- // 文件未加载成功.
62- // TODO 可以从 package.json 中查询配置文件
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+ }
6369 }
6470 }
6571 } catch ( error ) {
66- this . _packagePath = '' ;
67- this . _package = { } ;
72+ this [ KEY_PACKAGE_PATH ] = '' ;
73+ this [ KEY_PACKAGE ] = { } ;
6874 logger . warn ( `Not Fount "${ CONSTANTS . PACKAGE_JSON } " !` ) ;
6975 }
7076 }
7177 }
7278
7379 get config ( ) {
74- return this [ ORIGNAL_CONFIG ] || { } ;
80+ return this [ KEY_ORIGNAL_CONFIG ] || { } ;
7581 }
7682
7783 get root ( ) {
@@ -119,11 +125,11 @@ class BaseConfig {
119125 }
120126
121127 get packagePath ( ) {
122- return this . _packagePath ;
128+ return this [ KEY_PACKAGE_PATH ] || '' ;
123129 }
124130
125131 get package ( ) {
126- return Object . freeze ( JSON . parse ( JSON . stringify ( this . _package || { } ) ) ) ;
132+ return Object . freeze ( JSON . parse ( JSON . stringify ( this [ KEY_PACKAGE ] || { } ) ) ) ;
127133 }
128134
129135 get key ( ) {
@@ -178,13 +184,13 @@ class BaseConfig {
178184 if ( currShared ) { // 兼容旧版
179185 return Object . keys ( currShared ) . reduce ( ( obj , key ) => {
180186 const aliasObj = currShared [ key ] ;
181- if ( aliasObj && typeof aliasObj === 'string' ) {
187+ if ( aliasObj && _ . isString ( aliasObj ) ) {
182188 obj [ key ] = {
183189 link : aliasObj ,
184190 } ;
185- } else if ( aliasObj && typeof aliasObj === 'object' ) {
191+ } else if ( aliasObj && _ . isPlainObject ( aliasObj ) ) {
186192 const link = aliasObj . link ;
187- if ( link && typeof link === 'string' ) {
193+ if ( link && _ . isString ( link ) ) {
188194 obj [ key ] = aliasObj ;
189195 }
190196 }
@@ -194,13 +200,13 @@ class BaseConfig {
194200 const currAlias = config . alias || { } ;
195201 return Object . keys ( currAlias ) . reduce ( ( obj , key ) => {
196202 const aliasObj = currAlias [ key ] ;
197- if ( aliasObj && typeof aliasObj === 'string' ) {
203+ if ( aliasObj && _ . isString ( aliasObj ) ) {
198204 obj [ key ] = {
199205 link : aliasObj ,
200206 } ;
201- } else if ( aliasObj && typeof aliasObj === 'object' ) {
207+ } else if ( aliasObj && _ . isPlainObject ( aliasObj ) ) {
202208 const link = aliasObj . link ;
203- if ( link && typeof link === 'string' ) {
209+ if ( link && _ . isString ( link ) ) {
204210 obj [ key ] = aliasObj ;
205211 }
206212 }
@@ -226,7 +232,7 @@ class BaseConfig {
226232 Object . keys ( currShared ) . forEach ( k => {
227233 const p = currShared [ k ] ;
228234 const aliasKey = `${ aliasName } /${ k } ` ;
229- if ( ! alias [ aliasKey ] && typeof p === 'string' ) {
235+ if ( ! alias [ aliasKey ] && _ . isString ( p ) ) {
230236 const filePath = path . resolve ( this . root , p ) ;
231237 alias [ aliasKey ] = filePath ;
232238 }
@@ -241,17 +247,17 @@ class BaseConfig {
241247 const currAlias = config . alias || { } ;
242248 return Object . keys ( currAlias ) . reduce ( ( obj , key ) => {
243249 const aliasObj = currAlias [ key ] ;
244- if ( aliasObj && typeof aliasObj === 'string' ) {
250+ if ( aliasObj && _ . isString ( aliasObj ) ) {
245251 obj [ key ] = {
246252 link : aliasObj ,
247253 } ;
248254 } else if ( aliasObj && _ . isPlainObject ( aliasObj ) ) {
249- if ( aliasObj . server === true || typeof aliasObj . type === 'string' && aliasObj . type . toUpperCase ( ) === 'SERVER' ) {
255+ if ( aliasObj . server === true || _ . isString ( aliasObj . type ) && aliasObj . type . toUpperCase ( ) === 'SERVER' ) {
250256 // server ?
251257 return obj ;
252258 }
253259 const link = aliasObj . link ;
254- if ( link && typeof link === 'string' ) {
260+ if ( link && _ . isString ( link ) ) {
255261 obj [ key ] = aliasObj ;
256262 }
257263 }
@@ -276,7 +282,7 @@ class BaseConfig {
276282 Object . keys ( currAlias ) . forEach ( key => {
277283 const p = currAlias [ key ] ;
278284 const aliasKey = `${ aliasName } /${ key } ` ;
279- if ( ! alias [ aliasKey ] && typeof p === 'string' ) {
285+ if ( ! alias [ aliasKey ] && _ . isString ( p ) ) {
280286 const filePath = path . resolve ( this . root , p ) ;
281287 alias [ aliasKey ] = filePath ;
282288 }
0 commit comments