import { ConfigPlugin , withDangerousMod } from "@expo/config-plugins" ; import { generateImageAsync } from "@expo/image-utils" ; import fs from "fs" ; import path from "path" ; import { generateImessageIconsAsync } from "./withStickerIcon" ; import { getProjectStickersName } from "./withStickerXcodeTarget" ; const STICKERS_ROOT_PATH = "Stickers.xcassets" ; const IMESSAGE_APP_ICON_PATH = "Stickers.xcassets/iMessage App Icon.stickersiconset" ; const STICKER_PACK_PATH = "Stickers.xcassets/Sticker Pack.stickerpack" ; const defaultInfo = { author : "expo" , version : 1 , } ; const stickersRootContents = { info : defaultInfo , } ; function createSticker ( { stickerpackPath , name , imageBuffer , extension , accessibilityLabel , } ) { const stickerName = `${ name }.sticker` ; const stickerPath = path . join ( stickerpackPath , stickerName ) ; fs . mkdirSync ( stickerPath , { recursive : true } ) ; const imageName = `image${ extension || ".png" }` ; const contents = { info : defaultInfo , properties : { filename : imageName , } , } ; if ( accessibilityLabel ) { contents . properties [ "accessibility-label" ] = accessibilityLabel ; } fs . writeFileSync ( path . join ( stickerPath , imageName ) , imageBuffer ) ; const contentsPath = path . join ( stickerPath , "Contents.json" ) ; fs . writeFileSync ( contentsPath , JSON . stringify ( contents , null , 2 ) ) ; return { name : stickerName , } ; } export const withStickerAssets = ( config , { stickers , icon , size } ) => { if ( ! icon ) { icon = ( config . ios || { } ) . icon || config . icon ; } return withDangerousMod ( config , [ "ios" , async ( config ) => { const stickerPackName = getProjectStickersName ( config . modRequest . projectName ! ) ; const stickerRootPath = path . join ( config . modRequest . platformProjectRoot , stickerPackName ) ; const stickersAssetsPath = path . join ( stickerRootPath , STICKERS_ROOT_PATH ) ; await fs . promises . rmdir ( stickersAssetsPath , { recursive : true } ) . catch ( ( ) => null ) ; const stickersRootContentsJsonPath = path . join ( stickersAssetsPath , "Contents.json" ) ; fs . mkdirSync ( stickersAssetsPath , { recursive : true , } ) ; fs . writeFileSync ( stickersRootContentsJsonPath , JSON . stringify ( stickersRootContents , null , 2 ) ) ; const iMessageAppIconsPath = path . join ( stickerRootPath , IMESSAGE_APP_ICON_PATH ) ; const imessageIconContents = icon ? await generateImessageIconsAsync ( config . modRequest . projectRoot , icon , iMessageAppIconsPath ) : [ ] ; const iMessageAppIconContentsJsonPath = path . join ( iMessageAppIconsPath , "Contents.json" ) ; fs . mkdirSync ( iMessageAppIconsPath , { recursive : true , } ) ; fs . writeFileSync ( iMessageAppIconContentsJsonPath , JSON . stringify ( { info : defaultInfo , images : imessageIconContents } , null , 2 ) ) ; const stickerPackContentsPath = path . join ( stickerRootPath , STICKER_PACK_PATH ) ; const stickersContents = { info : defaultInfo , properties : { "grid-size" : size , } , stickers : [ ] , } ; if ( Array . isArray ( stickers ) ) { for ( const sticker of stickers ) { const src = typeof sticker === "string" ? sticker : sticker . image ; const { source , name : _name } = await generateImageAsync ( { projectRoot : config . modRequest . projectRoot , cacheType : "ios-stickers" , } , { src , } as any ) ; const results = createSticker ( { stickerpackPath : stickerPackContentsPath , name : sticker . name || _name , imageBuffer : source , extension : path . extname ( _name ) , accessibilityLabel : sticker . accessibilityLabel , } ) ; stickersContents . stickers . push ( { filename : results . name , } ) ; } } const stickerPackContentsJsonPath = path . join ( stickerPackContentsPath , "Contents.json" ) ; fs . mkdirSync ( stickerPackContentsPath , { recursive : true , } ) ; fs . writeFileSync ( stickerPackContentsJsonPath , JSON . stringify ( stickersContents , null , 2 ) ) ; return config ; } , ] ) ; } ; export type Sticker = { image : string ; name ? : string ; accessibilityLabel ? : string ; } ; export type Props = { stickers ? : ( string | Sticker ) [ ] ; icon ? : string ; name ? : string ; columns ? : 2 | 3 | 4 ; } ;