From 00e239f634141b921811ac0ffa44a9fc560db438 Mon Sep 17 00:00:00 2001 From: Logan Graham Date: Wed, 20 Jul 2022 19:56:44 -0400 Subject: [PATCH] Add woff & targetFormats options --- src/index.ts | 28 ++++++++++++++++++---------- src/types.ts | 11 ++++++++++- tests/index.test.ts | 14 +++++++++++--- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 89053d5..f2a795c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,15 +5,16 @@ import { existsSync, readFileSync, writeFileSync } from "fs"; import { resolve } from "path"; import { sync as makeDirSync } from "mkdirp"; -import { FontAwesomeOptions, IconYAML, Subset, SubsetOption } from "./types"; +import { FontAwesomeOptions, IconYAML, Subset, SubsetOption, TargetFormat } from "./types"; import subsetFont from "subset-font"; import yaml from "yaml"; import { addIconError, findIconByName } from "./utils"; -const OUTPUT_FORMATS: { targetFormat: "woff2" | "sfnt"; fileExt: string }[] = [ - { targetFormat: "woff2", fileExt: "woff2" }, - { targetFormat: "sfnt", fileExt: "ttf" }, -]; +const OUTPUT_FORMAT_MAP: Record = { + sfnt: "ttf", + woff: "woff", + woff2: "woff2", +}; /** * This function will take an object of glyph names and output a subset of the standard fonts optimized in size for @@ -26,9 +27,9 @@ const OUTPUT_FORMATS: { targetFormat: "woff2" | "sfnt"; fileExt: string }[] = [ function fontawesomeSubset( subset: SubsetOption, outputDir: string, - options: FontAwesomeOptions = { package: "free" } + options: FontAwesomeOptions = {} ) { - const { package: packageType } = options; + const { package: packageType = "free", targetFormats = ["woff2", "sfnt"] } = options; // Maps style to actual font name / file name. const fontMap: Record = { solid: "fa-solid-900", @@ -51,6 +52,13 @@ function fontawesomeSubset( return Promise.resolve(false); } + // Check that we have atleast one target format for output + if (!Array.isArray(targetFormats) || targetFormats.length === 0) { + console.error("One or more target formats are required. Exiting."); + + return Promise.resolve(false); + } + const fontMeta = resolve(packageLocation, "../../metadata/icons.yml"); const fontFiles = resolve(packageLocation, "../../webfonts"); @@ -111,12 +119,12 @@ function fontawesomeSubset( const outputFile = resolve(outputDir, fontFileName); // Loop over our requested output formats, and generate our subsets - for (const oFormat of OUTPUT_FORMATS) { + for (const targetFormat of targetFormats) { promises.push( subsetFont(fontData, unicodeCharacters.join(" "), { - targetFormat: oFormat.targetFormat, + targetFormat: targetFormat, }).then((data) => { - writeFileSync(`${outputFile}.${oFormat.fileExt}`, data); + writeFileSync(`${outputFile}.${OUTPUT_FORMAT_MAP[targetFormat]}`, data); }) ); } diff --git a/src/types.ts b/src/types.ts index abd89aa..caf9c37 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,12 @@ export interface FontAwesomeOptions { - package: "free" | "pro"; + /** + * The FontAwesome package type we should use. Defaults to 'free'. + */ + package?: "free" | "pro"; + /** + * Requested font output targets. + */ + targetFormats?: TargetFormat[]; } export type Subset = "solid" | "light" | "regular" | "thin" | "brands" | "duotone"; @@ -8,6 +15,8 @@ export type GlyphName = string; export type SubsetOption = GlyphName[] | Partial>; +export type TargetFormat = "woff" | "woff2" | "sfnt"; + /** * Type of individual result / item inside the YAML file. */ diff --git a/tests/index.test.ts b/tests/index.test.ts index 420c3de..e3ae33a 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -77,7 +77,7 @@ describe("fontawesomeSubset", () => { } }); - it("should create ttf & woff2 files for each requested subset", async () => { + it("should create ttf, woff, and woff2 files for each requested subset", async () => { const tempDir = await createTempDir(); await fontawesomeSubset( { @@ -89,7 +89,7 @@ describe("fontawesomeSubset", () => { duotone: ["abacus"], }, tempDir, - { package: "pro" } + { package: "pro", targetFormats: ["woff2", "woff", "sfnt"] } ); const fontNames = [ @@ -100,7 +100,7 @@ describe("fontawesomeSubset", () => { "fa-thin-100", "fa-light-300", ] - .map((name) => [`${name}.ttf`, `${name}.woff2`]) + .map((name) => [`${name}.ttf`, `${name}.woff`, `${name}.woff2`]) .flat(); const fontPromises = fontNames.map((name) => fs.promises.access(`${tempDir}${SEP}${name}`)); const wasSuccessful = await Promise.all(fontPromises) @@ -144,4 +144,12 @@ describe("fontawesomeSubset", () => { ); expect(response).toBeFalsy(); }); + + it("should error when no target formats are provided", async () => { + const errorSpy = jest.spyOn(console, "error").mockImplementationOnce(() => false); + const response = await fontawesomeSubset(["arrow-left"], "", { targetFormats: [] }); + + expect(errorSpy).toBeCalledWith("One or more target formats are required. Exiting."); + expect(response).toBeFalsy(); + }); });