|
1 | 1 | import * as path from 'path'
|
2 | 2 | import sharp from 'sharp'
|
3 | 3 |
|
4 |
| -import { iconTypesAndEdgesMap } from './constants' |
5 |
| -import { normalizeOutputTypes, resolveAndCreateOrUseOutputPath, resolveAndCheckInputFilePath } from './helpers' |
| 4 | +import { |
| 5 | + normalizeOutputTypes, |
| 6 | + resolveAndCreateOrUseOutputPath, |
| 7 | + resolveAndCheckInputFilePath, |
| 8 | + forEachIconTypeEdgeIncludes, |
| 9 | + isValidHexColorString, |
| 10 | +} from './helpers' |
6 | 11 | import { IconType } from './types'
|
7 | 12 |
|
8 | 13 | /**
|
9 | 14 | * Generate favicons in various formats from image.
|
10 | 15 | * @param {string} imageInput File from which icons will be generated. Can be path to input file or buffer.
|
11 |
| - * @param {IconType | IconType[]} outputTypes Icon types to be generated. Can be a single type or an array of types. null means all types. |
| 16 | + * @param {IconType | Array<IconType>} outputIconTypes Icon types to be generated. Can be a single type or an array of types. null means all types. |
12 | 17 | * @param {string} outputDirectoryPath Directory where to save icons. If not specified it will be `icons/`
|
13 | 18 | */
|
14 | 19 | export async function faviconize(
|
15 | 20 | imageInput: string | Buffer,
|
16 |
| - outputTypes?: IconType | Array<IconType>, |
| 21 | + outputIconTypes?: IconType | Array<IconType>, |
17 | 22 | outputDirectoryPath?: string,
|
18 | 23 | ) {
|
19 | 24 | const resolvedImageInput = Buffer.isBuffer(imageInput) ? imageInput : await resolveAndCheckInputFilePath(imageInput)
|
20 |
| - const normalizedOutputTypes = normalizeOutputTypes(outputTypes) |
| 25 | + const normalizedOutputTypes = normalizeOutputTypes(outputIconTypes) |
21 | 26 | const resolvedOutputPath = await resolveAndCreateOrUseOutputPath(outputDirectoryPath)
|
22 | 27 |
|
23 |
| - for (const [type, edges] of Object.entries(iconTypesAndEdgesMap)) { |
24 |
| - if (normalizedOutputTypes.has(type as IconType)) { |
25 |
| - try { |
26 |
| - await Promise.all( |
27 |
| - edges.map((edge) => { |
28 |
| - const size = [edge, edge] |
29 |
| - const outputFileAbsolutePath = path.join(resolvedOutputPath, `${type}-${size.join('x')}.png`) |
30 |
| - return sharp(resolvedImageInput) |
31 |
| - .resize(...size) |
32 |
| - .toFile(outputFileAbsolutePath) |
33 |
| - }), |
34 |
| - ) |
35 |
| - } catch (error) { |
36 |
| - console.error(error) |
37 |
| - } |
| 28 | + await forEachIconTypeEdgeIncludes(normalizedOutputTypes, async (type, edge) => { |
| 29 | + const size = [edge, edge] |
| 30 | + const outputFileAbsolutePath = path.join(resolvedOutputPath, `${type}-${size.join('x')}.png`) |
| 31 | + |
| 32 | + await sharp(resolvedImageInput) |
| 33 | + .resize(...size) |
| 34 | + .toFile(outputFileAbsolutePath) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Generate embeddable favicons link tags. |
| 40 | + * @param {IconType | Array<IconType>} outputIconTypes Icon types for whose link tags will be generated. Can be a single type or an array of types. null means all types. |
| 41 | + * @param {string} tileColor Optional HEX (`#rrggbb` or `#rgb`) string representing the color of the tile in Microsoft specific integrations. |
| 42 | + */ |
| 43 | +export async function generateIconsLinkTags(outputIconTypes?: IconType | Array<IconType>, tileColor?: string) { |
| 44 | + const normalizedOutputTypes = normalizeOutputTypes(outputIconTypes) |
| 45 | + const linkTags: Array<string> = [] |
| 46 | + |
| 47 | + if (tileColor) { |
| 48 | + if (!isValidHexColorString(tileColor)) { |
| 49 | + throw new Error(`Provided tile color (${tileColor}) is not valid hex color string.`) |
38 | 50 | }
|
| 51 | + |
| 52 | + linkTags.push(`<meta name="msapplication-TileColor" content="${tileColor}">`) |
39 | 53 | }
|
| 54 | + |
| 55 | + await forEachIconTypeEdgeIncludes(normalizedOutputTypes, (type, edge) => { |
| 56 | + const size = [edge, edge] |
| 57 | + const fileName = `${type}-${size.join('x')}.png` |
| 58 | + const filePath = path.join('icons', fileName) |
| 59 | + |
| 60 | + if (type === 'msapplication-TileImage') { |
| 61 | + linkTags.push(`<meta name="msapplication-TileImage" content="${filePath}">`) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + linkTags.push(`<link rel="${type}" type="image/png" href="${filePath}" sizes="${size.join('x')}">`) |
| 66 | + }) |
| 67 | + |
| 68 | + return linkTags |
40 | 69 | }
|
0 commit comments