-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from gronxb/fix/resolution
fix: resolution multiple prefix
- Loading branch information
Showing
18 changed files
with
284 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"typescript": true, | ||
"outputPath": "assets/icons", | ||
"icons": [ | ||
"bs/Bs0Circle", | ||
"fa/Fa500Px", | ||
"fa6/Fa500Px" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expect, describe, it, vi } from "vitest"; | ||
import { IconManager } from "./iconManager"; | ||
|
||
vi.mock("./utils/generateIcon", () => ({ | ||
generateIcon: vi.fn(), | ||
})); | ||
|
||
describe("groupIconsByPrefix", () => { | ||
it("should group icons by name", () => { | ||
const iconManager = new IconManager({ | ||
icons: [ | ||
"ai/AiFillAccountBook", | ||
"ai/AiFillAlert", | ||
"cg/CgAddR", | ||
"fa/FaApple", | ||
"fa6/FaApple", | ||
], | ||
outputPath: "", | ||
typescript: false, | ||
}); | ||
|
||
expect(iconManager.groupedIcons).toEqual([ | ||
["ai", ["AiFillAccountBook", "AiFillAlert"]], | ||
["cg", ["CgAddR"]], | ||
["fa", ["FaApple"]], | ||
["fa6", ["FaApple"]], | ||
]); | ||
}); | ||
|
||
it("should handle empty array", () => { | ||
const iconManager = new IconManager({ | ||
icons: [], | ||
outputPath: "", | ||
typescript: false, | ||
}); | ||
|
||
expect(iconManager.groupedIcons).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { initConfig, type Config } from "./utils/config"; | ||
import fs from "fs/promises"; | ||
import { separateCamelCase } from "./utils/separateCamelCase"; | ||
import { uniq } from "./utils/uniq"; | ||
import { log } from "./utils/console"; | ||
import { generateIconCode } from "./utils/generateIcon"; | ||
import { saveIconCode } from "./utils/saveIconCode"; | ||
import { searchFunction } from "./utils/searchFunction"; | ||
|
||
export class IconManager { | ||
private config: Config; | ||
|
||
private _groupedIcons: [string, string[]][] = []; | ||
|
||
private _prefixCodeMap: Map<string, string> = new Map(); | ||
|
||
private _resolutionTable: Record<string, string[]> = { | ||
fa: ["fa", "fa6"], | ||
hi: ["hi", "hi2"], | ||
io: ["io", "io5"], | ||
}; | ||
|
||
constructor(config: Config) { | ||
this.config = config; | ||
} | ||
|
||
private get icons() { | ||
return this.config.icons; | ||
} | ||
|
||
public async checkIfIconExists(prefix: string, iconName: string) { | ||
const iconCode = await this.getIconCodeByPrefix(prefix); | ||
if (!iconCode) { | ||
return false; | ||
} | ||
|
||
return searchFunction(iconCode, iconName); | ||
} | ||
|
||
public async getResolutionPrefixes(iconName: string) { | ||
const [prefix] = await separateCamelCase(iconName); | ||
const lowerCasePrefix = prefix.toLowerCase(); | ||
|
||
const result: string[] = []; | ||
const resolutionPrefixes = this._resolutionTable[lowerCasePrefix] ?? [ | ||
lowerCasePrefix, | ||
]; | ||
|
||
for (const prefix of resolutionPrefixes) { | ||
try { | ||
if (await this.checkIfIconExists(prefix, iconName)) { | ||
result.push(prefix); | ||
} | ||
} catch {} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async getIconCodeByPrefix(prefix: string) { | ||
if (!this._prefixCodeMap.has(prefix)) { | ||
try { | ||
const prefixPath = await import.meta | ||
.resolve(`react-icons/${prefix}`) | ||
.replace("file://", ""); | ||
|
||
const prefixCode = await fs.readFile(prefixPath, "utf8"); | ||
this._prefixCodeMap.set(prefix, prefixCode); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
return this._prefixCodeMap.get(prefix); | ||
} | ||
|
||
private _groupIconsByPrefix() { | ||
const groupedIcons: { [key: string]: string[] } = {}; | ||
|
||
for (const icon of this.icons) { | ||
const [prefix, name] = icon.split("/"); | ||
if (!groupedIcons[prefix]) { | ||
groupedIcons[prefix] = []; | ||
} | ||
groupedIcons[prefix].push(name); | ||
} | ||
return Object.entries(groupedIcons); | ||
} | ||
|
||
get groupedIcons() { | ||
if (!this._groupedIcons.length) { | ||
this._groupedIcons = this._groupIconsByPrefix(); | ||
} | ||
|
||
return this._groupedIcons; | ||
} | ||
|
||
public async addIcon(prefix: string, iconName: string) { | ||
const updatedIcons = uniq([...this.icons, `${prefix}/${iconName}`]); | ||
this.config.icons = updatedIcons; | ||
return initConfig(this.config); | ||
} | ||
|
||
public async sync() { | ||
const groupedIcons = this.groupedIcons; | ||
|
||
await Promise.allSettled( | ||
groupedIcons.map(async ([prefix, icons]) => { | ||
try { | ||
const data = await generateIconCode( | ||
prefix, | ||
icons, | ||
this.config.typescript | ||
); | ||
await saveIconCode(this.config.outputPath, data.filename, data.code); | ||
} catch (error) { | ||
log.notFound(prefix); | ||
} | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.