All files utils.ts

100% Statements 202/202
92.55% Branches 87/94
100% Functions 48/48
100% Lines 188/188

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505                              5x         5x           13x 10x 10x 10x     10x 10x   10x 10x         21x   21x   21x     21x   21x 21x   21x 21x 1x 1x     21x 2x   21x 21x       2x 2x 2x 2x 2x 2x 2x 2x     10x   10x     9x 9x 9x     1x 1x     10x 22x 7x 7x   7x 1x 1x   1x 2x 2x 2x 2x   2x                 1x           1x       21x       10x                                   5x 24x   28x               46x 45x 45x 1x     44x 141x 131x   131x 130x     44x       20x 32x           17x 17x 3x                     144x     144x 124x                                 43x 128x 128x 128x 2x         126x     40x 40x 126x 126x 113x   113x 40x 7x 26x   7x       33x                       2x 2x 2x 2x 13x 2x       13x   13x 13x       2x             10x 10x 10x 10x 10x 10x 10x 1x   9x 9x                 7x 7x 7x 7x   7x 7x 1x   6x 6x                 7x 7x 7x 7x 7x 1x   6x 6x                 6x 6x 6x 6x 6x 1x   5x 5x                     7x 7x 7x 7x 7x 19x     19x   19x 19x 19x 19x 19x 19x     7x 7x 1x   6x 6x                                                                                       5x           9x 9x           9x 9x 41x 39x 37x 438x 37x 37x 37x 37x 37x               18x                 18x         13x 13x             13x 13x 9x   13x 13x 65x   48x 9x   39x   13x 13x     5x 12x                          
import { SVGIcons2SVGFontStream } from 'svgicons2svgfont';
import fs, { ReadStream } from 'fs-extra';
import path from 'path';
import color from 'colors-cli';
import { load } from 'cheerio';
import svg2ttf from 'svg2ttf';
import ttf2eot from 'ttf2eot';
import ttf2woff from 'ttf2woff';
import ttf2woff2 from 'ttf2woff2';
import nunjucks from 'nunjucks';
import { merge } from 'auto-config-loader';
import { type SvgToFontOptions } from './index.js';
import { log } from './log.js';
import { analyzeSvgColors, generateLayerSvgs, type MulticolorMap, type CreateSVGResult } from './multicolor.js';
 
let UnicodeObj: Record<string, string> = {};
/**
 * Unicode Private Use Area start.
 * https://en.wikipedia.org/wiki/Private_Use_Areas
 */
let startUnicode = 0xea01;
 
/**
 * SVG to SVG font
 */
