Skip to content

Commit

Permalink
Update dependencies and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Jul 25, 2023
1 parent 545d0b5 commit 18f9ea5
Show file tree
Hide file tree
Showing 18 changed files with 5,064 additions and 3,154 deletions.
8,078 changes: 4,994 additions & 3,084 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@
"dependencies": {
"escape-html": "^1.0.3",
"sharp": "^0.32.4",
"xml2js": "^0.5.0"
"xml2js": "^0.6.1"
},
"devDependencies": {
"@babel/core": "^7.19.6",
"@babel/preset-env": "^7.19.4",
"@babel/preset-typescript": "^7.18.6",
"@types/escape-html": "^1.0.2",
"@types/xml2js": "^0.4.11",
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.1.3",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-prettier": "^5.0.0",
"icojs": "^0.17.0",
"jest": "^29.2.1",
"jest-image-snapshot": "^6.1.0",
"prettier": "^2.7.1",
"rimraf": "^4.3.1",
"prettier": "^3.0.0",
"rimraf": "^5.0.1",
"typescript": "^5.0.4",
"unbuild": "^1.1.2"
},
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ await fs.mkdir(dest, { recursive: true });
await Promise.all(
response.images.map(
async (image) =>
await fs.writeFile(path.join(dest, image.name), image.contents)
)
await fs.writeFile(path.join(dest, image.name), image.contents),
),
);
await Promise.all(
response.files.map(
async (file) =>
await fs.writeFile(path.join(dest, file.name), file.contents)
)
await fs.writeFile(path.join(dest, file.name), file.contents),
),
);
await fs.writeFile(path.join(dest, htmlBasename), response.html.join("\n"));
```
Expand Down
32 changes: 16 additions & 16 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ function minByKey<T>(array: T[], keyFn: (e: T) => unknown): T {

export function mapValues<T, U>(
dict: Record<string, T>,
mapper: (value: T, key: string) => U
mapper: (value: T, key: string) => U,
): Record<string, U> {
return Object.fromEntries(
Object.entries(dict).map(([key, value]) => [key, mapper(value, key)])
Object.entries(dict).map(([key, value]) => [key, mapper(value, key)]),
);
}

export function filterKeys<T>(
dict: Record<string, T>,
predicate: (key: string) => boolean
predicate: (key: string) => boolean,
): Record<string, T> {
return Object.fromEntries(
Object.entries(dict).filter((pair) => predicate(pair[0]))
Object.entries(dict).filter((pair) => predicate(pair[0])),
);
}

Expand All @@ -67,7 +67,7 @@ export function asString(arg: unknown): string | undefined {
}

export async function sourceImages(
src: string | Buffer | (string | Buffer)[]
src: string | Buffer | (string | Buffer)[],
): Promise<SourceImage[]> {
if (Buffer.isBuffer(src)) {
try {
Expand Down Expand Up @@ -109,7 +109,7 @@ function flattenIconOptions(iconOptions: IconOptions): IconPlaneOptions[] {

export function relativeTo(
base: string | undefined | null,
path: string
path: string,
): string {
if (!base) {
return path;
Expand All @@ -124,7 +124,7 @@ export function relativeTo(
function bestSource(
sourceset: SourceImage[],
width: number,
height: number
height: number,
): SourceImage {
const sideSize = Math.max(width, height);
return minByKey(sourceset, (icon) => {
Expand All @@ -141,7 +141,7 @@ async function resize(
source: SourceImage,
width: number,
height: number,
pixelArt: boolean
pixelArt: boolean,
): Promise<Buffer> {
if (source.metadata.format === "svg") {
const options = {
Expand Down Expand Up @@ -177,7 +177,7 @@ async function resize(
function createBlankImage(
width: number,
height: number,
background?: string
background?: string,
): sharp.Sharp {
const transparent = !background || background === "transparent";

Expand All @@ -198,11 +198,11 @@ function createBlankImage(

async function createPlane(
sourceset: SourceImage[],
options: IconPlaneOptions
options: IconPlaneOptions,
): Promise<sharp.Sharp> {
const offset =
Math.round(
(Math.max(options.width, options.height) * options.offset) / 100
(Math.max(options.width, options.height) * options.offset) / 100,
) || 0;
const width = options.width - offset * 2;
const height = options.height - offset * 2;
Expand All @@ -213,7 +213,7 @@ async function createPlane(
let pipeline = createBlankImage(
options.width,
options.height,
options.background
options.background,
).composite([{ input: image, left: offset, top: offset }]);

if (options.rotate) {
Expand All @@ -237,7 +237,7 @@ function toPng(pipeline: sharp.Sharp): Promise<Buffer> {

async function createSvg(
sourceset: SourceImage[],
options: IconPlaneOptions
options: IconPlaneOptions,
): Promise<Buffer> {
const { width, height } = options;
const source = bestSource(sourceset, width, height);
Expand All @@ -250,22 +250,22 @@ async function createSvg(
return Buffer.from(
`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}">
<image width="${width}" height="${height}" xlink:href="data:image/png;base64,${encodedPng}"/>
</svg>`
</svg>`,
);
}
}

