Skip to content

Commit

Permalink
feat: add disable log option log. (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 24, 2022
1 parent c05707f commit 51fd8c5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ async function creatFont() {

> svgtofont(options)


"log": false,

### log

> Type: `Boolean`
A value of `false` disables logging

### dist

> Type: `String`
Expand Down
7 changes: 4 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FS from 'fs-extra';
import yargs, { Arguments } from 'yargs';
import path from 'path';
import svgtofont from './';
import { log } from './log';

type ArgvResult = Arguments<{
sources: string;
Expand All @@ -28,7 +29,7 @@ const sourcesPath = path.join(process.cwd(), argv.sources);
const outputPath = path.join(process.cwd(), argv.output);

if (!FS.pathExistsSync(sourcesPath)) {
console.error('The directory does not exist!', sourcesPath);
log.error('The directory does not exist!', sourcesPath);
process.exit();
}

Expand All @@ -50,7 +51,7 @@ svgtofont({
},
})
.then(() => {
console.log('done!');
log.log('done!');
}).catch((err) => {
console.log('SvgToFont:ERR:', err);
log.log('SvgToFont:ERR:', err);
});
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import image2uri from 'image2uri';
import { SvgIcons2FontOptions } from 'svgicons2svgfont';
import color from 'colors-cli';
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 = {
/** A value of `false` disables logging */
log?: boolean;
/**
* The output directory.
* @default fonts
Expand Down Expand Up @@ -176,6 +179,7 @@ export default async (options: SvgToFontOptions = {}) => {
}
}

log.disabled = options.log || false;
options.dist = options.dist || path.join(process.cwd(), 'fonts');
options.src = options.src || path.join(process.cwd(), 'svg');
options.startUnicode = options.startUnicode || 0xea01;
Expand Down Expand Up @@ -304,32 +308,32 @@ export default async (options: SvgToFontOptions = {}) => {
}
const classHtmlStr = await createHTML(options.website.template, tempData);
fs.outputFileSync(fontClassPath, classHtmlStr);
console.log(`${color.green('SUCCESS')} Created ${fontClassPath} `);
log.log(`${color.green('SUCCESS')} Created ${fontClassPath} `);

tempData._IconHtml = unicodeHtml.join('');
tempData._type = 'unicode';
const unicodeHtmlStr = await createHTML(options.website.template, tempData);
fs.outputFileSync(unicodePath, unicodeHtmlStr);
console.log(`${color.green('SUCCESS')} Created ${unicodePath} `);
log.log(`${color.green('SUCCESS')} Created ${unicodePath} `);

tempData._IconHtml = symbolHtml.join('');
tempData._type = 'symbol';
const symbolHtmlStr = await createHTML(options.website.template, tempData);
fs.outputFileSync(symbolPath, symbolHtmlStr);
console.log(`${color.green('SUCCESS')} Created ${unicodePath} `);
log.log(`${color.green('SUCCESS')} Created ${unicodePath} `);
}

if (options.outSVGPath) {
const outPath = await generateIconsSource(options);
console.log(`${color.green('SUCCESS')} Created ${outPath} `);
log.log(`${color.green('SUCCESS')} Created ${outPath} `);
}
if (options.outSVGReact) {
const outPath = await generateReactIcons(options);
console.log(`${color.green('SUCCESS')} Created React Components. `);
log.log(`${color.green('SUCCESS')} Created React Components. `);
}

} catch (error) {
console.log('SvgToFont:CLI:ERR:', error);
log.log('SvgToFont:CLI:ERR:', error);
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export class Log {
_disabled?:boolean;
constructor(disabled?: boolean) {
this.disabled = disabled || false
}
get disabled () {
return this._disabled;
}
set disabled(val: boolean) {
this._disabled = val;
}
log = (message?: any, ...optionalParams: any[]) => {
if (this.disabled) return () => {}
return console.log(message, ...optionalParams)
}
error = (message?: any, ...optionalParams: any[]) => {
if (this.disabled) return () => {}
return console.error(message, ...optionalParams)
}
}

export const log = new Log();
17 changes: 9 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import copy from 'copy-template-dir';
import del from 'del';
import moveFile from 'move-file';
import { SvgToFontOptions } from './';
import { log } from './log';

let UnicodeObj: Record<string, string> = {};
/**
Expand Down Expand Up @@ -44,7 +45,7 @@ export function createSVG(options: SvgToFontOptions = {}): Promise<Record<string
// Setting the font destination
fontStream.pipe(fs.createWriteStream(DIST_PATH))
.on("finish", () => {
console.log(`${color.green('SUCCESS')} ${color.blue('SVG')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue('SVG')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(UnicodeObj);
})
.on("error", (err) => {
Expand Down Expand Up @@ -130,7 +131,7 @@ export async function createTypescript(options: Omit<SvgToFontOptions, 'typescri
`export type ${enumName}Icon = ${fileNames.map(name => `"${name}"`).join(' | ')}\n` +
`export const ${enumName}Prefix = "${options.classNamePrefix}-"`
);
console.log(`${color.green('SUCCESS')} Created ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} Created ${DIST_PATH}`);
}

/*
Expand All @@ -157,7 +158,7 @@ export function createTTF(options: SvgToFontOptions = {}): Promise<Buffer> {
if (err) {
return reject(err);
}
console.log(`${color.green('SUCCESS')} ${color.blue('TTF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue('TTF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(ttfBuf);
});
});
Expand All @@ -175,7 +176,7 @@ export function createEOT(options: SvgToFontOptions = {}, ttf: Buffer) {
if (err) {
return reject(err);
}
console.log(`${color.green('SUCCESS')} ${color.blue('EOT')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue('EOT')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(eot);
});
});
Expand All @@ -192,7 +193,7 @@ export function createWOFF(options: SvgToFontOptions = {}, ttf: Buffer) {
if (err) {
return reject(err);
}
console.log(`${color.green('SUCCESS')} ${color.blue('WOFF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue('WOFF')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve(woff);
});
});
Expand All @@ -209,7 +210,7 @@ export function createWOFF2(options: SvgToFontOptions = {}, ttf: Buffer) {
if (err) {
return reject(err);
}
console.log(`${color.green('SUCCESS')} ${color.blue('WOFF2')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue('WOFF2')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve({
path: DIST_PATH
});
Expand Down Expand Up @@ -239,7 +240,7 @@ export function createSvgSymbol(options: SvgToFontOptions = {}) {
if (err) {
return reject(err);
}
console.log(`${color.green('SUCCESS')} ${color.blue('Svg Symbol')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
log.log(`${color.green('SUCCESS')} ${color.blue('Svg Symbol')} font successfully created!\n ╰┈▶ ${DIST_PATH}`);
resolve({
path: DIST_PATH,
svg: $.html("svg")
Expand Down Expand Up @@ -311,7 +312,7 @@ export function copyTemplate(inDir: string, outDir: string, { _opts, ...vars }:
return null;
}));
}
createdFiles.forEach(filePath => console.log(`${color.green('SUCCESS')} Created ${filePath} `));
createdFiles.forEach(filePath => log.log(`${color.green('SUCCESS')} Created ${filePath} `));
resolve(createdFiles);
})
});
Expand Down

0 comments on commit 51fd8c5

Please sign in to comment.