import glob from "fast-glob"; import fs from "fs"; import path from "path"; import { formatStyleAttribute, getGroupedPathArray, SVGGroupedItem, } from "./parse"; export const toPascalCase = (str: string = ""): string => { return str .split("-") .filter((word) => word.length > 0) .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) .join(""); }; export interface GenerateResult { pathList: string[]; spriteList: string[]; } export const generate = (inputPath: string): GenerateResult => { const pathList: string[] = []; const spriteList: string[] = []; const matches = glob.sync(inputPath); for (const file of matches) { const name = path.parse(file).name; const pascalName = toPascalCase(name); const svgContent = fs.readFileSync(file, "utf-8"); // 默认输出 24x24 规格的路径 const array: SVGGroupedItem[] = getGroupedPathArray(svgContent, 24); const items: string[] = []; const paths: string[] = []; array.forEach((item) => { // 保持你原有的 currentColor 逻辑 let { fill = "", stroke = "" } = item.styles; if (stroke && stroke !== "none") item.styles.stroke = "currentcolor"; if ((fill && fill !== "none") || Object.keys(item.styles).length === 0) { item.styles.fill = "currentcolor"; } const style = formatStyleAttribute(item.styles); items.push(`{d: "${item.d}", s: "${style}"}`); paths.push(``); }); pathList.push(`export const ${pascalName} = [${items}]`); spriteList.push( `${paths.join("")}`, ); console.log(`Successfully pathified: ${pascalName}`); } return { pathList, spriteList }; };