fix(ui-styling): find installed shadcn components under src/ and custom aliases (#602)

get_installed_components() only stripped a literal "@/" from aliases.components and ignored aliases.ui, so projects with a src/ directory, a "~/" alias, or a custom ui alias reported no installed components and the already-installed guard in add_components() never triggered.
This commit is contained in:
Jay Pokale 2026-11-26 00:34:31 +05:30 committed by GitHub
parent 8db3633881
commit bde080f4c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 76 additions and 10 deletions

View File

@ -52,18 +52,32 @@ class ShadcnInstaller:
with open(self.components_json, encoding="utf-8") as f:
config = json.load(f)
components_dir = self.project_root / config.get("aliases", {}).get(
"components", "components"
).replace("@/", "")
ui_dir = components_dir / "ui"
aliases = config.get("aliases", {})
ui_alias = aliases.get("ui") or (
aliases.get("components", "components").rstrip("/") + "/ui"
)
ui_dir = self._resolve_alias_dir(ui_alias)
if not ui_dir.exists():
if ui_dir is None:
return []
return [f.stem for f in ui_dir.glob("*.tsx") if f.is_file()]
except (json.JSONDecodeError, KeyError, OSError):
return []
def _resolve_alias_dir(self, alias: str) -> Optional[Path]:
"""Map a components.json import alias (e.g. "@/components/ui") to a directory.
The "@/" and "~/" prefixes point at the project root, or at src/ when
the project uses a src directory (the shadcn default for Next.js).
"""
relative = alias[2:] if alias[:2] in ("@/", "~/") else alias
for base in (self.project_root, self.project_root / "src"):
candidate = base / relative
if candidate.is_dir():
return candidate
return None
def _get_shadcn_version(self) -> str:
"""Read shadcn version from project package.json; fall back to a pinned default."""
pkg_json = self.project_root / "package.json"

View File

@ -86,6 +86,25 @@ class TestShadcnInstaller:
assert sorted(installed) == ["button", "card"]
@pytest.mark.parametrize(
"aliases, ui_path",
[
({"components": "@/components", "ui": "@/components/ui"}, "src/components/ui"),
({"components": "~/components"}, "components/ui"),
({"components": "@/components", "ui": "@/components/shadcn"}, "components/shadcn"),
],
)
def test_get_installed_components_resolves_aliases(self, tmp_path, aliases, ui_path):
"""Installed components are found under src/, ~/ and custom ui aliases."""
(tmp_path / "components.json").write_text(json.dumps({"aliases": aliases}))
ui_dir = tmp_path / ui_path
ui_dir.mkdir(parents=True)
(ui_dir / "button.tsx").write_text("export const Button = () => {}")
installer = ShadcnInstaller(project_root=tmp_path)
assert installer.get_installed_components() == ["button"]
def test_get_installed_components_no_config(self, tmp_path):
"""Test getting installed components without config."""
installer = ShadcnInstaller(project_root=tmp_path)

View File

@ -52,18 +52,32 @@ class ShadcnInstaller:
with open(self.components_json, encoding="utf-8") as f:
config = json.load(f)
components_dir = self.project_root / config.get("aliases", {}).get(
"components", "components"
).replace("@/", "")
ui_dir = components_dir / "ui"
aliases = config.get("aliases", {})
ui_alias = aliases.get("ui") or (
aliases.get("components", "components").rstrip("/") + "/ui"
)
ui_dir = self._resolve_alias_dir(ui_alias)
if not ui_dir.exists():
if ui_dir is None:
return []
return [f.stem for f in ui_dir.glob("*.tsx") if f.is_file()]
except (json.JSONDecodeError, KeyError, OSError):
return []
def _resolve_alias_dir(self, alias: str) -> Optional[Path]:
"""Map a components.json import alias (e.g. "@/components/ui") to a directory.
The "@/" and "~/" prefixes point at the project root, or at src/ when
the project uses a src directory (the shadcn default for Next.js).
"""
relative = alias[2:] if alias[:2] in ("@/", "~/") else alias
for base in (self.project_root, self.project_root / "src"):
candidate = base / relative
if candidate.is_dir():
return candidate
return None
def _get_shadcn_version(self) -> str:
"""Read shadcn version from project package.json; fall back to a pinned default."""
pkg_json = self.project_root / "package.json"

View File

@ -86,6 +86,25 @@ class TestShadcnInstaller:
assert sorted(installed) == ["button", "card"]
@pytest.mark.parametrize(
"aliases, ui_path",
[
({"components": "@/components", "ui": "@/components/ui"}, "src/components/ui"),
({"components": "~/components"}, "components/ui"),
({"components": "@/components", "ui": "@/components/shadcn"}, "components/shadcn"),
],
)
def test_get_installed_components_resolves_aliases(self, tmp_path, aliases, ui_path):
"""Installed components are found under src/, ~/ and custom ui aliases."""
(tmp_path / "components.json").write_text(json.dumps({"aliases": aliases}))
ui_dir = tmp_path / ui_path
ui_dir.mkdir(parents=True)
(ui_dir / "button.tsx").write_text("export const Button = () => {}")
installer = ShadcnInstaller(project_root=tmp_path)
assert installer.get_installed_components() == ["button"]
def test_get_installed_components_no_config(self, tmp_path):
"""Test getting installed components without config."""
installer = ShadcnInstaller(project_root=tmp_path)