From 7ccea887359d356e523e5ee32cdf95b7f4313edd Mon Sep 17 00:00:00 2001 From: Nicolas Merget Date: Thu, 5 Sep 2024 07:52:10 +0200 Subject: [PATCH] feat: add overwrite sources for cli --- src/generate-icon-fonts/data.ts | 21 ++++++++------------- src/generate-icon-fonts/gather-icons.ts | 2 +- src/generate-icon-fonts/index.ts | 21 +++++++++++++-------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/generate-icon-fonts/data.ts b/src/generate-icon-fonts/data.ts index 97d598c..df46067 100644 --- a/src/generate-icon-fonts/data.ts +++ b/src/generate-icon-fonts/data.ts @@ -47,22 +47,17 @@ export const gifOptions: ProgrammOptionsType[] = [ description: "Extra logging", defaultValue: false, }, - // TODO: This is buggy we should fix it by making a PR to https://github.com/jaywcjlove/svgtofont -/* { + { + name: "overwriteSources", + short: "ows", + description: "Overwrite all svgs inside src directory", + defaultValue: false, + }, + // TODO: This is buggy we should fix it by making a PR to https://github.com/jaywcjlove/svgtofont + /* { name: "outSVGReact", description: "Creates react svg components", short: "react", defaultValue: false, },*/ ]; - -export const fileEndingsToDelete = [ - "eot", - "less", - "module.less", - "styl", - "svg", - "symbol.svg", - "ttf", - "woff", -]; diff --git a/src/generate-icon-fonts/gather-icons.ts b/src/generate-icon-fonts/gather-icons.ts index 331ac29..fdde79e 100644 --- a/src/generate-icon-fonts/gather-icons.ts +++ b/src/generate-icon-fonts/gather-icons.ts @@ -238,7 +238,7 @@ const gatherIcons = ( } } - return undefined; + return globPaths; }; export default gatherIcons; diff --git a/src/generate-icon-fonts/index.ts b/src/generate-icon-fonts/index.ts index 1aec9e5..0a72456 100644 --- a/src/generate-icon-fonts/index.ts +++ b/src/generate-icon-fonts/index.ts @@ -2,7 +2,6 @@ import FSE from "fs-extra"; -import { fileEndingsToDelete } from "./data"; import svgToFont from "./svg-to-font"; import cleanIcons from "../clean-icons"; import { OptionsType } from "../types"; @@ -18,7 +17,6 @@ const debugLog = (debug: boolean, message: string) => { const generateIconFonts = async (values: OptionsType): Promise => { const { src, - fontName, dryRun, cleanIgnoreVariants, debug = false, @@ -44,7 +42,7 @@ const generateIconFonts = async (values: OptionsType): Promise => { } debugLog(debug, "---Start gathering icon---"); - gatherIcons(temporaryDirectory, values); + const iconPaths = gatherIcons(temporaryDirectory, values); debugLog(debug, "---Start cleaning icon---"); await cleanIcons( @@ -61,16 +59,23 @@ const generateIconFonts = async (values: OptionsType): Promise => { const subTemporaryDir = `${temporaryDirectory}/${directory}`; debugLog(debug, `svgToFont for ${subTemporaryDir}`); await svgToFont(subTemporaryDir, subDist, values); - for (const ending of fileEndingsToDelete) { - FSE.removeSync(`${subDist}/${fontName}.${ending}`); - } FSE.removeSync(`${subDist}/symbol.html`); FSE.removeSync(`${subDist}/unicode.html`); } - if (overwriteSources) { - FSE.copySync(`${temporaryDirectory}/all`, `${src}`, { overwrite: true }); + if (overwriteSources && iconPaths) { + const tempAllDir = `${temporaryDirectory}/all`; + iconPaths.forEach((svgPath) => { + const paths = svgPath.split("/"); + const filename: string = paths.at(-1) || ""; + const tmpFile = `${tempAllDir}/${filename}`; + if (FSE.existsSync(`${tempAllDir}/${filename}`)) { + FSE.copySync(tmpFile, svgPath, { + overwrite: true, + }); + } + }); } if (!debug) {