Skip to content

Commit

Permalink
feat: add generateInfoData option. #188
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 24, 2022
1 parent 4e31dbe commit d552b80
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,25 @@ async function generate () {
generate();
```

### generateInfoData

> Type: `Boolean`
> Default value: `false`
Output `./dist/info.json`, The content is as follows:

```js
{
"adobe": {
"encodedCode": "\\ea01",
"prefix": "svgtofont",
"className": "svgtofont-adobe",
"unicode": ""
},
...
}
```

### src

> Type: `String`
Expand Down
39 changes: 36 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ export type SvgToFontOptions = {
* ```
*/
outSVGPath?: boolean;
/**
* Output `./dist/info.json`, The content is as follows:
* @example
* ```js
* {
* "adobe": {
* "encodedCode": "\\ea01",
* "prefix": "svgtofont",
* "className": "svgtofont-adobe",
* "unicode": ""
* },
* .....
* }
* ```
*/
generateInfoData?: boolean;
/**
* This is the setting for [svgicons2svgfont](https://github.com/nfroidure/svgicons2svgfont/tree/dd713bea4f97afa59f7dba6a21ff7f22db565bcf#api)
*/
Expand Down Expand Up @@ -163,6 +179,15 @@ export type SvgToFontOptions = {
typescript?: boolean | TypescriptOptions
}

export type IconInfo = {
prefix: string;
symbol: string;
unicode: string;
className: string;
encodedCode: string | number;
}
export type InfoData = Record<string, Partial<IconInfo>>

export default async (options: SvgToFontOptions = {}) => {
const confPath = path.join(process.cwd(), '.svgtofontrc');
if (fs.pathExistsSync(confPath)) {
Expand Down Expand Up @@ -197,6 +222,7 @@ export default async (options: SvgToFontOptions = {}) => {
// If you generate a font you need to generate a style.
if (options.website && !options.css) options.css = true;

const infoDataPath = path.resolve(options.dist, 'info.json');
try {
if (options.emptyDist) {
await fs.emptyDir(options.dist);
Expand All @@ -211,8 +237,9 @@ export default async (options: SvgToFontOptions = {}) => {
let unicodeHtml: string[] = [];
let symbolHtml: string[] = [];
const prefix = options.classNamePrefix || options.fontName;

const infoData: InfoData = {}
Object.keys(unicodeObject).forEach(name => {
if (!infoData[name]) infoData[name] = {};
const _code = unicodeObject[name];
let symbolName = options.classNamePrefix + options.symbolNameDelimiter + name
let iconPart = symbolName + '">';
Expand All @@ -226,7 +253,10 @@ export default async (options: SvgToFontOptions = {}) => {
cssString.push(`.${symbolName}:before { content: "\\${encodedCodes.toString(16)}"; }\n`);
cssToVars.push(`$${symbolName}: "\\${encodedCodes.toString(16)}";\n`);
}

infoData[name].encodedCode = `\\${encodedCodes.toString(16)}`;
infoData[name].prefix = prefix;
infoData[name].className = symbolName;
infoData[name].unicode = `&#${encodedCodes};`;
cssIconHtml.push(`<li class="class-icon"><i class="${iconPart}</i><p class="name">${name}</p></li>`);
unicodeHtml.push(`<li class="unicode-icon"><span class="iconfont">${_code}</span><h4>${name}</h4><span class="unicode">&amp;#${encodedCodes};</span></li>`);
symbolHtml.push(`
Expand All @@ -238,7 +268,10 @@ export default async (options: SvgToFontOptions = {}) => {
</li>
`);
});

if (options.generateInfoData) {
await fs.writeJSON(infoDataPath, infoData, { spaces: 2 });
log.log(`${color.green('SUCCESS')} Created ${infoDataPath} `);
}
const ttf = await createTTF(options);
await createEOT(options, ttf);
await createWOFF(options, ttf);
Expand Down
12 changes: 6 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function createSVG(options: SvgToFontOptions = {}): Promise<Record<string
// Setting the font destination
fontStream.pipe(fs.createWriteStream(DIST_PATH))
.on("finish", () => {
log.log(`${color.green('SUCCESS')} ${color.blue('SVG')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue_bt('SVG')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(UnicodeObj);
})
.on("error", (err) => {
Expand Down Expand Up @@ -159,7 +159,7 @@ export function createTTF(options: SvgToFontOptions = {}): Promise<Buffer> {
if (err) {
return reject(err);
}
log.log(`${color.green('SUCCESS')} ${color.blue('TTF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue_bt('TTF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(ttfBuf);
});
});
Expand All @@ -177,7 +177,7 @@ export function createEOT(options: SvgToFontOptions = {}, ttf: Buffer) {
if (err) {
return reject(err);
}
log.log(`${color.green('SUCCESS')} ${color.blue('EOT')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue_bt('EOT')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(eot);
});
});
Expand All @@ -194,7 +194,7 @@ export function createWOFF(options: SvgToFontOptions = {}, ttf: Buffer) {
if (err) {
return reject(err);
}
log.log(`${color.green('SUCCESS')} ${color.blue('WOFF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue_bt('WOFF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(woff);
});
});
Expand All @@ -211,7 +211,7 @@ export function createWOFF2(options: SvgToFontOptions = {}, ttf: Buffer) {
if (err) {
return reject(err);
}
log.log(`${color.green('SUCCESS')} ${color.blue('WOFF2')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue_bt('WOFF2')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve({
path: DIST_PATH
});
Expand Down Expand Up @@ -241,7 +241,7 @@ export function createSvgSymbol(options: SvgToFontOptions = {}) {
if (err) {
return reject(err);
}
log.log(`${color.green('SUCCESS')} ${color.blue('Svg Symbol')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue_bt('Svg Symbol')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve({
path: DIST_PATH,
svg: $.html("svg")
Expand Down

0 comments on commit d552b80

Please sign in to comment.