Skip to content

Commit

Permalink
Merge branch 'master' into merge-xhmikosr-prs
Browse files Browse the repository at this point in the history
  • Loading branch information
tancredi authored Dec 24, 2020
2 parents 2e4d359 + 606c8c8 commit d74e836
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/cli/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import color from 'cli-color';
import { RunnerResults } from '../core/runner';
import { pluralize } from '../utils/string';

export const getLogger = (debug = false, silent = false) => ({
error(error: Error | string) {
Expand Down Expand Up @@ -29,7 +30,11 @@ export const getLogger = (debug = false, silent = false) => ({
results({ assetsIn, writeResults, options: { inputDir } }: RunnerResults) {
const iconsCount = Object.values(assetsIn).length;

this.log(color.white(`✔ ${iconsCount} svg found in ${inputDir}`));
this.log(
color.white(
`✔ ${iconsCount} ${pluralize('SVG', iconsCount)} found in ${inputDir}`
)
);

for (const { writePath } of writeResults) {
this.log(color.blue(`✔ Generated`, color.blueBright(writePath)));
Expand Down
9 changes: 4 additions & 5 deletions src/generators/asset-types/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ const generator: FontGenerator<Buffer> = {
dependsOn: FontAssetType.SVG,

generate: (options, svg: Buffer) =>
renderTemplate(
options.templates.css,
{ ...options, fontSrc: renderSrcAttribute(options, svg) },
{ helpers: { codepoint: str => str.toString(16) } }
)
renderTemplate(options.templates.css, {
...options,
fontSrc: renderSrcAttribute(options, svg)
})
};

export default generator;
9 changes: 4 additions & 5 deletions src/generators/asset-types/sass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ const generator: FontGenerator<Buffer> = {
dependsOn: FontAssetType.SVG,

generate: (options, svg: Buffer) =>
renderTemplate(
options.templates.sass,
{ ...options, fontSrc: renderSrcAttribute(options, svg) },
{ helpers: { codepoint: str => str.toString(16) } }
)
renderTemplate(options.templates.sass, {
...options,
fontSrc: renderSrcAttribute(options, svg)
})
};

export default generator;
10 changes: 9 additions & 1 deletion src/utils/__tests__/codepoints.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DEFAULT_START_CODEPOINT } from '../../constants';
import { AssetsMap } from '../assets';
import { getCodepoints } from '../codepoints';
import { getCodepoints, getHexCodepoint } from '../codepoints';

const mockAssetsMap = (ids: string[] = ['foo', 'bar', 'test']): AssetsMap =>
ids.reduce(
Expand Down Expand Up @@ -56,4 +56,12 @@ describe('CodePoints utilities', () => {
bazz: 12
});
});

test('`getHexCodepoint` converts codepoint to hex string', () => {
expect(getHexCodepoint(62087)).toEqual('f287');
expect(getHexCodepoint(61940)).toEqual('f1f4');
expect(getHexCodepoint(61941)).toEqual('f1f5');
expect(getHexCodepoint(61942)).toEqual('f1f6');
expect(getHexCodepoint(61943)).toEqual('f1f7');
});
});
11 changes: 11 additions & 0 deletions src/utils/__tests__/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pluralize } from '../string';

describe('String utilities', () => {
test('`pluralize` correctly adds an `s` to a word if given number is other than `1`', () => {
expect(pluralize('foo', 0)).toBe('foos');
expect(pluralize('foo', 1)).toBe('foo');
expect(pluralize('foo', 2)).toBe('foos');
expect(pluralize('foo', 10)).toBe('foos');
expect(pluralize('foo', 11)).toBe('foos');
});
});
23 changes: 19 additions & 4 deletions src/utils/__tests__/template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Handlebars from 'handlebars';
import { renderTemplate } from '../template';
import { renderTemplate, TEMPLATE_HELPERS } from '../template';
import { readFile } from '../fs-async';

const readFileMock = (readFile as any) as jest.Mock;
Expand All @@ -21,12 +21,11 @@ describe('Template utilities', () => {
const templateOut = '::rendered::';
const templateFn = jest.fn((_: any, __: any) => templateOut);
const context = { foo: 'bar' };
const options = { helpers: { foo: () => 'bar' } };

readFileMock.mockImplementation(async () => template);
hbsCompileMock.mockImplementation(() => templateFn);

expect(await renderTemplate(filename, context, options)).toBe(templateOut);
expect(await renderTemplate(filename, context)).toBe(templateOut);

expect(readFileMock).toHaveBeenCalledTimes(1);
expect(readFileMock).toHaveBeenCalledWith(
Expand All @@ -38,6 +37,22 @@ describe('Template utilities', () => {
expect(hbsCompileMock).toHaveBeenCalledWith(template);

expect(templateFn).toHaveBeenCalledTimes(1);
expect(templateFn).toHaveBeenCalledWith(context, options);
expect(templateFn).toHaveBeenCalledWith(context, expect.any(Object));
});

test('`renderTemplate` combines given options Object with default helpers', async () => {
const templateFn = jest.fn();
const userHelpers = { bar: jest.fn() };
const options = { some: 'option', helpers: userHelpers };

hbsCompileMock.mockImplementation(() => templateFn);

await renderTemplate('foo-bar.hbs', {}, options);

expect(templateFn).toHaveBeenCalledTimes(1);
expect(templateFn).toHaveBeenCalledWith(expect.any(Object), {
some: 'option',
helpers: { ...TEMPLATE_HELPERS, ...userHelpers }
});
});
});
3 changes: 3 additions & 0 deletions src/utils/codepoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ export const getCodepoints = (

return { ...predefined, ...out };
};

export const getHexCodepoint = (decimalCodepoint: number): string =>
decimalCodepoint.toString(16);
2 changes: 2 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const pluralize = (word: string, amount: number) =>
amount === 1 ? word : `${word}s`;
12 changes: 10 additions & 2 deletions src/utils/template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Handlebars from 'handlebars';
import { resolve, isAbsolute } from 'path';
import { readFile } from './fs-async';
import { getHexCodepoint } from './codepoints';

export const TEMPLATE_HELPERS: Record<string, Function> = {
codepoint: getHexCodepoint
};

export type CompileOptions = {
helpers?: { [key: string]: (...args: any[]) => string };
Expand All @@ -9,12 +14,15 @@ export type CompileOptions = {
export const renderTemplate = async (
templatePath: string,
context: object,
options?: CompileOptions
options: CompileOptions = {}
) => {
const absoluteTemplatePath = isAbsolute(templatePath)
? templatePath
: resolve(process.cwd(), templatePath);
const template = await readFile(absoluteTemplatePath, 'utf8');

return Handlebars.compile(template)(context, options);
return Handlebars.compile(template)(context, {
...options,
helpers: { ...TEMPLATE_HELPERS, ...(options.helpers || {}) }
});
};

0 comments on commit d74e836

Please sign in to comment.