@@ -481,14 +481,13 @@ const splitNameVersion = function (str: string) {
481481 } ;
482482} ;
483483
484- const getDependencies = ( dependencies : Record < string , string > , packagePath : string ) => {
485- if ( fs . existsSync ( packagePath ) ) return ;
484+ const getDependencies = ( packagePath : string ) => {
485+ if ( ! fs . existsSync ( packagePath ) ) {
486+ const { name : moduleName , version } = splitNameVersion ( packagePath ) ;
486487
487- const { name : moduleName , version } = splitNameVersion ( packagePath ) ;
488-
489- if ( ! moduleName ) return ;
490-
491- dependencies [ moduleName ] = version ;
488+ return [ moduleName , version ] ;
489+ }
490+ return [ ] ;
492491} ;
493492
494493const setPackages = ( packages : ModuleMainFilePath , app : App , packagePath : string , cwd : string , key ?: string ) => {
@@ -597,38 +596,29 @@ const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string
597596 }
598597} ;
599598
600- const flattenPackagesConfig = ( packages : ( string | Record < string , string > ) [ ] ) => {
601- const packagesConfig : ( [ string ] | [ string , string ] ) [ ] = [ ] ;
602- packages . forEach ( ( item ) => {
603- if ( typeof item === 'object' ) {
604- for ( const [ key , packagePath ] of Object . entries ( item ) ) {
605- const index = packagesConfig . findIndex ( ( [ , k ] ) => {
606- return k === key ;
607- } ) ;
608- if ( index > - 1 ) {
609- packagesConfig [ index ] = [ packagePath , key ] ;
610- } else {
611- packagesConfig . push ( [ packagePath , key ] ) ;
612- }
613- }
614- } else if ( typeof item === 'string' ) {
615- if ( packagesConfig . find ( ( [ k ] ) => k === item ) ) {
616- return ;
617- }
618- packagesConfig . push ( [ item ] ) ;
619- }
620- } ) ;
621- return packagesConfig ;
622- } ;
623-
624599export const resolveAppPackages = ( app : App ) : ModuleMainFilePath => {
625600 const dependencies : Record < string , string > = { } ;
626601
627602 const { packages = [ ] , npmConfig = { } , source } = app . options ;
628603
629- const packagePaths = flattenPackagesConfig ( packages ) ;
604+ // 将 packages 统一展开为 [packagePath, key?] 的形式
605+ const resolvedPackages : [ string , string ?] [ ] = [ ] ;
606+ for ( const item of packages ) {
607+ if ( typeof item === 'string' ) {
608+ resolvedPackages . push ( [ item ] ) ;
609+ } else if ( typeof item === 'object' ) {
610+ for ( const [ key , packagePath ] of Object . entries ( item ) ) {
611+ resolvedPackages . push ( [ packagePath , key ] ) ;
612+ }
613+ }
614+ }
630615
631- packagePaths . forEach ( ( [ packagePath ] ) => getDependencies ( dependencies , packagePath ) ) ;
616+ for ( const [ packagePath ] of resolvedPackages ) {
617+ const [ moduleName , version ] = getDependencies ( packagePath ) ;
618+ if ( moduleName && version ) {
619+ dependencies [ moduleName ] = version ;
620+ }
621+ }
632622
633623 if ( npmConfig . autoInstall && Object . keys ( dependencies ) . length ) {
634624 if ( ! npmConfig . keepPackageJsonClean ) {
@@ -659,7 +649,9 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
659649 dsValueMap : { } ,
660650 } ;
661651
662- packagePaths . forEach ( ( [ packagePath , key ] ) => setPackages ( packagesMap , app , packagePath , source , key ) ) ;
652+ for ( const [ packagePath , key ] of resolvedPackages ) {
653+ setPackages ( packagesMap , app , packagePath , source , key ) ;
654+ }
663655
664656 return packagesMap ;
665657} ;
0 commit comments