diff --git a/README.md b/README.md index bd73fdce..1f7bd4b4 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,16 @@ async function creatFont() { > svgtofont(options) + + + "log": false, + +### log + +> Type: `Boolean` + +A value of `false` disables logging + ### dist > Type: `String` diff --git a/src/cli.ts b/src/cli.ts index 35bfa18a..b6d49f7e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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; @@ -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(); } @@ -50,7 +51,7 @@ svgtofont({ }, }) .then(() => { - console.log('done!'); + log.log('done!'); }).catch((err) => { - console.log('SvgToFont:ERR:', err); + log.log('SvgToFont:ERR:', err); }); diff --git a/src/index.ts b/src/index.ts index 5c386923..c031424b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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; @@ -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); } } diff --git a/src/log.ts b/src/log.ts new file mode 100644 index 00000000..e045fe51 --- /dev/null +++ b/src/log.ts @@ -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(); \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 11b02a7b..ec181876 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 = {}; /** @@ -44,7 +45,7 @@ export function createSVG(options: SvgToFontOptions = {}): Promise { - 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) => { @@ -130,7 +131,7 @@ export async function createTypescript(options: Omit `"${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}`); } /* @@ -157,7 +158,7 @@ export function createTTF(options: SvgToFontOptions = {}): Promise { 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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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 }); @@ -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") @@ -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); }) });