export function createSVG(options: SvgToFontOptions = {}): Promise<CreateSVGResult> {
  const icons = listIconFiles(options.src);
  startUnicode = options.startUnicode
  UnicodeObj = {}
  const multicolorMap: MulticolorMap = {};
 
  // Temp directory for multicolor layer SVGs
  const tempDir = options.multicolor ? path.join(options.dist, '.multicolor-tmp') : null;
  if (tempDir) fs.ensureDirSync(tempDir);
 
  return new Promise(async (resolve, reject) => {
    const fontStream = new SVGIcons2SVGFontStream({
      ...options.svgicons2svgfont
    });
 
    function writeFontStream(svgPath: string, originalName: string, name: string) {
      const glyph = fs.createReadStream(svgPath) as ReadStream & { metadata: { unicode: string[], name: string } };
 
      const curUnicode = String.fromCodePoint(startUnicode);
      // Callbacks keep matching on the SVG file name. Classes use `name`.
      const [_curUnicode, _startUnicode] = options.getIconUnicode
        ? (options.getIconUnicode(originalName, curUnicode, startUnicode) || [curUnicode]) : [curUnicode];
 
      if (_startUnicode) startUnicode = _startUnicode;
 
      const unicode: string[] = [_curUnicode];
      if (curUnicode === _curUnicode && (!_startUnicode || startUnicode === _startUnicode)) startUnicode++;
 
      UnicodeObj[name] = unicode[0];
      if (!!options.useNameAsUnicode) {
        unicode[0] = originalName;
        UnicodeObj[name] = originalName;
      }
      // Ligatures stay the original file name. That string is font text, not a class.
      if (!!options.addLigatures) {
        unicode.push(originalName)
      }
      glyph.metadata = { unicode, name };
      fontStream.write(glyph);
    }
 
    function writeGlyphFromFile(filePath: string, glyphName: string): { unicode: string; codepoint: number } {
      const glyph = fs.createReadStream(filePath) as ReadStream & { metadata: { unicode: string[], name: string } };
      const curUnicode = String.fromCodePoint(startUnicode);
      const unicode: string[] = [curUnicode];
      UnicodeObj[glyphName] = unicode[0];
      startUnicode++;
      glyph.metadata = { unicode, name: glyphName };
      fontStream.write(glyph);
      return { unicode: curUnicode, codepoint: curUnicode.codePointAt(0)! };
    }
 
    const DIST_PATH = path.join(options.dist, options.fontName + ".svg");
    // Setting the font destination
    fontStream.pipe(fs.createWriteStream(DIST_PATH))
      .on("finish", () => {
        // Clean up temp directory
        if (tempDir) fs.removeSync(tempDir);
        log.log(`${color.green('SUCCESS')} ${color.blue_bt('SVG')} font successfully created!\n  ╰┈▶ ${DIST_PATH}`);
        resolve({ unicodeObject: UnicodeObj, multicolorMap });
      })
      .on("error", (err) => {
        Eif (err) {
          reject(err);
        }
      });
    icons.forEach(({ filePath, originalName, name }) => {
      if (options.multicolor) {
        const svgContent = fs.readFileSync(filePath, 'utf-8');
        const analysis = analyzeSvgColors(svgContent);
 
        if (analysis.isMulticolor) {
          const layerSvgs = generateLayerSvgs(analysis);
          const layers: import('./multicolor.js').MulticolorLayerInfo[] = [];
 
          layerSvgs.forEach((layer, idx) => {
            const glyphName = `${name}-path${idx + 1}`;
            const tmpPath = path.join(tempDir!, glyphName + '.svg');
            fs.writeFileSync(tmpPath, layer.svgContent);
            const result = writeGlyphFromFile(tmpPath, glyphName);
 
            layers.push({
              layerIndex: idx,
              glyphName,
              unicode: result.unicode,
              color: layer.color,
              encodedCode: result.codepoint,
            });
          });
 
          multicolorMap[name] = {
            originalName,
            layerCount: layers.length,
            layers,
          };
 
          return; // Skip normal processing for this multicolor icon
        }
      }
 
      writeFontStream(filePath, originalName, name);
    });
 
    // Do not forget to end the stream
    fontStream.end();
  });
}
 
/**
 * Converts a string to pascal case.
 * 
 * @example
 * 
 * ```js
 * toPascalCase('some_database_field_name'); // 'SomeDatabaseFieldName'
 * toPascalCase('Some label that needs to be pascalized');
 * // 'SomeLabelThatNeedsToBePascalized'
 * toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty'
 * toPascalCase('some-mixed_string with spaces_underscores-and-hyphens');
 * // 'SomeMixedStringWithSpacesUnderscoresAndHyphens'
 * ```
 */
export const toPascalCase = (str: string) =>
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.charAt(0).toUpperCase() + x.slice(1).toLowerCase())
    .join('');
 
/*
 * Filter svg files
 * @return {Array} svg files
 */
export function filterSvgFiles(svgFolderPath: string): string[] {
  let files = fs.readdirSync(svgFolderPath, 'utf-8');
  let svgArr = [];
  if (!files) {
    throw new Error(`Error! Svg folder is empty.${svgFolderPath}`);
  }
 
  for (let i in files) {
    if (typeof files[i] !== 'string' || path.extname(files[i]) !== '.svg') continue;
    const filePath = path.join(svgFolderPath, files[i]);
    // Directory entries are basenames, so compare the path that is stored.
    if (!svgArr.includes(filePath)) {
      svgArr.push(filePath);
    }
  }
  return svgArr;
}
 
export function snakeToUppercase(str: string) {
  return str.split(/[-_]/)
    .map(partial => partial.charAt(0).toUpperCase() + partial.slice(1))
    .join('')
}
 
