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

Add option for customizing fontawesome package location #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fontawesomeSubset(["check", "square", "caret-up"], "sass/webfonts");
- `output_dir` - Directory that you want the webfonts to be generated in. Relative to current NPM process. Ex: `sass/webfonts`
- `options` - Object of options to further customize the tool.
- `package` - `free` or `pro` . Defaults to `free` version. See [below](#using-with-fontawesome-pro) for Pro instructions.
- `packagePath` - `string` . Defaults to the default package names from the fontawesome registry. Can be used with an NPM alias to customize the path of the FontAwesome package to use. Ex: `fa-pro-6` combined with `npm install fa-pro-6@npm:@fortawesome/fontawesome-pro@^6.0.0`
- `targetFormats` - A string array of one or more formats to export. Available options: `woff` `woff2` `sfnt` (`ttf`). Defaults to `woff2` & `sfnt`.

### Using with FontAwesome Pro
Expand Down
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();
});
});