diff --git a/src/commands/add.ts b/src/commands/add.ts index 746a605..370c147 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -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); } diff --git a/src/utils/checkAvailableIcons.ts b/src/utils/checkAvailableIcons.ts new file mode 100644 index 0000000..9e4f452 --- /dev/null +++ b/src/utils/checkAvailableIcons.ts @@ -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; + } +};