/** Enum member for a normalized icon name. Leading digits are not valid identifiers. */
export function toEnumMember(name: string): string {
  const member = snakeToUppercase(name);
  if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(member)) return member;
  return `_${member}`;
}
 
/**
 * CSS-ident-safe icon name. Characters outside [A-Za-z0-9_-] become "-".
 * Leading and trailing hyphens are removed. A leading digit is prefixed
 * with "_" so the result can be a class, a SCSS variable, and an SVG id.
 * Names that are already valid are left unchanged. Ligatures keep using
 * the original file name.
 */
export function normalizeIconName(fileName: string): string {
  const name = fileName
    .replace(/[^A-Za-z0-9_-]+/g, '-')
    .replace(/^-+|-+$/g, '');
  if (/^[0-9]/.test(name)) return `_${name}`;
  return name;
}
 
export type IconFile = {
  filePath: string;
  /** SVG file name without the extension. */
  originalName: string;
  /** Normalized name used for classes, variables, and ids. */
  name: string;
};
 
/**
 * SVG icons in a folder, with file names normalized to one shared identifier.
 * Throws when a file name cannot be normalized, or when two files normalize
 * to the same name.
 */
export function listIconFiles(svgFolderPath: string): IconFile[] {
  const icons = filterSvgFiles(svgFolderPath).map((filePath) => {
    const originalName = path.basename(filePath, path.extname(filePath));
    const name = normalizeIconName(originalName);
    if (!name) {
      throw new Error(
        `Cannot derive a valid icon name from "${originalName}". ` +
        `Use a file name that contains a letter, digit, "_" or "-".`
      );
    }
    return { filePath, originalName, name };
  });
 
  const groups = new Map<string, string[]>();
  for (const icon of icons) {
    const originals = groups.get(icon.name);
    if (originals) originals.push(icon.originalName);
    else groups.set(icon.name, [icon.originalName]);
  }
  const collisions = [...groups.entries()].filter(([, originals]) => originals.length > 1);
  if (collisions.length > 0) {
    const details = collisions
      .map(([name, originals]) => `${originals.map((original) => `"${original}"`).join(', ')} → "${name}"`)
      .join('; ');
    throw new Error(
      `Icon names collide after normalization: ${details}. Rename the SVG files so each icon name is unique.`
    );
  }
  return icons;
}
 
export type TypescriptOptions = {
  extension?: 'd.ts' | 'ts' | 'tsx',
  enumName?: string
}
 
/**
 * Create typescript declarations for icon classnames
 */
export async function createTypescript(options: Omit<SvgToFontOptions, 'typescript'> & { typescript: TypescriptOptions | true }) {
  const tsOptions = options.typescript === true ? {} : options.typescript;
  const uppercaseFontName = snakeToUppercase(options.fontName);
  const { extension = 'd.ts', enumName = uppercaseFontName } = tsOptions;
  const DIST_PATH = path.join(options.dist, `${options.fontName}.${extension}`);
  const fileNames = listIconFiles(options.src).map(icon => icon.name);
  await fs.writeFile(
    DIST_PATH,
    [
      `export enum ${enumName} {`,
      ...fileNames.map(name => `  ${toEnumMember(name)} = "${options.classNamePrefix}-${name}",`),
      '}',
      `export type ${enumName}Classname = ${fileNames.map(name => `"${options.classNamePrefix}-${name}"`).join(' | ')}`,
      `export type ${enumName}Icon = ${fileNames.map(name => `"${name}"`).join(' | ')}`,
      `export const ${enumName}Prefix = "${options.classNamePrefix}-"`,
    ].join('\n'),
  );
  log.log(`${color.green('SUCCESS')} Created ${DIST_PATH}`);
}
 
/**
 * SVG font to TTF
 */
