-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shadcn): implement icons transformer
- Loading branch information
Showing
14 changed files
with
145 additions
and
27 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
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,20 @@ | ||
import { getConfig } from "@/src/utils/get-config" | ||
import { getProjectInfo } from "@/src/utils/get-project-info" | ||
import { logger } from "@/src/utils/logger" | ||
import { Command } from "commander" | ||
|
||
export const info = new Command() | ||
.name("info") | ||
.description("get information about your project") | ||
.option( | ||
"-c, --cwd <cwd>", | ||
"the working directory. defaults to the current directory.", | ||
process.cwd() | ||
) | ||
.action(async (opts) => { | ||
logger.info("> project info") | ||
console.log(await getProjectInfo(opts.cwd)) | ||
logger.break() | ||
logger.info("> components.json") | ||
console.log(await getConfig(opts.cwd)) | ||
}) |
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
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,4 @@ | ||
export const ICON_LIBRARIES = { | ||
lucide: "lucide-react", | ||
radix: "@radix-ui/react-icons", | ||
} |
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,70 @@ | ||
import { ICON_LIBRARIES } from "@/src/utils/icon-libraries" | ||
import { getRegistryIcons } from "@/src/utils/registry" | ||
import { Transformer } from "@/src/utils/transformers" | ||
import { SyntaxKind } from "ts-morph" | ||
|
||
// Lucide is the default icon library in the registry. | ||
const SOURCE_LIBRARY = "lucide" | ||
|
||
export const transformIcons: Transformer = async ({ sourceFile, config }) => { | ||
// No transform if we cannot read the icon library. | ||
if (!config.iconLibrary || !(config.iconLibrary in ICON_LIBRARIES)) { | ||
return sourceFile | ||
} | ||
|
||
const registryIcons = await getRegistryIcons() | ||
const sourceLibrary = SOURCE_LIBRARY | ||
const targetLibrary = config.iconLibrary | ||
|
||
if (sourceLibrary === targetLibrary) { | ||
return sourceFile | ||
} | ||
|
||
let targetedIcons: string[] = [] | ||
for (const importDeclaration of sourceFile.getImportDeclarations() ?? []) { | ||
if ( | ||
importDeclaration.getModuleSpecifier()?.getText() !== | ||
`"${ICON_LIBRARIES[SOURCE_LIBRARY]}"` | ||
) { | ||
continue | ||
} | ||
|
||
for (const specifier of importDeclaration.getNamedImports() ?? []) { | ||
const iconName = specifier.getName() | ||
|
||
const targetedIcon = registryIcons[iconName][targetLibrary] | ||
|
||
if (!targetedIcon) { | ||
continue | ||
} | ||
|
||
targetedIcons.push(targetedIcon) | ||
|
||
// Remove the named import. | ||
specifier.remove() | ||
|
||
// Replace with the targeted icon. | ||
sourceFile | ||
.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement) | ||
.filter((node) => node.getTagNameNode()?.getText() === iconName) | ||
.forEach((node) => node.getTagNameNode()?.replaceWithText(targetedIcon)) | ||
} | ||
|
||
// If the named import is empty, remove the import declaration. | ||
if (importDeclaration.getNamedImports()?.length === 0) { | ||
importDeclaration.remove() | ||
} | ||
} | ||
|
||
if (targetedIcons.length > 0) { | ||
sourceFile.addImportDeclaration({ | ||
moduleSpecifier: | ||
ICON_LIBRARIES[targetLibrary as keyof typeof ICON_LIBRARIES], | ||
namedImports: targetedIcons.map((icon) => ({ | ||
name: icon, | ||
})), | ||
}) | ||
} | ||
|
||
return sourceFile | ||
} |
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 |
---|---|---|
|
@@ -14,5 +14,6 @@ | |
"lib": "~/lib", | ||
"hooks": "~/lib/hooks", | ||
"ui": "~/ui" | ||
} | ||
}, | ||
"iconLibrary": "lucide" | ||
} |
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 |
---|---|---|
|
@@ -10,5 +10,6 @@ | |
"aliases": { | ||
"utils": "@/lib/utils", | ||
"components": "@/components" | ||
} | ||
}, | ||
"iconLibrary": "radix" | ||
} |
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