Skip to content

Commit

Permalink
Attempt to fix #318
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed May 3, 2022
1 parent 797c876 commit e21a6c1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
35 changes: 26 additions & 9 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "path";
import * as fs from "fs";
import { platform } from "process";
import sharp from "sharp";
import { toIco } from "./ico";
import { FaviconImage } from "./index";
Expand Down Expand Up @@ -90,7 +91,7 @@ export async function sourceImages(
if (!src.length) {
throw new Error("No source provided");
}
const images = await Promise.all(src.map(sourceImages));
const images = await mapAsync(src, sourceImages);

return images.flat();
} else {
Expand Down Expand Up @@ -244,14 +245,12 @@ export class Images {
const properties = flattenIconOptions(iconOptions);

if (path.extname(name) === ".ico" || properties.length !== 1) {
const images = await Promise.all(
properties.map((props) =>
this.createPlaneFavicon(
sourceset,
props,
`${props.width}x${props.height}.rawdata`,
true
)
const images = await mapAsync(properties, (props) =>
this.createPlaneFavicon(
sourceset,
props,
`${props.width}x${props.height}.rawdata`,
true
)
);
const contents = toIco(images.map((image) => image.contents as RawImage));
Expand All @@ -265,3 +264,21 @@ export class Images {
return await this.createPlaneFavicon(sourceset, properties[0], name, false);
}
}

export async function mapAsync<T, U>(
inputs: T[],
computation: (input: T, index: number) => Promise<U>
): Promise<U[]> {
if (platform === "win32") {
// run sequentially
const result: U[] = [];
let index = 0;
for (const input of inputs) {
result.push(await computation(input, index));
index++;
}
return result;
} else {
return await Promise.all(inputs.map(computation));
}
}
26 changes: 14 additions & 12 deletions src/platforms/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { maskable, transparentIcon } from "../config/icons";
import {
Dictionary,
Images,
mapAsync,
relativeTo,
SourceImage,
sourceImages,
Expand Down Expand Up @@ -69,10 +70,10 @@ export class AndroidPlatform extends Platform {
async createImages(sourceset: SourceImage[]): Promise<FaviconImage[]> {
const images = new Images();

let icons = await Promise.all(
Object.entries(this.iconOptions).map(([iconName, iconOption]) =>
let icons = await mapAsync(
Object.entries(this.iconOptions),
([iconName, iconOption]) =>
images.createFavicon(sourceset, iconName, iconOption)
)
);

// Generate android maskable images from a different source set
Expand All @@ -84,10 +85,10 @@ export class AndroidPlatform extends Platform {
this.options.manifestMaskable
);

const maskable = await Promise.all(
Object.entries(ICONS_OPTIONS_MASKABLE).map(([iconName, iconOption]) =>
const maskable = await mapAsync(
Object.entries(ICONS_OPTIONS_MASKABLE),
([iconName, iconOption]) =>
images.createFavicon(maskableSourceset, iconName, iconOption)
)
);

icons = [...icons, ...maskable];
Expand Down Expand Up @@ -122,20 +123,21 @@ export class AndroidPlatform extends Platform {

private async shortcutIcons(): Promise<FaviconImage[]> {
const images = new Images();
const icons = await Promise.all(
this.options.shortcuts.map(async (shortcut, index) => {
const icons = await mapAsync(
this.options.shortcuts,
async (shortcut, index) => {
if (!shortcut.name || !shortcut.url || !shortcut.icon) return null;
const shortcutSourceset = await sourceImages(shortcut.icon);
return Promise.all(
Object.entries(SHORTCUT_ICONS_OPTIONS).map(([shortcutName, option]) =>
return await mapAsync(
Object.entries(SHORTCUT_ICONS_OPTIONS),
([shortcutName, option]) =>
images.createFavicon(
shortcutSourceset,
`shortcut${index + 1}-${shortcutName}`,
option
)
)
);
})
}
);
return icons.flat();
}
Expand Down
7 changes: 4 additions & 3 deletions src/platforms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
filterKeys,
Images,
mapValues,
mapAsync,
relativeTo,
SourceImage,
} from "../helpers";
Expand Down Expand Up @@ -62,10 +63,10 @@ export class Platform<IO extends IconOptions = IconOptions> {

async createImages(sourceset: SourceImage[]): Promise<FaviconImage[]> {
const images = new Images();
return await Promise.all(
Object.entries(this.iconOptions).map(([iconName, iconOption]) =>
return await mapAsync(
Object.entries(this.iconOptions),
([iconName, iconOption]) =>
images.createFavicon(sourceset, iconName, iconOption)
)
);
}

Expand Down

0 comments on commit e21a6c1

Please sign in to comment.