Skip to content

Commit

Permalink
feat: accept promise-based transform function
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Jun 12, 2020
1 parent 038a05e commit 19123c1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
14 changes: 8 additions & 6 deletions 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';
Expand All @@ -20,7 +21,7 @@ import {

const debug = Debug('cordova-res:image');

export type SharpTransformation = (pipeline: Sharp) => Sharp;
export type SharpTransformation = (pipeline: Sharp) => Promise<Sharp> | Sharp;

/**
* Check an array of source files, returning the first viable image.
Expand Down Expand Up @@ -207,20 +208,21 @@ export async function generateImage(
);
}

const pipeline = applyTransformations(src, [
const pipeline = await applyTransformations(src, [
createImageResizer(image),
createImageConverter(image.format),
]);

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<Sharp> {
return reduce(
transformations,
async (pipeline, transformation) => transformation(pipeline),
src,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -19,6 +19,7 @@ import {
PLATFORMS,
Platform,
RunPlatformOptions,
TransformFunction,
prettyPlatform,
run as runPlatform,
} from './platform';
Expand Down Expand Up @@ -197,7 +198,7 @@ namespace CordovaRes {
*
* @returns Sharp object
*/
readonly transform?: (image: ImageSchema, pipeline: Sharp) => Sharp;
readonly transform?: TransformFunction;
};

/**
Expand Down
27 changes: 16 additions & 11 deletions src/platform.ts
Expand Up @@ -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> | Sharp;

export interface ResourceOptions<S> {
/**
Expand Down Expand Up @@ -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);
},
);
}

/**
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 19123c1

Please sign in to comment.