import { generateImageAsync } from "@expo/image-utils" ; import fs from "fs" ; import { join } from "path" ; export type ContentsJsonImageIdiom = "iphone" | "ipad" | "ios-marketing" | "universal" ; export type ContentsJsonImageAppearance = { appearance : "luminosity" ; value : "dark" ; } ; export type ContentsJsonImageScale = "1x" | "2x" | "3x" ; export interface ContentsJsonImage { appearances ? : ContentsJsonImageAppearance [ ] ; idiom : ContentsJsonImageIdiom ; size ? : string ; scale : ContentsJsonImageScale ; filename ? : string ; platform ? : string ; } const IMAGE_CACHE_NAME = "ios-stickers-icons" ; const ICON_CONTENTS = [ { idiom : "iphone" , sizes : [ { size : 29 , scales : [ 2 , 3 ] , } , { size : [ 60 , 45 ] , scales : [ 2 , 3 ] , } , ] , } , { idiom : "ipad" , sizes : [ { size : 29 , scales : [ 2 ] , } , { size : [ 67 , 50 ] , scales : [ 2 ] , } , { size : [ 74 , 55 ] , scales : [ 2 ] , } , ] , } , { idiom : "universal" , sizes : [ { platform : "ios" , size : [ 27 , 20 ] , scales : [ 2 , 3 ] , } , { platform : "ios" , size : [ 32 , 24 ] , scales : [ 2 , 3 ] , } , ] , } , { idiom : "ios-marketing" , sizes : [ { platform : "ios" , size : 1024 , scales : [ 1 ] , } , { size : [ 1024 , 768 ] , scales : [ 1 ] , } , ] , } , ] ; export async function generateImessageIconsAsync ( projectRoot , icon , iconsPath ) { fs . mkdirSync ( iconsPath , { recursive : true } ) ; const imagesJson = [ ] ; const generatedIcons = { } ; for ( const platform of ICON_CONTENTS ) { for ( const { size , scales , ... rest } of platform . sizes ) { for ( const scale of scales ) { const filename = getAppleIconName ( size , scale , platform . idiom ) ; const [ width , height ] = Array . isArray ( size ) ? size : [ size , size ] ; if ( ! ( filename in generatedIcons ) ) { const iconWSizePx = width * scale ; const iconHSizePx = height * scale ; const { source } = await generateImageAsync ( { projectRoot , cacheType : IMAGE_CACHE_NAME } , { src : icon , name : filename , width : iconWSizePx , height : iconHSizePx , removeTransparency : true , resizeMode : "cover" , backgroundColor : "#ffffff" , } ) ; const assetPath = join ( iconsPath , filename ) ; await fs . promises . writeFile ( assetPath , source ) ; generatedIcons [ filename ] = true ; } imagesJson . push ( { idiom : platform . idiom as ContentsJsonImageIdiom , size : `${ width }x${ height }` , scale : `${ scale }x` , filename , } ) ; if ( rest . platform ) { imagesJson [ imagesJson . length - 1 ] . platform = rest . platform ; } } } } return imagesJson ; } function getAppleIconName ( size , scale , idiom ) { const [ width , height ] = Array . isArray ( size ) ? size : [ size , size ] ; return `App-Icon-${ idiom }-${ width }x${ height }@${ scale }x.png` ; }