import path from 'path' ; import fs from 'fs-extra' ; import image2uri from 'image2uri' ; import { SVGIcons2SVGFontOptions } from 'svgicons2svgfont' ; import color from 'colors-cli' ; import { OptimizeOptions } from 'svgo' ; import { generateIconsSource , generateReactIcons } from './generate' ; import { createSVG , createTTF , createEOT , createWOFF , createWOFF2 , createSvgSymbol , copyTemplate , CSSOptions , createHTML , createTypescript , TypescriptOptions } from './utils' ; export type SvgToFontOptions = { default example dist ? : string ; default example src ? : string ; default fontName ? : string ; /**
     * Create CSS/LESS/Scss/Styl files, default `true`.
     */ css ? : boolean | CSSOptions ; /**
     * Output `./dist/react/`, SVG generates `react` components.
     */ outSVGReact ? : boolean ; example outSVGPath ? : boolean ; /**
     * This is the setting for [svgicons2svgfont](https://github.com/nfroidure/svgicons2svgfont/tree/dd713bea4f97afa59f7dba6a21ff7f22db565bcf#api)
     */ svgicons2svgfont ? : SVGIcons2SVGFontOptions ; /** Some options can be configured with svgoOptions though it. [svgo](https://github.com/svg/svgo#configuration) */ svgoOptions ? : OptimizeOptions ; default classNamePrefix ? : SvgToFontOptions [ 'fontName' ] ; /**
    * Directory of custom templates.
    */ styleTemplates ? : string ; default startUnicode ? : number ; default useNameAsUnicode ? : boolean ; default emptyDist ? : boolean ; /**
     * This is the setting for [svg2ttf](https://github.com/fontello/svg2ttf/tree/c33a126920f46b030e8ce960cc7a0e38a6946bbc#svg2ttfsvgfontstring-options---buf)
     */ svg2ttf ? : unknown ; website ? : { like corners ? : { example url ? : string ; default width ? : number ; default height ? : number ; default bgColor ? : '#dc3545' ; } ; default index ? : 'font-class' | 'unicode' | 'symbol' ; /**
         * website title
         */ title ? : string ; example favicon ? : string ; example logo ? : string ; version ? : string ; meta ? : { description ? : string ; keywords ? : string ; } ; description ? : string ; template ? : string ; footerInfo ? : string ; links : Array < { title : string ; url : string ; } > ; } ; default typescript ? : boolean | TypescriptOptions ; } ; export default async ( options = { } ) => { const pkgPath = path . join ( process . cwd ( ) , 'package.json' ) ; if ( fs . pathExistsSync ( pkgPath ) ) { const pkg = require ( pkgPath ) ; if ( pkg . svgtofont ) { options = { ... options , ... pkg . svgtofont } ; } } options . dist = options . dist || path . join ( process . cwd ( ) , 'fonts' ) ; options . src = options . src || path . join ( 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 . classNamePrefix = options . classNamePrefix || options . fontName ; const fontSize = options . css && typeof options . css !== 'boolean' && options . css . fontSize ? options . css . fontSize : '16px' ; if ( options . website && ! options . css ) options . css = true ; try { if ( options . emptyDist ) { await fs . emptyDir ( options . dist ) ; } await fs . ensureDir ( options . dist ) ; const unicodeObject = await createSVG ( options ) ; let cssString = [ ] ; let cssToVars = [ ] ; let cssIconHtml = [ ] ; let unicodeHtml = [ ] ; let symbolHtml = [ ] ; const prefix = options . classNamePrefix || options . fontName ; Object . keys ( unicodeObject ) . forEach ( name => { const _code = unicodeObject [ name ] ; let symbolName = options . classNamePrefix + '-' + name ; let iconPart = symbolName + '">' ; let encodedCodes = _code . charCodeAt ( 0 ) ; if ( options . useNameAsUnicode ) { symbolName = name ; iconPart = prefix + '">' + name ; encodedCodes = _code . split ( '' ) . map ( x => x . charCodeAt ( 0 ) ) . join ( ';&amp;#' ) ; } else { cssString . push ( `.${ symbolName }:before { content: "\\${ encodedCodes . toString ( 16 ) }"; }\n` ) ; cssToVars . push ( `$${ symbolName }: "\\${ encodedCodes . toString ( 16 ) }";\n` ) ; } 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 ( `
        <li class="symbol">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="${ options . fontName }.symbol.svg#${ symbolName }"></use>
          </svg>
          <h4>${ symbolName }</h4>
        </li>
      ` ) ; } ) ; const ttf = await createTTF ( options ) ; await createEOT ( options , ttf ) ; await createWOFF ( options , ttf ) ; await createWOFF2 ( options , ttf ) ; await createSvgSymbol ( options ) ; if ( options . css ) { const styleTemplatePath = options . styleTemplates || ( ! options . useNameAsUnicode ? path . resolve ( __dirname , 'styles' ) : path . resolve ( __dirname , 'ligature-styles' ) ) ; await copyTemplate ( styleTemplatePath , options . dist , { fontname : options . fontName , cssString : cssString . join ( '' ) , cssToVars : cssToVars . join ( '' ) , fontSize : fontSize , timestamp : new Date ( ) . getTime ( ) , prefix , _opts : typeof options . css === 'boolean' ? { } : { ... options . css } } ) ; } if ( options . typescript ) { await createTypescript ( { ... options , typescript : options . typescript } ) ; } if ( options . website ) { const pageName = [ 'font-class' , 'unicode' , 'symbol' ] ; let fontClassPath = path . join ( options . dist , 'index.html' ) ; let unicodePath = path . join ( options . dist , 'unicode.html' ) ; let symbolPath = path . join ( options . dist , 'symbol.html' ) ; const indexName = pageName . includes ( options . website . index ) ? pageName . indexOf ( options . website . index ) : 0 ; pageName . forEach ( ( name , index ) => { const _path = path . join ( options . dist , indexName === index ? 'index.html' : `${ name }.html` ) ; if ( name === 'font-class' ) fontClassPath = _path ; if ( name === 'unicode' ) unicodePath = _path ; if ( name === 'symbol' ) symbolPath = _path ; } ) ; options . website . template = options . website . template || path . join ( __dirname , 'website' , 'index.ejs' ) ; const tempData = { meta : null , links : null , corners : null , description : null , footerInfo : null , ... options . website , fontname : options . fontName , classNamePrefix : options . classNamePrefix , _type : 'font-class' , _link : `${ ( options . css && typeof options . css !== 'boolean' && options . css . fileName ) || options . fontName }.css` , _IconHtml : cssIconHtml . join ( '' ) , _title : options . website . title || options . fontName } ; if ( options . website . logo && fs . pathExistsSync ( options . website . logo ) && path . extname ( options . website . logo ) === '.svg' ) { tempData . logo = fs . readFileSync ( options . website . logo ) . toString ( ) ; } if ( options . website . favicon && fs . pathExistsSync ( options . website . favicon ) ) { tempData . favicon = image2uri ( options . website . favicon ) ; } else { tempData . favicon = '' ; } const classHtmlStr = await createHTML ( options . website . template , tempData ) ; fs . outputFileSync ( fontClassPath , classHtmlStr ) ; console . 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 } ` ) ; 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 } ` ) ; } if ( options . outSVGPath ) { const outPath = await generateIconsSource ( options ) ; console . log ( `${ color . green ( 'SUCCESS' ) } Created ${ outPath } ` ) ; } if ( options . outSVGReact ) { const outPath = await generateReactIcons ( options ) ; console . log ( `${ color . green ( 'SUCCESS' ) } Created React Components. ` ) ; } } catch ( error ) { console . log ( 'SvgToFont:CLI:ERR:' , error ) ; } } ; /**
 * https://github.com/Microsoft/TypeScript/issues/5565#issuecomment-155226290
 */ module . exports = exports [ "default" ] ;