Skip to content

Commit

Permalink
feat: add config options. fix #208
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 10, 2023
1 parent 54bc335 commit 0b99cbd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ async function creatFont() {

> svgtofont(options)
### config

> Type: `config?: AutoConfOption<SvgToFontOptions>`
By default, settings are automatically loaded from `.svgtofontrc` and `package.json`. You can add configuration to `package.json`. [#48](https://github.com/jaywcjlove/svgtofont/issues/48)

Support for `.svgtofontrc` and [more](https://github.com/jaywcjlove/auto-config-loader/blob/add7ae012f5c3903296fbf0ef06e3631e379c2cc/core/README.md?plain=1#L106-L135) configuration files.

### log

> Type: `Boolean`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@types/ttf2eot": "~2.0.0",
"@types/ttf2woff": "~2.0.2",
"@types/ttf2woff2": "~2.0.0",
"auto-config-loader": "^1.7.1",
"auto-config-loader": "^1.7.4",
"cheerio": "~1.0.0-rc.12",
"colors-cli": "~1.0.28",
"copy-template-dir": "~1.4.0",
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import fs from 'fs-extra';
import image2uri from 'image2uri';
import { SvgIcons2FontOptions } from 'svgicons2svgfont';
import color from 'colors-cli';
import { autoConf, merge } from 'auto-config-loader';
import { autoConf, merge, AutoConfOption } from 'auto-config-loader';
import { Config } from 'svgo';
import { log } from './log';
import { generateIconsSource, generateReactIcons } from './generate';
import { createSVG, createTTF, createEOT, createWOFF, createWOFF2, createSvgSymbol, copyTemplate, CSSOptions, createHTML, createTypescript, TypescriptOptions } from './utils';

export type SvgToFontOptions = {
/** Support for .svgtofontrc and more configuration files. */
config?: AutoConfOption<SvgToFontOptions>
/** A value of `false` disables logging */
log?: boolean;
/** log callback function */
Expand Down Expand Up @@ -197,7 +199,7 @@ export type IconInfo = {
export type InfoData = Record<string, Partial<IconInfo>>

export default async (options: SvgToFontOptions = {}) => {
const defaultOptions: SvgToFontOptions = {
const defaultOptions: SvgToFontOptions = merge({
dist: path.resolve(process.cwd(), 'fonts'),
src: path.resolve(process.cwd(), 'svg'),
startUnicode: 0xea01,
Expand All @@ -207,12 +209,13 @@ export default async (options: SvgToFontOptions = {}) => {
},
fontName: 'iconfont',
symbolNameDelimiter: '-',
}
}, options);
const data = autoConf<SvgToFontOptions>('svgtofont', {
mustExist: true,
default: defaultOptions,
...options.config,
});
options = merge(defaultOptions, options, data);
options = merge(defaultOptions, data);
const pkgPath = path.join(process.cwd(), 'package.json');
if (fs.pathExistsSync(pkgPath)) {
const pkg = require(pkgPath);
Expand Down
41 changes: 40 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,43 @@ it('example simple test case for useNameAsUnicode.', async () => {
// should not contain any variables
expect(css.toString().indexOf('$') === -1).toBeTruthy();
await fs.emptyDir(dist);
});
});

it('example .svgtofontrc.js test case for useNameAsUnicode', async () => {
const dist = path.resolve(process.cwd(), 'test', 'templates', 'dist2');
await fs.emptyDir(dist);
await svgtofont({
config: {
cwd: path.resolve(process.cwd(), 'test', 'templates')
},
src: path.resolve(process.cwd(), 'test', 'example', 'svg'),
dist: dist,
fontName: 'nameAsUnicode',
css: false,
classNamePrefix: 'my-icons',
useNameAsUnicode: true,
emptyDist: true,
typescript: true,
});
const fileNames = await fs.readdir(dist);
expect(fileNames).toEqual([
'nameAsUnicode.css',
'nameAsUnicode.d.ts',
'nameAsUnicode.eot',
'nameAsUnicode.less',
'nameAsUnicode.module.less',
'nameAsUnicode.scss',
'nameAsUnicode.styl',
'nameAsUnicode.svg',
'nameAsUnicode.symbol.svg',
'nameAsUnicode.ttf',
'nameAsUnicode.woff',
'nameAsUnicode.woff2',
]);
const css = await fs.readFile(path.resolve(dist, 'nameAsUnicode.css'));
// should contain a class with the prefix or the font name, in this case we provided a prefix so we should get that
expect(css.toString().indexOf('.my-icons') > -1).toBeTruthy();
// should not contain any variables
expect(css.toString().indexOf('$') === -1).toBeTruthy();
await fs.emptyDir(dist);
});
1 change: 1 addition & 0 deletions test/templates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist2
5 changes: 5 additions & 0 deletions test/templates/.svgtofontrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from "path";

export default {
"dist": path.resolve(process.cwd(), "test/templates/dist2")
}
3 changes: 3 additions & 0 deletions test/templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const rootPath = path.resolve(process.cwd(), "test", "templates");
* @type {import('../../lib').SvgToFontOptions}
*/
const options = {
config: {
cwd: rootPath,
},
src: path.resolve(rootPath, "svg"), // svg path
dist: path.resolve(rootPath, "dist"), // output path
// emptyDist: true, // Clear output directory contents
Expand Down
3 changes: 3 additions & 0 deletions test/templates/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const path = require("path");
const rootPath = path.resolve(process.cwd(), "test", "example");

svgtofont({
config: {
cwd: rootPath,
},
src: path.resolve(rootPath, "svg"), // svg path
dist: path.resolve(rootPath, "dist"), // output path
fontName: "svgtofont", // font name
Expand Down

0 comments on commit 0b99cbd

Please sign in to comment.