From ccd993bea3b3a592eae09a8be225bc2d1a244634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=98=84=EC=88=98?= <54893898+junghyeonsu@users.noreply.github.com> Date: Sat, 2 Dec 2023 12:57:13 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=ED=8F=B4=EB=8D=94=EA=B0=80=20?= =?UTF-8?q?=EC=97=86=EC=9D=84=20=EB=95=8C=20deleteAllFilesInDir=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B0=8F=20=ED=8F=B4?= =?UTF-8?q?=EB=8D=94=20=EC=83=9D=EC=84=B1=EC=9D=80=20=ED=95=9C=EB=B2=88?= =?UTF-8?q?=EB=A7=8C=20(#30)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 폴더가 없을 때 deleteAllFilesInDir 예외처리 및 폴더 생성은 한번만 * chore: changeset --- .changeset/cold-rice-happen.md | 7 +++++++ packages/generator/src/core/drawable.ts | 8 ++++++-- packages/generator/src/core/pdf.ts | 9 ++++++--- packages/generator/src/core/png.ts | 8 ++++++-- packages/generator/src/core/react.ts | 8 ++++++-- packages/generator/src/core/svg.ts | 7 ++++++- packages/generator/src/generator.ts | 4 ++++ packages/utils/src/index.ts | 2 ++ 8 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 .changeset/cold-rice-happen.md diff --git a/.changeset/cold-rice-happen.md b/.changeset/cold-rice-happen.md new file mode 100644 index 0000000..55bdb6a --- /dev/null +++ b/.changeset/cold-rice-happen.md @@ -0,0 +1,7 @@ +--- +"@icona/generator": patch +"@icona/utils": patch +"@icona/types": patch +--- + +chore: 폴더가 없을 때 deleteAllFilesInDir 예외처리 및 폴더 생성은 한번만 diff --git a/packages/generator/src/core/drawable.ts b/packages/generator/src/core/drawable.ts index f5626d0..0c611c0 100644 --- a/packages/generator/src/core/drawable.ts +++ b/packages/generator/src/core/drawable.ts @@ -31,14 +31,18 @@ export const generateDrawable = ({ throw new Error("There is no icons data"); } + const iconData = Object.entries(icons); + if (iconData.length !== 0) { + makeFolderIfNotExistFromRoot(path); + } + if (config.genMode === "recreate") { deleteAllFilesInDir(resolve(projectPath, path)); } // TODO: Name transform option - Object.entries(icons).forEach(async ([name, data]) => { + iconData.forEach(async ([name, data]) => { const { svg } = data; - makeFolderIfNotExistFromRoot(path); const drawablePath = resolve(projectPath, path, `${name}.xml`); let drawable = await svg2vectordrawable(svg, drawableConfig); diff --git a/packages/generator/src/core/pdf.ts b/packages/generator/src/core/pdf.ts index b94254b..1d421bd 100644 --- a/packages/generator/src/core/pdf.ts +++ b/packages/generator/src/core/pdf.ts @@ -32,15 +32,18 @@ export const generatePDF = ({ throw new Error("There is no icons data"); } + const iconData = Object.entries(icons); + if (iconData.length !== 0) { + makeFolderIfNotExistFromRoot(path); + } + if (config.genMode === "recreate") { deleteAllFilesInDir(resolve(projectPath, path)); } // TODO: Name transform option - Object.entries(icons).forEach(async ([name, data]) => { + iconData.forEach(async ([name, data]) => { const { svg } = data; - makeFolderIfNotExistFromRoot(path); - const svgPath = resolve(projectPath, path, `${name}.pdf`); /** diff --git a/packages/generator/src/core/png.ts b/packages/generator/src/core/png.ts index a1b9b13..41910fa 100644 --- a/packages/generator/src/core/png.ts +++ b/packages/generator/src/core/png.ts @@ -28,16 +28,20 @@ export const generatePNG = ({ throw new Error("There is no icons data"); } + const iconData = Object.entries(icons); + if (iconData.length !== 0) { + makeFolderIfNotExistFromRoot(path); + } + if (config.genMode === "recreate") { deleteAllFilesInDir(resolve(projectPath, path)); } // TODO: Name transform option - Object.entries(icons).forEach(([name, data]) => { + iconData.forEach(([name, data]) => { const { png } = data; if (!png) return; - makeFolderIfNotExistFromRoot(path); const buffer = Buffer.from(png, "base64"); const pngPath = resolve(projectPath, path, `${name}.png`); writeFileSync(pngPath, buffer); diff --git a/packages/generator/src/core/react.ts b/packages/generator/src/core/react.ts index 2972b31..a57e4ce 100644 --- a/packages/generator/src/core/react.ts +++ b/packages/generator/src/core/react.ts @@ -31,14 +31,18 @@ export const generateReact = ({ throw new Error("There is no icons data"); } + const iconData = Object.entries(icons); + if (iconData.length !== 0) { + makeFolderIfNotExistFromRoot(path); + } + if (config.genMode === "recreate") { deleteAllFilesInDir(resolve(projectPath, path)); } // TODO: Name transform option - Object.entries(icons).forEach(async ([name, data]) => { + iconData.forEach(async ([name, data]) => { const { svg } = data; - makeFolderIfNotExistFromRoot(path); const componentName = name .replace(/^[a-z]/, (ch) => ch.toUpperCase()) diff --git a/packages/generator/src/core/svg.ts b/packages/generator/src/core/svg.ts index 96f266f..41b46d0 100644 --- a/packages/generator/src/core/svg.ts +++ b/packages/generator/src/core/svg.ts @@ -30,12 +30,17 @@ export const generateSVG = ({ throw new Error("There is no icons data"); } + const iconData = Object.entries(icons); + if (iconData.length !== 0) { + makeFolderIfNotExistFromRoot(path); + } + if (config.genMode === "recreate") { deleteAllFilesInDir(resolve(projectPath, path)); } // TODO: Name transform option - Object.entries(icons).forEach(([name, data]) => { + iconData.forEach(([name, data]) => { const { svg } = data; makeFolderIfNotExistFromRoot(path); diff --git a/packages/generator/src/generator.ts b/packages/generator/src/generator.ts index e9d6252..ed21313 100644 --- a/packages/generator/src/generator.ts +++ b/packages/generator/src/generator.ts @@ -14,11 +14,15 @@ export const generator = ( const { pdf, drawable, react, svg, png } = config; const generate = () => { + console.log("[@Icona/generator] Start generating..."); + if (svg?.active) generateSVG({ icons, config: svg }); if (react?.active) generateReact({ icons, config: react }); if (pdf?.active) generatePDF({ icons, config: pdf }); if (drawable?.active) generateDrawable({ icons, config: drawable }); if (png?.active) generatePNG({ icons, config: png }); + + console.log("[@Icona/generator] Finish generating!"); }; return { generate }; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index efd7e97..27c1e06 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -79,6 +79,8 @@ export const generateConfigFile = (config: string) => { }; export const deleteAllFilesInDir = (dirPath: string) => { + if (!existsSync(dirPath)) return; + try { readdirSync(dirPath).forEach((file) => { unlinkSync(path.join(dirPath, file));