Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix #318 #392

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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