Skip to content

Commit

Permalink
fix: check icon
Browse files Browse the repository at this point in the history
  • Loading branch information
gronxb committed Sep 14, 2024
1 parent f3fa74f commit 0488ec1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ import { syncIcons } from "../utils/generateIcon";
import { addIcons, checkIfConfigExists } from "../utils//config";
import { generateBaseCode } from "../utils/generateBaseCode";
import { init } from "./init";
import { checkAvailableIcons } from "../utils/checkAvailableIcons";
import { groupIconsByPrefix } from "../utils/groupIconsByPrefix";
import { log } from "../utils/console";

export const add = async (iconNames: string[]) => {
try {
if (!checkIfConfigExists()) {
await init();
}

const groupedIcons = groupIconsByPrefix(iconNames);
await Promise.all(
groupedIcons.map(async ([prefix, icons]) => {
if (!(await checkAvailableIcons(prefix, icons))) {
throw new Error(`Not found ${prefix}`);
}
})
);

const config = await addIcons(iconNames);
await generateBaseCode(config);
await syncIcons(config);
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
log.error(error.message);
} else {
console.error(error);
}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/checkAvailableIcons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from "fs/promises";

export const checkAvailableIcons = async (
prefix: string,
iconNames: string[]
) => {
try {
const icons = import.meta.resolve(`react-icons/${prefix}`);
const file = await fs.readFile(icons, "utf8");
return (
iconNames.length > 0 &&
iconNames.every((iconName) => file.includes(iconName))
);
} catch (error) {
return false;
}
};

0 comments on commit 0488ec1

Please sign in to comment.