Skip to content

Commit

Permalink
add packagePath option, closes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
omacranger committed Apr 1, 2023
1 parent 3c01ccc commit 9f16e70
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ function fontawesomeSubset(
outputDir: string,
options: FontAwesomeOptions = {}
) {
const { package: packageType = "free", targetFormats = ["woff2", "sfnt"] } = options;
const {
package: packageType = "free",
targetFormats = ["woff2", "sfnt"],
packagePath,
} = options;
// Maps style to actual font name / file name.
const fontTypes = Object.keys(STYLE_FONT_MAP);
let packageLocation: string;

// Check to see if the user has either free, or pro installed.
try {
packageLocation = require.resolve(`@fortawesome/fontawesome-${packageType}`);
packageLocation = require.resolve(packagePath ?? `@fortawesome/fontawesome-${packageType}`);
} catch (e) {
console.error(
`Unable to resolve the module '@fortawesome/fontawesome-${packageType}'. Double-check that you have your preferred fontawesome package installed as a dependency and the package type passed into the options if using Pro features.\n\n\`fontawesomeSubset(..., ..., { package: 'pro' })\``
`Unable to resolve the module '${
packagePath ?? `@fortawesome/fontawesome-${packageType}`
}'. Double-check that you have your preferred fontawesome package installed as a dependency and the package type passed into the options if using Pro features.\n\n\`fontawesomeSubset(..., ..., { package: 'pro' })\``
);
return Promise.resolve(false);
}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export interface FontAwesomeOptions {
* The FontAwesome package type we should use. Defaults to 'free'.
*/
package?: PackageType;
/**
* Override the path we use to search for a valid FontAwesome package. Useful for npm aliases.
*/
packagePath?: string;
/**
* Requested font output targets.
*/
Expand Down
28 changes: 26 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ describe("fontawesomeSubset", () => {
subsetMock.mockImplementation(jest.fn(() => Promise.resolve(Buffer.from(""))));
});

afterEach(() => {
jest.resetAllMocks();
});

const testShouldAllAllRequiredGlyphs = async (
subsets: SubsetOption,
expected: { family: string; icon: string; duotone?: boolean }[]
Expand Down Expand Up @@ -226,8 +230,6 @@ describe("fontawesomeSubset", () => {
expect(response).toBeFalsy();
expect(warnSpy).toBeCalledTimes(2);
expect(tableSpy).toBeCalledTimes(1);

jest.resetAllMocks();
});

it("should warn when a font file is not found for a requested subset", async () => {
Expand All @@ -248,4 +250,26 @@ describe("fontawesomeSubset", () => {
expect(errorSpy).toBeCalledWith("One or more target formats are required. Exiting.");
expect(response).toBeFalsy();
});

it("should allow customizing the package resolve path with options.packagePath", async () => {
const errorSpy = jest.spyOn(console, "error").mockImplementationOnce(() => false);
const response = await fontawesomeSubset(["arrow-left"], "", {
packagePath: `@fortawesome/fontawesome-${PACKAGE}`,
});

expect(errorSpy).not.toBeCalled();
expect(response).toBeTruthy();
});

it("should error when an invalid packagePath is provided", async () => {
const errorSpy = jest.spyOn(console, "error").mockImplementationOnce(() => false);
const response = await fontawesomeSubset(["arrow-left"], "", {
packagePath: "some-other-name",
});

expect(errorSpy).toBeCalledWith(
expect.stringContaining("Unable to resolve the module 'some-other-name'.")
);
expect(response).toBeFalsy();
});
});

0 comments on commit 9f16e70

Please sign in to comment.