export function createTTF(options: SvgToFontOptions = {}): Promise<Buffer> {
  return new Promise((resolve, reject) => {
    options.svg2ttf = options.svg2ttf || {};
    const DIST_PATH = path.join(options.dist, options.fontName + ".ttf");
    let ttf = svg2ttf(fs.readFileSync(path.join(options.dist, options.fontName + ".svg"), "utf8"), options.svg2ttf);
    const ttfBuf = Buffer.from(ttf.buffer);
    fs.writeFile(DIST_PATH, ttfBuf, (err: NodeJS.ErrnoException) => {
      if (err) {
        return reject(err);
      }
      log.log(`${color.green('SUCCESS')} ${color.blue_bt('TTF')} font successfully created!\n  ╰┈▶ ${DIST_PATH}`);
      resolve(ttfBuf);
    });
  });
};
 
/**
 * TTF font to EOT
 */
export function createEOT(options: SvgToFontOptions = {}, ttf: Buffer) {
  return new Promise((resolve, reject) => {
    const DIST_PATH = path.join(options.dist, options.fontName + '.eot');
    const ttf2eotResult = ttf2eot(ttf);
    const eot = Buffer.from(ttf2eotResult.buffer, ttf2eotResult.byteOffset, ttf2eotResult.byteLength);
 
    fs.writeFile(DIST_PATH, eot, (err: NodeJS.ErrnoException) => {
      if (err) {
        return reject(err);
      }
      log.log(`${color.green('SUCCESS')} ${color.blue_bt('EOT')} font successfully created!\n  ╰┈▶ ${DIST_PATH}`);
      resolve(eot);
    });
  });
};
 
/**
 * TTF font to WOFF
 */
export function createWOFF(options: SvgToFontOptions = {}, ttf: Buffer) {
  return new Promise((resolve, reject) => {
    const DIST_PATH = path.join(options.dist, options.fontName + ".woff");
    const woff = Buffer.from(ttf2woff(ttf).buffer);
    fs.writeFile(DIST_PATH, woff, (err) => {
      if (err) {
        return reject(err);
      }
      log.log(`${color.green('SUCCESS')} ${color.blue_bt('WOFF')} font successfully created!\n  ╰┈▶ ${DIST_PATH}`);
      resolve(woff);
    });
  });
};
 
/**
 * TTF font to WOFF2
 */
export function createWOFF2(options: SvgToFontOptions = {}, ttf: Buffer) {
  return new Promise((resolve, reject) => {
    const DIST_PATH = path.join(options.dist, options.fontName + ".woff2");
    const woff2 = Buffer.from(ttf2woff2(ttf).buffer);
    fs.writeFile(DIST_PATH, woff2, (err) => {
      if (err) {
        return reject(err);
      }
      log.log(`${color.green('SUCCESS')} ${color.blue_bt('WOFF2')} font successfully created!\n  ╰┈▶ ${DIST_PATH}`);
      resolve({
        path: DIST_PATH
      });
    });
  });
};
 
/**
 * Create SVG Symbol
 */
export function createSvgSymbol(options: SvgToFontOptions = {}) {
  const DIST_PATH = path.join(options.dist, `${options.fontName}.symbol.svg`);
  const $ = load('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0" style="display:none;"></svg>');
  const icons = listIconFiles(options.src);
  return new Promise((resolve, reject) => {
    icons.forEach(({ filePath, name }) => {
      let file = fs.readFileSync(filePath, "utf8");
 
      // trim xml declaration
      file = file.replace(/<\?xml.*?\?>\s*/g, '').trim();
 
      const svgNode = $(file);
      const symbolNode = $("<symbol></symbol>");
      symbolNode.attr("viewBox", svgNode.attr("viewBox"));
      symbolNode.attr("id", `${options.classNamePrefix}-${name}`);
      symbolNode.append(svgNode.html());
      $('svg').append(symbolNode);
    });
 
    fs.writeFile(DIST_PATH, $.html("svg"), (err) => {
      if (err) {
        return reject(err);
      }
      log.log(`${color.green('SUCCESS')} ${color.blue_bt('Svg Symbol')} font successfully created!\n  ╰┈▶ ${DIST_PATH}`);
      resolve({
        path: DIST_PATH,
        svg: $.html("svg")
      });
    });
  });
};
 
