Skip to content

Commit

Permalink
feat: support for more config files. #48
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 8, 2023
1 parent c271be5 commit e6cdc20
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ npm i svgtofont

You can add configuration to package.json. [#48](https://github.com/jaywcjlove/svgtofont/issues/48)

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

```js
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@types/ttf2eot": "~2.0.0",
"@types/ttf2woff": "~2.0.2",
"@types/ttf2woff2": "~2.0.0",
"auto-config-loader": "^1.7.1",
"cheerio": "~1.0.0-rc.12",
"colors-cli": "~1.0.28",
"copy-template-dir": "~1.4.0",
Expand All @@ -88,7 +89,6 @@
"ttf2eot": "~3.1.0",
"ttf2woff": "~3.0.0",
"ttf2woff2": "~5.0.0",
"yaml": "^2.1.3",
"yargs": "~17.7.1"
},
"devDependencies": {
Expand Down
46 changes: 18 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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 { Config } from 'svgo';
import * as YAML from 'yaml';
import { log } from './log';
import { generateIconsSource, generateReactIcons } from './generate';
import { createSVG, createTTF, createEOT, createWOFF, createWOFF2, createSvgSymbol, copyTemplate, CSSOptions, createHTML, createTypescript, TypescriptOptions } from './utils';
Expand Down Expand Up @@ -197,46 +197,36 @@ export type IconInfo = {
export type InfoData = Record<string, Partial<IconInfo>>

export default async (options: SvgToFontOptions = {}) => {
const confPath = path.join(process.cwd(), '.svgtofontrc');
if (fs.pathExistsSync(confPath)) {
const conf = await fs.readJson(confPath);
options = { ...options, ...conf };
} else {
// load yaml config
const confPath = path.join(process.cwd(), '.svgtofontrc.yaml');
if (fs.pathExistsSync(confPath)) {
const conf = await YAML.parse(fs.readFileSync(confPath, 'utf-8'));
options = { ...options, ...conf };
}
const defaultOptions: SvgToFontOptions = {
dist: path.resolve(process.cwd(), 'fonts'),
src: path.resolve(process.cwd(), 'svg'),
startUnicode: 0xea01,
svg2ttf: {},
svgicons2svgfont: {
fontName: 'iconfont',
},
fontName: 'iconfont',
symbolNameDelimiter: '-',
}

const data = autoConf<SvgToFontOptions>('svgtofont', {
mustExist: true,
default: defaultOptions,
});
options = merge(defaultOptions, options, data);
const pkgPath = path.join(process.cwd(), 'package.json');
if (fs.pathExistsSync(pkgPath)) {
const pkg = require(pkgPath);
if (pkg.svgtofont) {
const cssOptions = options.css
options = { ...options, ...pkg.svgtofont }
if (pkg.svgtofont.css && cssOptions && typeof cssOptions === 'object') {
options.css = { ...cssOptions, ...pkg.svgtofont.css }
}
}
if (options.website && pkg.version) {
options.website.version = pkg.version;
}
}
if (options.log === undefined) options.log = true;
log.disabled = !options.log;
if (options.logger && typeof options.logger === 'function') log.logger = options.logger;
options.dist = options.dist || path.resolve(process.cwd(), 'fonts');
options.src = options.src || path.resolve(process.cwd(), 'svg');
options.startUnicode = options.startUnicode || 0xea01;
options.svg2ttf = options.svg2ttf || {};
options.emptyDist = options.emptyDist;
options.fontName = options.fontName || 'iconfont';
options.svgicons2svgfont = options.svgicons2svgfont || {};

options.svgicons2svgfont.fontName = options.fontName;
options.symbolNameDelimiter = options.symbolNameDelimiter || '-';
options.classNamePrefix = options.classNamePrefix || options.fontName;

const fontSize = options.css && typeof options.css !== 'boolean' && options.css.fontSize ? options.css.fontSize : '16px';
// If you generate a font you need to generate a style.
if (options.website && !options.css) options.css = true;
Expand Down

0 comments on commit e6cdc20

Please sign in to comment.