Skip to content

Commit

Permalink
chore: 폴더가 없을 때 deleteAllFilesInDir 예외처리 및 폴더 생성은 한번만 (#30)
Browse files Browse the repository at this point in the history
* chore: 폴더가 없을 때 deleteAllFilesInDir 예외처리 및 폴더 생성은 한번만

* chore: changeset
  • Loading branch information
junghyeonsu authored Dec 2, 2023
1 parent 013fddd commit ccd993b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/cold-rice-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@icona/generator": patch
"@icona/utils": patch
"@icona/types": patch
---

chore: 폴더가 없을 때 deleteAllFilesInDir 예외처리 및 폴더 생성은 한번만
8 changes: 6 additions & 2 deletions packages/generator/src/core/drawable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions packages/generator/src/core/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/generator/src/core/png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions packages/generator/src/core/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/core/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit ccd993b

Please sign in to comment.