export type CSSOptions = {
  /**
   * Output the css file to the specified directory
   */
  output?: string;
  /**
   * Which files are exported.
   */
  include?: RegExp;
  /**
   * Setting font size.
   */
  fontSize?: string | boolean;
  /**
   * Set the path in the css file
   * https://github.com/jaywcjlove/svgtofont/issues/48#issuecomment-739547189
   */
  cssPath?: string;
  /**
   * Set file name
   * https://github.com/jaywcjlove/svgtofont/issues/48#issuecomment-739547189
   */
  fileName?: string;
  /**
   * Ad hoc template variables.
   */
  templateVars?: Record<string, any>;
  /**
   * When including CSS files in a CSS file,
   * you can add a timestamp parameter or custom text to the file path to prevent browser caching issues and ensure style updates are applied. @default true
   * @example `path/to/iconfont.css?t=1612345678`
   */
  hasTimestamp?: boolean | string;
}
 
// As we are processing css files, we need to eacape HTML entities.
const safeNunjucks = nunjucks.configure({ autoescape: false });
 
/**
 * Copy template files
 */
export async function copyTemplate(inDir: string, outDir: string, { _opts, ...vars }: Record<string, any> & { _opts: CSSOptions }) {
  const files = await fs.readdir(inDir, { withFileTypes: true });
  const context = {
    ...(_opts.templateVars || {}),
    ...vars,
    cssPath: _opts.cssPath || '',
    filename: _opts.fileName || vars.fontname,
  }
  await fs.ensureDir(outDir);
  for (const file of files) {
    if (!file.isFile()) continue;
    if (_opts.include && !(new RegExp(_opts.include)).test(file.name)) continue;
    let newFileName = file.name.replace(/\.template$/, '').replace(/^_/, '');
    for (const key in context) newFileName = newFileName.replace(`{{${key}}}`, `${context[key]}`);
    const template = await fs.readFile(path.join(inDir, file.name), 'utf8');
    const content = safeNunjucks.renderString(template, context);
    const filePath = path.join(outDir, newFileName)
    await fs.writeFile(filePath, content);
    log.log(`${color.green('SUCCESS')} Created ${filePath} `);
  }
};
 
/**
 * Create HTML
 */
export function createHTML(templatePath: string, data: Record<string, any>): string {
  return nunjucks.renderString(fs.readFileSync(templatePath, 'utf8'), {
    ...data,
    Date: Date,
    JSON: JSON,
    Math: Math,
    Number: Number,
    Object: Object,
    RegExp: RegExp,
    String: String,
    typeof: (v: any) => typeof v,
  });
};
 
export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp: number, excludeFormat: string[], hasTimestamp: boolean | string = true): string {
  const timestamString = hasTimestamp === true ? `?t=${timestamp}` : (typeof hasTimestamp == 'string' ? `?t=${hasTimestamp}` : undefined);
  const formats = [
    { ext: 'eot', format: 'embedded-opentype', ieFix: true },
    { ext: 'woff2', format: 'woff2' },
    { ext: 'woff', format: 'woff' },
    { ext: 'ttf', format: 'truetype' },
    { ext: 'svg', format: 'svg' }
  ];
  let cssString = `  font-family: "${fontName}";\n`;
  if (!excludeFormat.includes('eot')) {
    cssString += `  src: url('${cssPath}${fontName}.eot${timestamString || ''}'); /* IE9*/\n`;
  }
  cssString += '  src: ';
  const srcParts = formats
    .filter(format => !excludeFormat.includes(format.ext))
    .map(format => {
      if (format.ext === 'eot') {
        return `url('${cssPath}${fontName}.eot${timestamString || '?'}#iefix') format('${format.format}') /* IE6-IE8 */`;
      }
      return `url('${cssPath}${fontName}.${format.ext}${timestamString || ''}') format('${format.format}')`;
    });
  cssString += srcParts.join(',\n  ') + ';';
  return cssString;
}
 
export const getDefaultOptions = (options: SvgToFontOptions): SvgToFontOptions => {
  return merge({
    dist: path.resolve(process.cwd(), 'fonts'),
    src: path.resolve(process.cwd(), 'svg'),
    startUnicode: 0xea01,
    svg2ttf: {},
    svgicons2svgfont: {
      fontName: 'iconfont',
    },
    fontName: 'iconfont',
    symbolNameDelimiter: '-',
    multicolor: false,
  }, options);
};