export async function createFavicon(
sourceset: SourceImage[],
name: string,
iconOptions: IconOptions
iconOptions: IconOptions,
): Promise<FaviconImage> {
const properties = flattenIconOptions(iconOptions);
const ext = extname(name);

if (ext === ".ico" || properties.length !== 1) {
const images = await Promise.all(
properties.map((props) => createPlane(sourceset, props).then(toRawImage))
properties.map((props) => createPlane(sourceset, props).then(toRawImage)),
);
const contents = toIco(images);
return { name, contents };
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface FaviconResponse {

export async function favicons(
source: string | Buffer | (string | Buffer)[],
options: FaviconOptions = {}
options: FaviconOptions = {},
): Promise<FaviconResponse> {
options = {
...defaultOptions,
Expand Down Expand Up @@ -86,7 +86,7 @@ class FaviconStream extends Transform {
override _transform(
file: any, // eslint-disable-line @typescript-eslint/no-explicit-any -- superclass uses any
_encoding: BufferEncoding,
callback: TransformCallback
callback: TransformCallback,
) {
const { html: htmlPath, pipeHTML, ...options } = this.#options;

Expand Down
28 changes: 14 additions & 14 deletions src/platforms/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ export class AndroidPlatform extends Platform {
constructor(options: FaviconOptions) {
super(
options,
uniformIconOptions(options, options.icons.android, ICONS_OPTIONS)
uniformIconOptions(options, options.icons.android, ICONS_OPTIONS),
);
}

override async createImages(
sourceset: SourceImage[]
sourceset: SourceImage[],
): Promise<FaviconImage[]> {
let icons = await Promise.all(
this.iconOptions.map((iconOption) =>
createFavicon(sourceset, iconOption.name, iconOption)
)
createFavicon(sourceset, iconOption.name, iconOption),
),
);

// Generate android maskable images from a different source set
Expand All @@ -111,13 +111,13 @@ export class AndroidPlatform extends Platform {
typeof this.options.manifestMaskable !== "boolean"
) {
const maskableSourceset = await sourceImages(
this.options.manifestMaskable
this.options.manifestMaskable,
);

const maskableIcons = await Promise.all(
ICONS_OPTIONS_MASKABLE.map((iconOption) =>
createFavicon(maskableSourceset, iconOption.name, iconOption)
)
createFavicon(maskableSourceset, iconOption.name, iconOption),
),
);

icons = [...icons, ...maskableIcons];
Expand Down Expand Up @@ -160,11 +160,11 @@ export class AndroidPlatform extends Platform {
createFavicon(
shortcutSourceset,
`shortcut${index + 1}-${shortcutName}`,
option
)
)
option,
),
),
);
})
}),
);
return icons.flat();
}
Expand Down Expand Up @@ -259,13 +259,13 @@ export class AndroidPlatform extends Platform {
src: this.cacheBusting(
relativeTo(
basePath,
`shortcut${index + 1}-${shortcutName}`
)
`shortcut${index + 1}-${shortcutName}`,
),
),
sizes: `${width}x${height}`,
type: "image/png",
};
}
},
)
: undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/appleIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AppleIconPlatform extends Platform {
constructor(options: FaviconOptions) {
super(
options,
uniformIconOptions(options, options.icons.appleIcon, ICONS_OPTIONS)
uniformIconOptions(options, options.icons.appleIcon, ICONS_OPTIONS),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/platforms/appleStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class AppleStartupPlatform extends Platform<AppleStartupImage> {
constructor(options: FaviconOptions) {
super(
options,
uniformIconOptions(options, options.icons.appleStartup, ICONS_OPTIONS)
uniformIconOptions(options, options.icons.appleStartup, ICONS_OPTIONS),
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/platforms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export function uniformIconOptions<T extends NamedIconOptions>(
| boolean
| (string | NamedIconOptions)[]
| undefined,
platformConfig: (T & OptionalMixin)[]
platformConfig: (T & OptionalMixin)[],
): T[] {
let result = [];
if (Array.isArray(iconsChoice)) {
const iconsChoices = Object.fromEntries(
iconsChoice.map((choice) =>
typeof choice === "object"
? [choice.name, choice]
: [choice, { name: choice }]
)
: [choice, { name: choice }],
),
);
result = platformConfig
.filter((iconOptions) => iconOptions.name in iconsChoices)
Expand Down Expand Up @@ -81,8 +81,8 @@ export class Platform<IO extends NamedIconOptions = NamedIconOptions> {
async createImages(sourceset: SourceImage[]): Promise<FaviconImage[]> {
return await Promise.all(
this.iconOptions.map((iconOption) =>
createFavicon(sourceset, iconOption.name, iconOption)
)
createFavicon(sourceset, iconOption.name, iconOption),
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/platforms/favicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class FaviconsPlatform extends Platform {
constructor(options: FaviconOptions) {
super(
options,
uniformIconOptions(options, options.icons.favicons, ICONS_OPTIONS)
uniformIconOptions(options, options.icons.favicons, ICONS_OPTIONS),
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/platforms/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class WindowsPlatform extends Platform {
constructor(options: FaviconOptions) {
super(
options,
uniformIconOptions(options, options.icons.windows, ICONS_OPTIONS)
uniformIconOptions(options, options.icons.windows, ICONS_OPTIONS),
);
if (!this.options.background) {
throw new Error("`background` is required for Windows icons");
Expand Down Expand Up @@ -69,7 +69,7 @@ export class WindowsPlatform extends Platform {

for (const { name, ...size } of SUPPORTED_TILES) {
const icon = this.iconOptions.find((iconOption) =>
hasSize(size, iconOption)
hasSize(size, iconOption),
);

if (icon) {
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/yandex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class YandexPlatform extends Platform {
constructor(options: FaviconOptions) {
super(
options,
uniformIconOptions(options, options.icons.yandex, ICONS_OPTIONS)
uniformIconOptions(options, options.icons.yandex, ICONS_OPTIONS),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/svgtool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import sharp from "sharp";
export function svgDensity(
metadata: sharp.Metadata,
width: number,
height: number
height: number,
): number | undefined {
if (!metadata.width || !metadata.height) {
return undefined;
Expand All @@ -28,8 +28,8 @@ export function svgDensity(
1,
currentDensity,
(currentDensity * width) / metadata.width,
(currentDensity * height) / metadata.height
(currentDensity * height) / metadata.height,
),
100000
100000,
);
}
Loading

0 comments on commit 18f9ea5

Please sign in to comment.