Skip to content

Commit

Permalink
fix: Merge pull request #27 from tancredi/fix/address-left-config-typ…
Browse files Browse the repository at this point in the history
…ing-issues

Fix/address left config typing issues
  • Loading branch information
tancredi authored Jan 12, 2021
2 parents b83de62 + 1c135c9 commit a4fbc04
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 136 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ module.exports = {
fontsUrl: '/static/fonts',
formatOptions: {
// Pass options directly to `svgicons2svgfont`
svg: {
metadata: {
foo: 'bar'
},
ascent: 0.5
woff: {
// Woff Extended Metadata Block - see https://www.w3.org/TR/WOFF/#Metadata
metadata: '...'
},
json: {
// render the JSON human readable with two spaces indentation (default is none, so minified)
Expand Down
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
"lint": "prettier --check ."
},
"files": [
"bin",
"lib",
"templates"
"bin/fantasticon",
"lib/**/*.{js,ts,map}",
"!lib/**/{__mocks__,__tests__}/",
"templates/*.hbs"
],
"prepublish": "build",
"dependencies": {
Expand All @@ -58,6 +59,11 @@
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.13",
"jest": "^26.6.3",
"@types/svg2ttf": "^5.0.0",
"@types/svgicons2svgfont": "^9.1.0",
"@types/ttf2eot": "^2.0.0",
"@types/ttf2woff": "^2.0.0",
"@types/ttf2woff2": "^2.0.0",
"mock-fs": "^4.13.0",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
Expand All @@ -72,11 +78,5 @@
},
"engines": {
"node": ">= 10.0.0"
},
"files": [
"bin/fantasticon",
"lib/**/*.{js,ts,map}",
"!lib/**/{__mocks__,__tests__}/",
"templates/*.hbs"
]
}
}
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { resolve } from 'path';
import { RunnerOptionalOptions } from './types/runner';
import { RunnerOptions } from './types/runner';
import { FontAssetType, OtherAssetType } from './types/misc';

export const TEMPLATES_DIR = resolve(__dirname, '../templates');

export const DEFAULT_OPTIONS: RunnerOptionalOptions = {
export const DEFAULT_OPTIONS: Omit<RunnerOptions, 'inputDir' | 'outputDir'> = {
name: 'icons',
fontTypes: [FontAssetType.EOT, FontAssetType.WOFF2, FontAssetType.WOFF],
assetTypes: [
Expand Down
4 changes: 2 additions & 2 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_OPTIONS } from '../constants';
import { RunnerOptions, RunnerOptionsInput } from '../types/runner';
import { RunnerOptions } from '../types/runner';
import {
loadAssets,
writeAssets,
Expand Down Expand Up @@ -27,7 +27,7 @@ export const sanitiseOptions = (userOptions: any) =>
});

export const generateFonts = async (
userOptions: RunnerOptionsInput,
userOptions: RunnerOptions,
mustWrite = false
): Promise<RunnerResults> => {
const options = await sanitiseOptions(userOptions);
Expand Down
11 changes: 3 additions & 8 deletions src/generators/asset-types/__tests__/eot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ jest.mock('ttf2eot', () =>
);

const mockOptions = (eotOptions = { __mock: 'options__' } as any) =>
(({
formatOptions: { [FontAssetType.EOT]: eotOptions }
} as unknown) as FontGeneratorOptions);
(({} as unknown) as FontGeneratorOptions);

const ttf = ('::ttf::' as unknown) as Buffer;

Expand All @@ -24,15 +22,12 @@ describe('`EOT` font generator', () => {
const ttfArr = new Uint8Array(('::ttf::' as unknown) as any[]);

expect(ttf2eot).toHaveBeenCalledTimes(1);
expect(ttf2eot).toHaveBeenCalledWith(ttfArr, { __mock: 'options__' });
expect(ttf2eot).toHaveBeenCalledWith(ttfArr);
expect(result).toEqual(Buffer.from(`::eot(${ttfArr})::`));
});

test('passes correctly format options to `ttf2eot`', async () => {
const formatOptions = { foo: 'bar' };
await eotGen.generate(mockOptions(formatOptions), ttf);

await eotGen.generate(mockOptions({}), ttf);
expect(ttf2eot).toHaveBeenCalledTimes(1);
expect(ttf2eot.mock.calls[0][1]).toEqual(formatOptions);
});
});
13 changes: 3 additions & 10 deletions src/generators/asset-types/__tests__/woff2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ttf2woff2 from 'ttf2woff2';
import { FontAssetType } from '../../../types/misc';
import { FontGeneratorOptions } from '../../../types/generator';
import woff2Gen from '../woff2';

Expand All @@ -10,9 +9,7 @@ jest.mock('ttf2woff2', () =>
);

const mockOptions = (woffOptions = { __mock: 'options__' } as any) =>
(({
formatOptions: { [FontAssetType.WOFF2]: woffOptions }
} as unknown) as FontGeneratorOptions);
(({} as unknown) as FontGeneratorOptions);

const ttf = ('::ttf::' as unknown) as Buffer;

Expand All @@ -23,18 +20,14 @@ describe('`WOFF2` font generator', () => {

test('resolves with the correctly processed return value of `ttf2woff2`', async () => {
const result = await woff2Gen.generate(mockOptions(), ttf);
const ttfArr = new Uint8Array(('::ttf::' as unknown) as any[]);

expect(ttf2woff2).toHaveBeenCalledTimes(1);
expect(ttf2woff2).toHaveBeenCalledWith(ttfArr, { __mock: 'options__' });
expect(result).toEqual(Buffer.from(`::woff2(${ttfArr})::`));
expect(ttf2woff2).toHaveBeenCalledWith(ttf);
expect(result).toEqual(Buffer.from(`::woff2(${ttf})::`));
});

test('passes correctly format options to `ttf2woff2`', async () => {
const formatOptions = { foo: 'bar' };
await woff2Gen.generate(mockOptions(formatOptions), ttf);

expect(ttf2woff2).toHaveBeenCalledTimes(1);
expect(ttf2woff2.mock.calls[0][1]).toEqual(formatOptions);
});
});
4 changes: 2 additions & 2 deletions src/generators/asset-types/eot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { FontAssetType } from '../../types/misc';
const generator: FontGenerator<Buffer> = {
dependsOn: FontAssetType.TTF,

async generate({ formatOptions }, ttf) {
const font = ttf2eot(new Uint8Array(ttf), formatOptions[FontAssetType.EOT]);
async generate(_options, ttf) {
const font = ttf2eot(new Uint8Array(ttf));
return Buffer.from(font.buffer);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/generators/asset-types/ttf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const generator: FontGenerator<string> = {
dependsOn: FontAssetType.SVG,

async generate({ formatOptions }, svg) {
const font = svg2ttf(svg, formatOptions[FontAssetType.TTF]);
const font = svg2ttf(svg, formatOptions?.ttf);
return Buffer.from(font.buffer);
}
};
Expand Down
5 changes: 1 addition & 4 deletions src/generators/asset-types/woff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const generator: FontGenerator<Buffer> = {
dependsOn: FontAssetType.TTF,

async generate({ formatOptions }, ttf) {
const font = ttf2woff(
new Uint8Array(ttf),
formatOptions[FontAssetType.WOFF]
);
const font = ttf2woff(new Uint8Array(ttf), formatOptions?.woff);
return Buffer.from(font.buffer);
}
};
Expand Down
7 changes: 2 additions & 5 deletions src/generators/asset-types/woff2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import { FontAssetType } from '../../types/misc';
const generator: FontGenerator<Buffer> = {
dependsOn: FontAssetType.TTF,

async generate({ formatOptions }, ttf) {
const font = ttf2woff2(
new Uint8Array(ttf),
formatOptions[FontAssetType.WOFF2]
);
async generate(_options, ttf) {
const font = ttf2woff2(ttf);
return Buffer.from(font.buffer);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/generators/generator-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'path';
import { DEFAULT_OPTIONS, TEMPLATES_DIR } from '../constants';
import { FormatOptions, RunnerOptions } from '../types/runner';
import { RunnerOptions } from '../types/runner';
import { FormatOptions } from '../types/format';
import { getCodepoints } from '../utils/codepoints';
import { FontGeneratorOptions } from '../types/generator';
import {
Expand Down
28 changes: 28 additions & 0 deletions src/types/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import svg2ttf from 'svg2ttf';
import ttf2woff from 'ttf2woff';
import { SvgIcons2FontOptions } from 'svgicons2svgfont';
import { Arguments } from '../types/utils';

type WoffOptions = Arguments<typeof ttf2woff>[1];
type TtfOptions = svg2ttf.FontOptions;
type SvgOptions = Omit<
SvgIcons2FontOptions,
'fontName' | 'fontHeight' | 'descent' | 'normalize'
>;

interface JsonOptions {
indent?: number;
}

interface TsOptions {
types?: ('enum' | 'constant' | 'literalId' | 'literalKey')[];
singleQuotes?: boolean;
}

export interface FormatOptions {
woff?: WoffOptions;
ttf?: TtfOptions;
svg?: SvgOptions;
json?: JsonOptions;
ts?: TsOptions;
}
3 changes: 2 additions & 1 deletion src/types/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AssetsMap } from '../utils/assets';
import { AssetType, OtherAssetType } from './misc';
import { RunnerOptions, FormatOptions } from './runner';
import { RunnerOptions } from './runner';
import { FormatOptions } from './format';

export type FontGeneratorOptions = RunnerOptions & {
assets: AssetsMap;
Expand Down
4 changes: 0 additions & 4 deletions src/types/modules/svg2ttf.d.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/types/modules/svgicons2svgfont.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/types/modules/ttf2eot.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/types/modules/ttf2woff.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/types/modules/ttf2woff2.d.ts

This file was deleted.

Loading

0 comments on commit a4fbc04

Please sign in to comment.