@@ -8,12 +8,8 @@ export const enum Operations {
88 Resize = "resize"
99}
1010
11- interface IGenerateImagesData extends ISplashesGenerationData {
12- propertiesToEnumerate : string [ ] ;
13- }
14-
1511export class AssetsGenerationService implements IAssetsGenerationService {
16- private get propertiesToEnumerate ( ) : any {
12+ private get propertiesToEnumerate ( ) : IDictionary < string [ ] > {
1713 return {
1814 icon : [ "icons" ] ,
1915 splash : [ "splashBackgrounds" , "splashCenterImages" , "splashImages" ]
@@ -27,65 +23,56 @@ export class AssetsGenerationService implements IAssetsGenerationService {
2723 @exported ( "assetsGenerationService" )
2824 public async generateIcons ( resourceGenerationData : IResourceGenerationData ) : Promise < void > {
2925 this . $logger . info ( "Generating icons ..." ) ;
30- const generationData = ( < IGenerateImagesData > resourceGenerationData ) ;
31- generationData . propertiesToEnumerate = this . propertiesToEnumerate . icon ;
32- await this . generateImagesForDefinitions ( generationData ) ;
26+ await this . generateImagesForDefinitions ( resourceGenerationData , this . propertiesToEnumerate . icon ) ;
3327 this . $logger . info ( "Icons generation completed." ) ;
3428 }
3529
3630 @exported ( "assetsGenerationService" )
3731 public async generateSplashScreens ( splashesGenerationData : ISplashesGenerationData ) : Promise < void > {
3832 this . $logger . info ( "Generating splash screens ..." ) ;
39- const generationData = ( < IGenerateImagesData > splashesGenerationData ) ;
40- generationData . propertiesToEnumerate = this . propertiesToEnumerate . splash ;
41- await this . generateImagesForDefinitions ( generationData ) ;
33+ await this . generateImagesForDefinitions ( splashesGenerationData , this . propertiesToEnumerate . splash ) ;
4234 this . $logger . info ( "Splash screens generation completed." ) ;
4335 }
4436
45- private async generateImagesForDefinitions ( data : IGenerateImagesData ) : Promise < void > {
46- data . background = data . background || "white" ;
47- const assetsStructure = await this . $projectDataService . getAssetsStructure ( { projectDir : data . projectDir } ) ;
48-
49- for ( const platform in assetsStructure ) {
50- if ( data . platform && platform . toLowerCase ( ) !== data . platform . toLowerCase ( ) ) {
51- continue ;
52- }
53-
54- const platformAssetsStructure = assetsStructure [ platform ] ;
55-
56- for ( const imageTypeKey in platformAssetsStructure ) {
57- if ( data . propertiesToEnumerate . indexOf ( imageTypeKey ) === - 1 || ! platformAssetsStructure [ imageTypeKey ] ) {
58- continue ;
59- }
60-
61- const imageType = platformAssetsStructure [ imageTypeKey ] ;
62-
63- for ( const assetItem of imageType . images ) {
64- if ( ! assetItem . filename ) {
65- continue ;
66- }
67-
68- const operation = assetItem . resizeOperation || Operations . Resize ;
69- const scale = assetItem . scale || 0.8 ;
70- const outputPath = assetItem . path ;
71-
72- switch ( operation ) {
73- case Operations . OverlayWith :
74- const imageResize = Math . round ( Math . min ( assetItem . width , assetItem . height ) * scale ) ;
75- const image = await this . resize ( data . imagePath , imageResize , imageResize ) ;
76- await this . generateImage ( data . background , assetItem . width , assetItem . height , outputPath , image ) ;
77- break ;
78- case Operations . Blank :
79- await this . generateImage ( data . background , assetItem . width , assetItem . height , outputPath ) ;
80- break ;
81- case Operations . Resize :
82- const resizedImage = await this . resize ( data . imagePath , assetItem . width , assetItem . height ) ;
83- resizedImage . write ( outputPath ) ;
84- break ;
85- default :
86- throw new Error ( `Invalid image generation operation: ${ operation } ` ) ;
87- }
88- }
37+ private async generateImagesForDefinitions ( generationData : ISplashesGenerationData , propertiesToEnumerate : string [ ] ) : Promise < void > {
38+ generationData . background = generationData . background || "white" ;
39+ const assetsStructure = await this . $projectDataService . getAssetsStructure ( generationData ) ;
40+
41+ const assetItems = _ ( assetsStructure )
42+ . filter ( ( assetGroup : IAssetGroup , platform : string ) => {
43+ return ! generationData . platform || platform . toLowerCase ( ) === generationData . platform . toLowerCase ( ) ;
44+ } )
45+ . map ( ( assetGroup : IAssetGroup ) =>
46+ _ . filter ( assetGroup , ( assetSubGroup : IAssetSubGroup , imageTypeKey : string ) =>
47+ propertiesToEnumerate . indexOf ( imageTypeKey ) !== - 1 && ! assetSubGroup [ imageTypeKey ]
48+ )
49+ )
50+ . flatten < IAssetSubGroup > ( )
51+ . map ( assetSubGroup => assetSubGroup . images )
52+ . flatten < IAssetItem > ( )
53+ . filter ( assetItem => ! ! assetItem . filename )
54+ . value ( ) ;
55+
56+ for ( const assetItem of assetItems ) {
57+ const operation = assetItem . resizeOperation || Operations . Resize ;
58+ const scale = assetItem . scale || 0.8 ;
59+ const outputPath = assetItem . path ;
60+
61+ switch ( operation ) {
62+ case Operations . OverlayWith :
63+ const imageResize = Math . round ( Math . min ( assetItem . width , assetItem . height ) * scale ) ;
64+ const image = await this . resize ( generationData . imagePath , imageResize , imageResize ) ;
65+ await this . generateImage ( generationData . background , assetItem . width , assetItem . height , outputPath , image ) ;
66+ break ;
67+ case Operations . Blank :
68+ await this . generateImage ( generationData . background , assetItem . width , assetItem . height , outputPath ) ;
69+ break ;
70+ case Operations . Resize :
71+ const resizedImage = await this . resize ( generationData . imagePath , assetItem . width , assetItem . height ) ;
72+ resizedImage . write ( outputPath ) ;
73+ break ;
74+ default :
75+ throw new Error ( `Invalid image generation operation: ${ operation } ` ) ;
8976 }
9077 }
9178 }
0 commit comments