11'use strict' ;
22
33const 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
106const Symbols = require ( '../../Constants/symbols' ) ;
117const CONSTANTS = require ( '../../Constants' ) ;
128const 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
376354module . exports = BaseConfig ;
0 commit comments