-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_component.py
More file actions
executable file
·302 lines (236 loc) · 8.82 KB
/
Copy pathinstall_component.py
File metadata and controls
executable file
·302 lines (236 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
"""Install Sparkle Design components automatically.
This script automates the installation of Sparkle Design components by:
1. Detecting the package manager
2. Validating configuration
3. Running the shadcn CLI to install the component
Usage:
install_component.py <component-name> [--path /path/to/project]
Examples:
install_component.py button
install_component.py card --path /path/to/project
"""
from __future__ import annotations
import argparse
import json
import subprocess
import sys
from pathlib import Path
def detect_package_manager(project_path: Path) -> str:
"""Detect package manager from lockfiles.
Args:
project_path: Path to the project directory
Returns:
Package manager name: "pnpm", "yarn", "bun", or "npm"
"""
lockfile_map = {
"pnpm-lock.yaml": "pnpm",
"yarn.lock": "yarn",
"bun.lockb": "bun",
"package-lock.json": "npm",
}
for lockfile, manager in lockfile_map.items():
if (project_path / lockfile).exists():
return manager
return "npm"
def validate_components_json(project_path: Path) -> tuple[bool, str]:
"""Validate components.json configuration.
Args:
project_path: Path to the project directory
Returns:
Tuple of (is_valid, error_message)
"""
components_json = project_path / "components.json"
if not components_json.exists():
return False, "components.json not found"
try:
with components_json.open(encoding="utf-8") as f:
config = json.load(f)
except json.JSONDecodeError as e:
return False, f"Invalid JSON in components.json: {e}"
except Exception as e:
return False, f"Error reading components.json: {e}"
if "registries" not in config:
return False, "'registries' field not found in components.json"
registries = config["registries"]
if not isinstance(registries, dict):
return False, f"'registries' should be an object, got {type(registries).__name__}"
if "@sparkle-design" not in registries:
return False, "@sparkle-design registry not configured in components.json"
registry_url = registries.get("@sparkle-design", "")
if not isinstance(registry_url, str) or not registry_url.startswith(("http://", "https://")):
return False, f"Invalid @sparkle-design registry URL: {registry_url!r}"
return True, ""
def get_component_path(project_path: Path, component_name: str) -> str:
"""Get the installation path for components from components.json.
Args:
project_path: Path to the project directory
component_name: Name of the component
Returns:
Relative path where components are installed
"""
components_json = project_path / "components.json"
default_path = "src/components/ui"
if not components_json.exists():
return f"{default_path}/{component_name}/"
try:
with components_json.open(encoding="utf-8") as f:
config = json.load(f)
# Get aliases.ui path, fallback to default
aliases = config.get("aliases", {})
ui_alias = aliases.get("ui", default_path)
# Remove @ prefix if present (e.g., @/components/ui -> components/ui)
ui_path = ui_alias.lstrip("@/")
return f"{ui_path}/{component_name}/"
except Exception:
return f"{default_path}/{component_name}/"
def get_package_manager_command(manager: str) -> list[str]:
"""Get the appropriate command for the package manager.
Args:
manager: Package manager name
Returns:
List of command parts
"""
if manager == "pnpm":
return ["pnpm", "dlx"]
elif manager == "yarn":
return ["yarn", "dlx"]
elif manager == "bun":
return ["bunx"]
else: # npm
return ["npx", "--yes"]
def install_component(
component_name: str,
project_path: Path,
package_manager: str,
) -> tuple[bool, str]:
"""Install a Sparkle Design component.
Args:
component_name: Name of the component to install
project_path: Path to the project directory
package_manager: Package manager to use
Returns:
Tuple of (success, output_message)
"""
pm_command = get_package_manager_command(package_manager)
full_command = pm_command + [
"shadcn@latest",
"add",
f"@sparkle-design/{component_name}",
]
print(f"🚀 Installing component: {component_name}")
print(f"📦 Using package manager: {package_manager}")
print(f"⚙️ Running: {' '.join(full_command)}\n")
try:
result = subprocess.run(
full_command,
cwd=project_path,
capture_output=True,
text=True,
check=True,
)
output = result.stdout + result.stderr
return True, output
except subprocess.CalledProcessError as e:
error_msg = f"Command failed with exit code {e.returncode}\n"
error_msg += f"stdout: {e.stdout}\n"
error_msg += f"stderr: {e.stderr}"
return False, error_msg
except FileNotFoundError:
error_msg = f"Command not found: {pm_command[0]}\n"
error_msg += f"Please install {package_manager} first."
return False, error_msg
except Exception as e:
return False, f"Unexpected error: {e}"
def main() -> int:
parser = argparse.ArgumentParser(
description="Install Sparkle Design components automatically",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Install button component in current directory
python install_component.py button
# Install card component in specific directory
python install_component.py card --path /path/to/project
# Install input component with custom package manager
python install_component.py input --pm yarn
""",
)
parser.add_argument(
"component",
help="Component name to install (e.g., button, card, input)",
)
parser.add_argument(
"--path",
type=Path,
default=Path.cwd(),
help="Path to project directory (default: current directory)",
)
parser.add_argument(
"--pm",
choices=["pnpm", "yarn", "bun", "npm"],
help="Package manager to use (default: auto-detect)",
)
args = parser.parse_args()
# Validate project path
if not args.path.exists():
print(f"❌ Error: Directory not found: {args.path}", file=sys.stderr)
return 1
if not args.path.is_dir():
print(f"❌ Error: Not a directory: {args.path}", file=sys.stderr)
return 1
print(f"📂 Project path: {args.path}\n")
# Detect or use specified package manager
if args.pm:
package_manager = args.pm
print(f"📦 Using specified package manager: {package_manager}\n")
else:
package_manager = detect_package_manager(args.path)
print(f"🔍 Detected package manager: {package_manager}\n")
# Validate configuration
print("🔍 Validating configuration...")
is_valid, error_msg = validate_components_json(args.path)
if not is_valid:
print(f"❌ Configuration error: {error_msg}\n", file=sys.stderr)
print("💡 Tip: Make sure components.json has the @sparkle-design registry configured:")
print(' "registries": {')
print(' "@sparkle-design": "https://sparkle-design.goodpatch.com/r/{name}.json"')
print(" }")
return 1
print("✅ Configuration is valid\n")
# Install component
success, output = install_component(
args.component,
args.path,
package_manager,
)
# Print output
if output.strip():
print("\n📄 Command output:")
print(output)
# Print result
if success:
component_path = get_component_path(args.path, args.component)
# Get import path from components.json aliases or default
components_json = args.path / "components.json"
import_alias = "@/components/ui"
try:
with components_json.open(encoding="utf-8") as f:
config = json.load(f)
import_alias = config.get("aliases", {}).get("ui", "@/components/ui")
except Exception:
pass
print(f"\n✅ Successfully installed: {args.component}")
print(f"\n📍 Component location: {component_path}")
print("\n💡 Next steps:")
print(f" 1. Import: import {{ ComponentName }} from '{import_alias}/{args.component}'")
print(" 2. Check if CSS imports are configured (first time only)")
print(" 3. Run type checking: <pm> lint")
print(" 4. View in Storybook: <pm> storybook")
return 0
else:
print(f"\n❌ Failed to install: {args.component}")
print(f"\n📄 Error details:\n{output}")
return 1
if __name__ == "__main__":
raise SystemExit(main())