Skip to content

Commit

Permalink
feat: add generate mode option (#27)
Browse files Browse the repository at this point in the history
* feat: add recreate generate mode

* chore: changeset
  • Loading branch information
junghyeonsu authored Nov 30, 2023
1 parent de32682 commit c43e052
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 12 deletions.
10 changes: 10 additions & 0 deletions .changeset/odd-yaks-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@icona/generator": minor
"@icona/types": minor
"@icona/utils": minor
---

feat: add generate mode option

- `overwrite` (default): overwrite existing files in folder
- `recreate`: rm -rf all files and generate new files in folder
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Shorten the process between designers (Figma) and developers (GitHub).
- **Designer**: Build icon set in Figma and click deploy button, that's it.
- **Developer**: Check the PR and use the icon svg set in your project. (Make react component, Make Pdf, Make XML, ...etc)

## Boilerplate

- [junghyeonsu/test-icona](https://github.com/junghyeonsu/test-icona)

## Preview

![preview](./images/preview.png)
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/core/drawable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenerateDrawableConfig, IconaIconData } from "@icona/types";
import {
deleteAllFilesInDir,
getIconaIconsFile,
getProjectRootPath,
makeFolderIfNotExistFromRoot,
Expand Down Expand Up @@ -30,6 +31,10 @@ export const generateDrawable = ({
throw new Error("There is no icons data");
}

if (config.genMode === "recreate") {
deleteAllFilesInDir(resolve(projectPath, path));
}

// TODO: Name transform option
Object.entries(icons).forEach(async ([name, data]) => {
const { svg } = data;
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/core/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GeneratePDFConfig, IconaIconData } from "@icona/types";
import {
deleteAllFilesInDir,
getIconaIconsFile,
getProjectRootPath,
makeFolderIfNotExistFromRoot,
Expand Down Expand Up @@ -31,6 +32,10 @@ export const generatePDF = ({
throw new Error("There is no icons data");
}

if (config.genMode === "recreate") {
deleteAllFilesInDir(resolve(projectPath, path));
}

// TODO: Name transform option
Object.entries(icons).forEach(async ([name, data]) => {
const { svg } = data;
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/core/png.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GeneratePNGConfig, IconaIconData } from "@icona/types";
import {
deleteAllFilesInDir,
getIconaIconsFile,
getProjectRootPath,
makeFolderIfNotExistFromRoot,
Expand Down Expand Up @@ -27,6 +28,10 @@ export const generatePNG = ({
throw new Error("There is no icons data");
}

if (config.genMode === "recreate") {
deleteAllFilesInDir(resolve(projectPath, path));
}

// TODO: Name transform option
Object.entries(icons).forEach(([name, data]) => {
const { png } = data;
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/core/react.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenerateReactConfig, IconaIconData } from "@icona/types";
import {
deleteAllFilesInDir,
getIconaIconsFile,
getProjectRootPath,
makeFolderIfNotExistFromRoot,
Expand Down Expand Up @@ -30,6 +31,10 @@ export const generateReact = ({
throw new Error("There is no icons data");
}

if (config.genMode === "recreate") {
deleteAllFilesInDir(resolve(projectPath, path));
}

// TODO: Name transform option
Object.entries(icons).forEach(async ([name, data]) => {
const { svg } = data;
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/core/svg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GenerateSVGConfig, IconaIconData } from "@icona/types";
import {
deleteAllFilesInDir,
getIconaIconsFile,
getProjectRootPath,
makeFolderIfNotExistFromRoot,
Expand Down Expand Up @@ -29,6 +30,10 @@ export const generateSVG = ({
throw new Error("There is no icons data");
}

if (config.genMode === "recreate") {
deleteAllFilesInDir(resolve(projectPath, path));
}

// TODO: Name transform option
Object.entries(icons).forEach(([name, data]) => {
const { svg } = data;
Expand Down
3 changes: 1 addition & 2 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"url": "git+https://github.com/daangn/icona.git",
"directory": "packages/types"
},
"main": "./dist/index.d.ts",
"main": "./src/index.d.ts",
"files": [
"dist",
"src"
],
"scripts": {
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions packages/types/src/lib.ts → packages/types/src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ export interface SvgToPdfOptions extends SVGtoPDFOptions {
x?: number;
y?: number;
}
/**
* @param overwrite overwrite existing files in folder
* @param recreate rm -rf all files and generate new files in folder
*/
export type GenerateMode = "recreate" | "overwrite";

export interface GeneratePDFConfig {
/**
* @default overwrite
*/
genMode?: GenerateMode;

/**
* generate drawable pdf files
* @default false
Expand Down Expand Up @@ -60,6 +71,11 @@ export interface GeneratePDFConfig {
}

export interface GenerateReactConfig {
/**
* @default overwrite
*/
genMode?: GenerateMode;

/**
* generate drawable react files
* @default false
Expand All @@ -80,6 +96,11 @@ export interface GenerateReactConfig {
}

export interface GenerateSVGConfig {
/**
* @default overwrite
*/
genMode?: GenerateMode;

/**
* generate drawable svg files
* @default true
Expand Down Expand Up @@ -108,6 +129,11 @@ interface Svg2vectordrawableOptions {
}

export interface GenerateDrawableConfig {
/**
* @default overwrite
*/
genMode?: GenerateMode;

/**
* generate drawable xml files
* @default false
Expand Down Expand Up @@ -135,6 +161,11 @@ export interface GenerateDrawableConfig {
}

export interface GeneratePNGConfig {
/**
* @default overwrite
*/
genMode?: GenerateMode;

/**
* generate drawable xml files
* @default false
Expand Down
9 changes: 1 addition & 8 deletions packages/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"isolatedModules": true,
"esModuleInterop": true,
"declaration": true,
"outDir": "dist",
"rootDir": "src",
"emitDeclarationOnly": true
},
"include": ["./src"],
"include": ["./src/**/*"],
}
21 changes: 19 additions & 2 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// TODO: Need test code for this file.
import type { IconaIconData } from "@icona/types";
import findup from "findup-sync";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { dirname, resolve } from "path";
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
unlinkSync,
writeFileSync,
} from "fs";
import path, { dirname, resolve } from "path";

export const ICONA_FOLDER = ".icona";
export const ICONA_ICONS_FILE = "icons.json";
Expand Down Expand Up @@ -70,3 +77,13 @@ export const generateConfigFile = (config: string) => {

writeFileSync(configPath, config);
};

export const deleteAllFilesInDir = (dirPath: string) => {
try {
readdirSync(dirPath).forEach((file) => {
unlinkSync(path.join(dirPath, file));
});
} catch (error) {
console.log(error);
}
};

0 comments on commit c43e052

Please sign in to comment.