-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.ts
More file actions
618 lines (561 loc) · 19.6 KB
/
cli.ts
File metadata and controls
618 lines (561 loc) · 19.6 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/bin/env node
import chalk from "chalk";
import { execSync } from "child_process";
import { Command } from "commander";
import fs from "fs";
import path from "path";
import { install } from "./index.js";
import { VALID_CLIENTS } from "./types.js";
import { normalizePath, recreateShadcnRegistryJson } from "./utils.js";
// Note: Native fetch is available in Node.js v18+.
// If older Node version, consider using a library like node-fetch.
const program = new Command();
export const MANIFEST_FILENAME = "21st-registry.json";
export const SHADCN_REGISTRY_FILENAME = "registry.json";
export interface ManifestEntry {
name: string; // From registry-item.json or the direct name provided
sourceUrl?: string; // The URL it was fetched from, if applicable
sourceType: "url_success" | "direct_name" | "url_fetch_failed";
registryItem?: any; // The actual fetched JSON content if sourceType is 'url_success'
fetchError?: string; // Error message if sourceType is 'url_fetch_failed'
addedByCLI: true;
}
program
.name("21st-dev-cli")
.description("Install MCP configuration for various AI clients")
.version("1.0.0");
program
.command("install")
.description("Install MCP configuration for a specific client")
.argument(
"<client>",
`The client to install for (${VALID_CLIENTS.join(", ")})`
)
.option("--api-key <key>", "API key for 21st.dev services")
.action(async (client: string, options: { apiKey?: string }) => {
if (!VALID_CLIENTS.includes(client as any)) {
console.error(
chalk.red(
`Invalid client "${client}". Available clients: ${VALID_CLIENTS.join(
", "
)}`
)
);
process.exit(1);
}
try {
await install(client as any, { apiKey: options.apiKey });
} catch (error) {
console.error(
chalk.red(
error instanceof Error ? error.message : "Unknown error occurred"
)
);
process.exit(1);
}
});
program
.command("add")
.description(
"Add a new UI component using shadcn/ui and update the registry."
)
.argument(
"<componentIdentifier>",
"Component name (e.g., button) or URL to component's registry JSON (e.g., https://21st.dev/r/...)"
)
.option("--no-install", "Prevent installation of dependencies by shadcn/ui")
.action(
async (componentIdentifier: string, options: { install?: boolean }) => {
const manifestPath = path.join(process.cwd(), MANIFEST_FILENAME);
let newEntry: ManifestEntry | null = null;
console.log(
chalk.blue(`Processing component: ${componentIdentifier}...`)
);
try {
// Check if componentIdentifier is a URL
let isUrl = false;
try {
const url = new URL(componentIdentifier);
isUrl = url.protocol === "http:" || url.protocol === "https:";
} catch (_) {
// Not a valid URL, treat as a direct name
}
if (isUrl) {
console.log(
chalk.blue(
`Fetching component details from ${componentIdentifier}...`
)
);
try {
const response = await fetch(componentIdentifier);
if (!response.ok) {
throw new Error(
`Failed to fetch: ${response.status} ${response.statusText}`
);
}
const registryItem = await response.json();
if (!registryItem.name) {
console.warn(
chalk.yellow(
"Warning: Fetched JSON does not have a 'name' property. Using identifier as name."
)
);
}
newEntry = {
name: registryItem.name || componentIdentifier,
sourceUrl: componentIdentifier,
sourceType: "url_success",
registryItem: registryItem,
addedByCLI: true,
};
console.log(
chalk.green(
`Successfully fetched details for "${newEntry.name}".`
)
);
} catch (fetchError) {
const errorMessage =
fetchError instanceof Error
? fetchError.message
: String(fetchError);
console.error(
chalk.red(
`Error fetching component details from URL: ${errorMessage}`
)
);
newEntry = {
name: componentIdentifier, // Use the URL itself as a fallback name
sourceUrl: componentIdentifier,
sourceType: "url_fetch_failed",
fetchError: errorMessage,
addedByCLI: true,
};
}
} else {
// Treat as a direct component name
newEntry = {
name: componentIdentifier,
sourceType: "direct_name",
addedByCLI: true,
};
console.log(
chalk.blue(
`Treating "${componentIdentifier}" as a direct component name.`
)
);
}
// Now, attempt to add with shadcn/ui CLI
// We pass the original componentIdentifier to shadcn
console.log(
chalk.blue(`Running shadcn add for "${componentIdentifier}"...`)
);
let shadcnCommand = `npx ${
!options.install ? "-y --no-install" : "-y"
} shadcn add --overwrite ${componentIdentifier}`;
execSync(shadcnCommand, {
stdio: "inherit",
});
console.log(
chalk.green(
`shadcn add command completed for "${componentIdentifier}".`
)
);
// Update manifest only if shadcn add was successful and we have an entry to add
if (newEntry) {
let manifest: ManifestEntry[] = [];
try {
if (fs.existsSync(manifestPath)) {
const fileContent = fs.readFileSync(manifestPath, "utf-8");
manifest = JSON.parse(fileContent);
if (!Array.isArray(manifest)) {
console.warn(
chalk.yellow(
`Warning: Manifest file ${MANIFEST_FILENAME} was malformed. Initializing a new one.`
)
);
manifest = [];
}
}
} catch (error) {
console.warn(
chalk.yellow(
`Warning: Could not read/parse ${MANIFEST_FILENAME}. Initializing. Error: ${
error instanceof Error ? error.message : String(error)
}`
)
);
manifest = [];
}
// Check for duplicates based on 'name' field of the newEntry
const isDuplicate = manifest.some(
(entry) =>
entry.name === newEntry!.name &&
entry.sourceType === newEntry!.sourceType
);
if (!isDuplicate) {
manifest.push(newEntry);
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log(
chalk.cyan(
`"${newEntry.name}" has been added/updated in ${MANIFEST_FILENAME}.`
)
);
} else {
console.log(
chalk.cyan(
`"${newEntry.name}" (type: ${newEntry.sourceType}) was already tracked in ${MANIFEST_FILENAME}.`
)
);
}
}
} catch (error) {
// This catch block now primarily handles errors from execSync or other unexpected errors
console.error(
chalk.red(
`Failed to process component "${componentIdentifier}". Error: ${
error instanceof Error ? error.message : "Unknown error"
}`
)
);
if (
error &&
typeof (error as any).status === "number" &&
(error as any).status !== 0
) {
process.exit((error as any).status);
}
process.exit(1); // General fallback exit
}
}
);
program
.command("analyze")
.description(
"Analyze a GitHub repository using Octolens analyzemyrepo service"
)
.argument(
"[repository]",
"GitHub repository in format owner/repo (e.g., facebook/react). Defaults to current repository."
)
.option("--open", "Open the analysis in browser")
.action(async (repository: string | undefined, options: { open?: boolean }) => {
const OCTOLENS_BASE_URL = "https://analyzemyrepo.com";
let repoToAnalyze = repository;
// If no repository provided, try to detect from git remote
if (!repoToAnalyze) {
try {
const gitRemote = execSync("git remote get-url origin", {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
}).trim();
// Parse GitHub URL formats:
// https://github.com/owner/repo.git
// git@github.com:owner/repo.git
const httpsMatch = gitRemote.match(/github\.com\/([^\/]+)\/([^\/\.]+)/);
const sshMatch = gitRemote.match(/github\.com:([^\/]+)\/([^\/\.]+)/);
if (httpsMatch) {
repoToAnalyze = `${httpsMatch[1]}/${httpsMatch[2]}`;
} else if (sshMatch) {
repoToAnalyze = `${sshMatch[1]}/${sshMatch[2]}`;
}
if (repoToAnalyze) {
console.log(chalk.blue(`Detected repository: ${repoToAnalyze}`));
}
} catch {
// Not in a git repo or no remote configured
}
}
if (!repoToAnalyze) {
console.error(
chalk.red(
"Could not detect repository. Please provide a repository in format owner/repo (e.g., facebook/react)"
)
);
process.exit(1);
}
// Validate repository format
if (!repoToAnalyze.match(/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+$/)) {
console.error(
chalk.red(
`Invalid repository format: "${repoToAnalyze}". Expected format: owner/repo`
)
);
process.exit(1);
}
const analysisUrl = `${OCTOLENS_BASE_URL}/analyze/${repoToAnalyze}`;
console.log(chalk.green(`\n🔍 Octolens Repository Analysis`));
console.log(chalk.cyan(`Repository: ${repoToAnalyze}`));
console.log(chalk.cyan(`Analysis URL: ${analysisUrl}\n`));
if (options.open) {
console.log(chalk.blue("Opening in browser..."));
try {
// Cross-platform open command
const openCommand =
process.platform === "darwin"
? "open"
: process.platform === "win32"
? "start"
: "xdg-open";
execSync(`${openCommand} "${analysisUrl}"`, {
stdio: "ignore",
});
console.log(chalk.green("Browser opened successfully!"));
} catch {
console.log(
chalk.yellow(
`Could not open browser automatically. Please visit the URL above.`
)
);
}
} else {
console.log(
chalk.gray("Tip: Use --open flag to open the analysis in your browser")
);
}
});
program
.command("remove")
.description(
"Remove a component and its unused dependencies (components and npm packages)"
)
.argument("<componentName>", "The name of the component to remove")
.option("--dry-run", "Show what would be deleted without actually deleting")
.option("--working-dir <dir>", "Working directory for component files", "src")
.action(
async (
componentName: string,
options: { dryRun?: boolean; workingDir?: string }
) => {
const manifestPath = path.join(process.cwd(), MANIFEST_FILENAME);
const workingDir = options.workingDir || "src";
console.log(
chalk.blue(`Preparing to remove component: ${componentName}`)
);
console.log(chalk.blue(`Working directory: ${workingDir}`));
try {
// Read the manifest
let manifest: ManifestEntry[] = [];
if (!fs.existsSync(manifestPath)) {
console.error(
chalk.red(`Manifest file ${MANIFEST_FILENAME} not found.`)
);
process.exit(1);
}
try {
const fileContent = fs.readFileSync(manifestPath, "utf-8");
manifest = JSON.parse(fileContent);
if (!Array.isArray(manifest)) {
console.error(
chalk.red(`Manifest file ${MANIFEST_FILENAME} is malformed.`)
);
process.exit(1);
}
} catch (error) {
console.error(
chalk.red(
`Could not read/parse ${MANIFEST_FILENAME}. Error: ${
error instanceof Error ? error.message : String(error)
}`
)
);
process.exit(1);
}
// Find the component to remove
const componentToRemove = manifest.find(
(entry) => entry.name === componentName
);
if (!componentToRemove) {
console.error(
chalk.red(`Component "${componentName}" not found in manifest`)
);
process.exit(1);
}
console.log(
chalk.blue(`Found component "${componentName}" in manifest`)
);
// Step 1: Count usage for all file paths across all components
const fileUsageCount = new Map<string, number>();
const componentFiles = new Map<string, string[]>();
const registryItems = new Map<string, any>();
// Helper function to collect files from registry item (including its dependencies)
const processRegistryItem = async (
url: string,
registryItem: any
): Promise<string[]> => {
if (componentFiles.has(url)) {
return componentFiles.get(url) || [];
}
let files: string[] = [];
// Collect files from the registry item
if (registryItem.files) {
registryItem.files.forEach((file: any) => {
if (file && file.path) {
files.push(normalizePath(workingDir, file.path));
}
});
}
// Collect files from registry dependencies
if (registryItem.registryDependencies) {
for (const depUrl of registryItem.registryDependencies) {
try {
if (!registryItems.has(depUrl)) {
console.log(
chalk.gray(`Fetching registry dependency: ${depUrl}`)
);
const response = await fetch(depUrl);
if (response.ok) {
const depData = await response.json();
registryItems.set(depUrl, depData);
}
} else {
console.log(
chalk.gray(`Using cached registry dependency: ${depUrl}`)
);
}
const depData = registryItems.get(depUrl);
const depFiles = await processRegistryItem(depUrl, depData);
files.push(...depFiles);
} catch (error) {
console.warn(
chalk.yellow(
`Warning: Could not fetch registry dependency ${depUrl}: ${
error instanceof Error ? error.message : String(error)
}`
)
);
}
}
}
files = [...new Set(files)];
componentFiles.set(url, files);
return files;
};
// Count usage across all components
for (const entry of manifest) {
if (entry.registryItem) {
registryItems.set(entry.name, entry.registryItem);
await processRegistryItem(entry.name, entry.registryItem);
}
}
// Count file usage after all components are processed
for (const [_, files] of componentFiles) {
files.forEach((filePath) => {
fileUsageCount.set(
filePath,
(fileUsageCount.get(filePath) || 0) + 1
);
});
}
console.log(chalk.blue(`Found dependencies:`));
for (const [name, files] of componentFiles) {
const text = `${name}:\n - ${files
.map((f) => `${f} (${fileUsageCount.get(f)} usages)`)
.join("\n - ")}`;
console.log(
name === componentToRemove.name
? chalk.yellow(text)
: chalk.gray(text)
);
}
// Step 2: Remove files that are only used by the component being deleted
const registryItemsToDelete = new Set<string>();
const markFilesToDelete = (url: string) => {
if (registryItemsToDelete.has(url)) {
return;
}
registryItemsToDelete.add(url);
console.log(chalk.gray(`${url}:`));
const files = componentFiles.get(url) || [];
files.forEach((path: any) => {
console.log(chalk.gray(` - ${path}`));
const count = fileUsageCount.get(path) || 0;
fileUsageCount.set(path, count - 1);
});
const deps = registryItems.get(url)?.registryDependencies || [];
for (const depUrl of deps) {
markFilesToDelete(depUrl);
}
};
console.log(
chalk.blue(`Marking files to delete for ${componentToRemove.name}`)
);
markFilesToDelete(componentToRemove.name);
const filesToDelete: string[] = (
componentFiles.get(componentToRemove.registryItem.name) || []
).filter((file) => {
const needToDelete = fileUsageCount.get(file) === 0;
if (!needToDelete) {
console.log(
chalk.yellow(
`Keeping file ${file} (${fileUsageCount.get(
file
)} other usages)`
)
);
}
return needToDelete;
});
// Delete the files
let deletedCount = 0;
for (const filePath of filesToDelete) {
let formattedPath = normalizePath(workingDir, filePath);
const fullPath = path.join(process.cwd(), formattedPath);
try {
if (fs.existsSync(fullPath)) {
if (options.dryRun) {
console.log(
chalk.green(`Would delete (dry run): ${formattedPath}`)
);
} else {
fs.unlinkSync(fullPath);
console.log(chalk.green(`Deleted: ${formattedPath}`));
}
deletedCount++;
} else {
console.log(chalk.gray(`File not found: ${formattedPath}`));
}
} catch (error) {
console.error(
chalk.red(
`Failed to delete ${path.join(workingDir, filePath)}: ${
error instanceof Error ? error.message : String(error)
}`
)
);
}
}
// Remove the component from manifest
if (!options.dryRun) {
const updatedManifest = manifest.filter(
(entry) => entry.name !== componentToRemove.name
);
fs.writeFileSync(
manifestPath,
JSON.stringify(updatedManifest, null, 2)
);
// Recreate shadcn registry
recreateShadcnRegistryJson();
}
console.log(
chalk.green(
`Successfully removed component "${componentName}", deleted ${deletedCount} file(s).`
)
);
console.log(
chalk.cyan(
`Updated ${MANIFEST_FILENAME} and ${SHADCN_REGISTRY_FILENAME}.`
)
);
} catch (error) {
console.error(
chalk.red(
`Failed to remove component "${componentName}". Error: ${
error instanceof Error ? error.message : "Unknown error"
}`
)
);
process.exit(1);
}
}
);
program.parse();