diff --git a/package.json b/package.json index e3f8ae3..1cebd81 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "splash screen" ], "dependencies": { + "@ionic/utils-array": "^2.1.1", "@ionic/utils-fs": "^3.0.0", "debug": "^4.1.1", "elementtree": "^0.1.7", diff --git a/src/image.ts b/src/image.ts index 0a1fbd6..54a109a 100644 --- a/src/image.ts +++ b/src/image.ts @@ -1,3 +1,4 @@ +import { reduce } from '@ionic/utils-array'; import { readFile, writeFile } from '@ionic/utils-fs'; import Debug from 'debug'; import sharp, { Metadata, Sharp } from 'sharp'; @@ -20,7 +21,7 @@ import { const debug = Debug('cordova-res:image'); -export type SharpTransformation = (pipeline: Sharp) => Sharp; +export type SharpTransformation = (pipeline: Sharp) => Promise | Sharp; /** * Check an array of source files, returning the first viable image. @@ -207,7 +208,7 @@ export async function generateImage( ); } - const pipeline = applyTransformations(src, [ + const pipeline = await applyTransformations(src, [ createImageResizer(image), createImageConverter(image.format), ]); @@ -215,12 +216,13 @@ export async function generateImage( await writeFile(image.src, await pipeline.toBuffer()); } -export function applyTransformations( +export async function applyTransformations( src: Sharp, transformations: readonly SharpTransformation[], -): Sharp { - return transformations.reduce( - (pipeline, transformation) => transformation(pipeline), +): Promise { + return reduce( + transformations, + async (pipeline, transformation) => transformation(pipeline), src, ); } diff --git a/src/index.ts b/src/index.ts index d982db1..1f134a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ import { PLATFORMS, Platform, RunPlatformOptions, + TransformFunction, prettyPlatform, run as runPlatform, } from './platform'; @@ -197,7 +198,7 @@ namespace CordovaRes { * * @returns Sharp object */ - readonly transform?: (image: ImageSchema, pipeline: Sharp) => Sharp; + readonly transform?: TransformFunction; }; /** diff --git a/src/platform.ts b/src/platform.ts index 285a16a..19c4175 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -49,7 +49,10 @@ export interface AndroidAdaptiveIconResourcePart { readonly [ResourceKey.SRC]: string; } -export type TransformFunction = (image: ImageSchema, pipeline: Sharp) => Sharp; +export type TransformFunction = ( + image: ImageSchema, + pipeline: Sharp, +) => Promise | Sharp; export interface ResourceOptions { /** @@ -291,17 +294,19 @@ export function getResourceTransformFunction( export function combineTransformFunctions( transformations: readonly TransformFunction[], ): TransformFunction { - return transformations.reduce((acc, transformation) => (image, pipeline) => { - const result = acc(image, pipeline); + return transformations.reduce( + (acc, transformation) => async (image, pipeline) => { + const result = await acc(image, pipeline); - if (!result || typeof result !== 'object') { - throw new BadInputError( - `Invalid Sharp pipeline returned while performing transforms: ${result}`, - ); - } + if (!result || typeof result !== 'object') { + throw new BadInputError( + `Invalid Sharp pipeline returned while performing transforms: ${result}`, + ); + } - return transformation(image, result); - }); + return transformation(image, result); + }, + ); } /** @@ -553,7 +558,7 @@ export async function generateImageResource( await ensureDir(pathlib.dirname(dest)); - const img = transform(generatedImage, pipeline.clone()); + const img = await transform(generatedImage, pipeline.clone()); await generateImage(generatedImage, img, metadata, errstream);