@foundrykit/cli 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +187 -219
- package/dist/cli.js +1023 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.js +768 -506
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
- package/src/commands/add.ts +230 -265
- package/src/commands/init.ts +404 -108
- package/src/commands/list.ts +113 -198
- package/src/index.ts +11 -1
- package/src/lib/registry.ts +293 -0
- package/tsup.config.ts +7 -2
- package/.turbo/turbo-build.log +0 -18
- package/.turbo/turbo-lint.log +0 -5
- package/.turbo/turbo-type-check.log +0 -5
package/src/commands/list.ts
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List command for FoundryKit CLI
|
|
3
|
+
* Displays available components from the registry
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { Command } from 'commander';
|
|
2
7
|
import chalk from 'chalk';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
interface Component {
|
|
6
|
-
name: string;
|
|
7
|
-
description: string;
|
|
8
|
-
category: 'primitives' | 'components' | 'blocks';
|
|
9
|
-
dependencies?: string[];
|
|
10
|
-
tags?: string[];
|
|
11
|
-
}
|
|
8
|
+
import type { ComponentMeta, ComponentCategory } from '@foundrykit/types';
|
|
9
|
+
import { getRegistry } from '../lib/registry.js';
|
|
12
10
|
|
|
13
|
-
interface
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
blocks: Component[];
|
|
11
|
+
interface ListOptions {
|
|
12
|
+
category?: string;
|
|
13
|
+
search?: string;
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
export function listCommand() {
|
|
@@ -21,235 +18,153 @@ export function listCommand() {
|
|
|
21
18
|
.description('List all available FoundryKit components and blocks')
|
|
22
19
|
.option(
|
|
23
20
|
'-c, --category <category>',
|
|
24
|
-
'filter by category (primitives, components, blocks)'
|
|
21
|
+
'filter by category (primitives, components, blocks, animations)'
|
|
25
22
|
)
|
|
26
23
|
.option('-s, --search <term>', 'search components by name or description')
|
|
27
|
-
.action(async (options:
|
|
24
|
+
.action(async (options: ListOptions) => {
|
|
28
25
|
try {
|
|
29
26
|
console.log(chalk.blue('📦 Available FoundryKit components:'));
|
|
30
27
|
|
|
31
|
-
const registry =
|
|
28
|
+
const registry = getRegistry();
|
|
32
29
|
|
|
33
|
-
//
|
|
34
|
-
let categories: ('primitives' | 'components' | 'blocks')[] = [
|
|
35
|
-
'primitives',
|
|
36
|
-
'components',
|
|
37
|
-
'blocks',
|
|
38
|
-
];
|
|
30
|
+
// Validate category if provided
|
|
39
31
|
if (options.category) {
|
|
32
|
+
const validCategories: ComponentCategory[] = [
|
|
33
|
+
'primitives',
|
|
34
|
+
'components',
|
|
35
|
+
'blocks',
|
|
36
|
+
'animations',
|
|
37
|
+
];
|
|
40
38
|
if (
|
|
41
|
-
!
|
|
39
|
+
!validCategories.includes(options.category as ComponentCategory)
|
|
42
40
|
) {
|
|
43
41
|
console.error(chalk.red(`Invalid category: ${options.category}`));
|
|
44
42
|
console.log(
|
|
45
|
-
chalk.gray(
|
|
43
|
+
chalk.gray(`Valid categories: ${validCategories.join(', ')}`)
|
|
46
44
|
);
|
|
47
|
-
|
|
45
|
+
process.exit(1);
|
|
48
46
|
}
|
|
49
|
-
categories = [
|
|
50
|
-
options.category as 'primitives' | 'components' | 'blocks',
|
|
51
|
-
];
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
console.log(`\n${chalk.cyan.bold(category.toUpperCase())}`);
|
|
72
|
-
console.log(chalk.gray('─'.repeat(40)));
|
|
73
|
-
|
|
74
|
-
for (const item of filteredItems) {
|
|
75
|
-
console.log(`${chalk.green('●')} ${chalk.bold(item.name)}`);
|
|
76
|
-
console.log(` ${chalk.gray(item.description)}`);
|
|
77
|
-
if (item.tags && item.tags.length > 0) {
|
|
78
|
-
console.log(
|
|
79
|
-
` ${chalk.blue(item.tags.map(tag => `#${tag}`).join(' '))}`
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
console.log();
|
|
49
|
+
// Fetch components from registry
|
|
50
|
+
console.log(chalk.gray('Fetching from registry...'));
|
|
51
|
+
const components = await registry.getComponents(
|
|
52
|
+
options.category,
|
|
53
|
+
options.search
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
if (components.length === 0) {
|
|
57
|
+
console.log(
|
|
58
|
+
chalk.yellow('\nNo components found matching your criteria.')
|
|
59
|
+
);
|
|
60
|
+
if (options.search) {
|
|
61
|
+
console.log(chalk.gray(`Search term: "${options.search}"`));
|
|
62
|
+
}
|
|
63
|
+
if (options.category) {
|
|
64
|
+
console.log(chalk.gray(`Category: ${options.category}`));
|
|
83
65
|
}
|
|
66
|
+
return;
|
|
84
67
|
}
|
|
85
68
|
|
|
69
|
+
// Group and display components
|
|
70
|
+
displayComponents(components);
|
|
71
|
+
|
|
72
|
+
// Show usage hints
|
|
86
73
|
console.log(chalk.cyan('\n💡 Usage:'));
|
|
87
|
-
console.log(
|
|
88
|
-
|
|
74
|
+
console.log(
|
|
75
|
+
chalk.gray(' foundrykit add # Interactive mode')
|
|
76
|
+
);
|
|
77
|
+
console.log(
|
|
78
|
+
chalk.gray(' foundrykit add <id> # Add specific component')
|
|
79
|
+
);
|
|
80
|
+
console.log(chalk.gray(' foundrykit add button # Example'));
|
|
89
81
|
} catch (error) {
|
|
90
82
|
console.error(chalk.red('❌ Failed to fetch component registry:'));
|
|
91
83
|
console.error(
|
|
92
84
|
chalk.red(error instanceof Error ? error.message : String(error))
|
|
93
85
|
);
|
|
94
|
-
|
|
95
|
-
// Fallback to local registry
|
|
96
|
-
console.log(chalk.yellow('\n📋 Using local registry:'));
|
|
97
|
-
displayLocalRegistry(options);
|
|
86
|
+
process.exit(1);
|
|
98
87
|
}
|
|
99
88
|
});
|
|
100
89
|
}
|
|
101
90
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
primitives: [
|
|
121
|
-
{
|
|
122
|
-
name: 'button',
|
|
123
|
-
description:
|
|
124
|
-
'Displays a button or a component that looks like a button.',
|
|
125
|
-
category: 'primitives',
|
|
126
|
-
tags: ['form', 'interactive'],
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
name: 'input',
|
|
130
|
-
description:
|
|
131
|
-
'Displays a form input field or a component that looks like an input.',
|
|
132
|
-
category: 'primitives',
|
|
133
|
-
tags: ['form'],
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
name: 'badge',
|
|
137
|
-
description: 'Displays a badge or a component that looks like a badge.',
|
|
138
|
-
category: 'primitives',
|
|
139
|
-
tags: ['status', 'label'],
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
name: 'card',
|
|
143
|
-
description: 'Displays a card with header, content, and footer.',
|
|
144
|
-
category: 'primitives',
|
|
145
|
-
tags: ['layout', 'container'],
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
name: 'avatar',
|
|
149
|
-
description:
|
|
150
|
-
'An image element with a fallback for representing the user.',
|
|
151
|
-
category: 'primitives',
|
|
152
|
-
tags: ['user', 'profile'],
|
|
153
|
-
},
|
|
154
|
-
],
|
|
155
|
-
components: [
|
|
156
|
-
{
|
|
157
|
-
name: 'search-bar',
|
|
158
|
-
description: 'A search input with suggestions and keyboard navigation.',
|
|
159
|
-
category: 'components',
|
|
160
|
-
dependencies: ['input', 'popover'],
|
|
161
|
-
tags: ['search', 'form'],
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
name: 'combobox',
|
|
165
|
-
description:
|
|
166
|
-
'Combines a text input with a listbox, allowing users to filter a list.',
|
|
167
|
-
category: 'components',
|
|
168
|
-
dependencies: ['input', 'popover', 'command'],
|
|
169
|
-
tags: ['form', 'selection'],
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
name: 'date-picker',
|
|
173
|
-
description: 'A date picker component with calendar popup.',
|
|
174
|
-
category: 'components',
|
|
175
|
-
dependencies: ['input', 'popover', 'calendar'],
|
|
176
|
-
tags: ['form', 'date'],
|
|
177
|
-
},
|
|
178
|
-
],
|
|
179
|
-
blocks: [
|
|
180
|
-
{
|
|
181
|
-
name: 'hero-minimal',
|
|
182
|
-
description:
|
|
183
|
-
'A minimal hero section with heading, description, and CTA button.',
|
|
184
|
-
category: 'blocks',
|
|
185
|
-
dependencies: ['button'],
|
|
186
|
-
tags: ['landing', 'marketing'],
|
|
187
|
-
},
|
|
188
|
-
{
|
|
189
|
-
name: 'hero-with-image',
|
|
190
|
-
description:
|
|
191
|
-
'A hero section with background image and content overlay.',
|
|
192
|
-
category: 'blocks',
|
|
193
|
-
dependencies: ['button'],
|
|
194
|
-
tags: ['landing', 'marketing', 'image'],
|
|
195
|
-
},
|
|
196
|
-
{
|
|
197
|
-
name: 'pricing',
|
|
198
|
-
description:
|
|
199
|
-
'A pricing section with multiple tiers and feature comparison.',
|
|
200
|
-
category: 'blocks',
|
|
201
|
-
dependencies: ['card', 'button', 'badge'],
|
|
202
|
-
tags: ['pricing', 'marketing'],
|
|
203
|
-
},
|
|
204
|
-
{
|
|
205
|
-
name: 'testimonials',
|
|
206
|
-
description: 'A testimonials section with customer quotes and avatars.',
|
|
207
|
-
category: 'blocks',
|
|
208
|
-
dependencies: ['avatar', 'card'],
|
|
209
|
-
tags: ['social-proof', 'marketing'],
|
|
210
|
-
},
|
|
211
|
-
],
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function displayLocalRegistry(options: { category?: string; search?: string }) {
|
|
216
|
-
const registry = getLocalRegistry();
|
|
217
|
-
|
|
218
|
-
let categories: ('primitives' | 'components' | 'blocks')[] = [
|
|
91
|
+
/**
|
|
92
|
+
* Display components grouped by category
|
|
93
|
+
*/
|
|
94
|
+
function displayComponents(components: ComponentMeta[]): void {
|
|
95
|
+
// Group components by category
|
|
96
|
+
const grouped = components.reduce(
|
|
97
|
+
(acc, component) => {
|
|
98
|
+
if (!acc[component.category]) {
|
|
99
|
+
acc[component.category] = [];
|
|
100
|
+
}
|
|
101
|
+
acc[component.category].push(component);
|
|
102
|
+
return acc;
|
|
103
|
+
},
|
|
104
|
+
{} as Record<ComponentCategory, ComponentMeta[]>
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Display in category order
|
|
108
|
+
const categoryOrder: ComponentCategory[] = [
|
|
219
109
|
'primitives',
|
|
220
110
|
'components',
|
|
221
111
|
'blocks',
|
|
112
|
+
'animations',
|
|
222
113
|
];
|
|
223
|
-
if (options.category) {
|
|
224
|
-
categories = [options.category as 'primitives' | 'components' | 'blocks'];
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
for (const category of categories) {
|
|
228
|
-
const items = registry[category];
|
|
229
|
-
const filteredItems = options.search
|
|
230
|
-
? items.filter(
|
|
231
|
-
item =>
|
|
232
|
-
item.name.toLowerCase().includes(options.search!.toLowerCase()) ||
|
|
233
|
-
item.description
|
|
234
|
-
.toLowerCase()
|
|
235
|
-
.includes(options.search!.toLowerCase())
|
|
236
|
-
)
|
|
237
|
-
: items;
|
|
238
114
|
|
|
239
|
-
|
|
115
|
+
for (const category of categoryOrder) {
|
|
116
|
+
const items = grouped[category];
|
|
117
|
+
if (!items || items.length === 0) continue;
|
|
240
118
|
|
|
241
119
|
console.log(`\n${chalk.cyan.bold(category.toUpperCase())}`);
|
|
242
120
|
console.log(chalk.gray('─'.repeat(40)));
|
|
243
121
|
|
|
244
|
-
for (const item of
|
|
245
|
-
|
|
122
|
+
for (const item of items) {
|
|
123
|
+
// Component name and ID
|
|
124
|
+
console.log(
|
|
125
|
+
`${chalk.green('●')} ${chalk.bold(item.name)} ${chalk.gray(
|
|
126
|
+
`(${item.id})`
|
|
127
|
+
)}`
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// Description
|
|
246
131
|
console.log(` ${chalk.gray(item.description)}`);
|
|
132
|
+
|
|
133
|
+
// Tags
|
|
247
134
|
if (item.tags && item.tags.length > 0) {
|
|
248
135
|
console.log(
|
|
249
136
|
` ${chalk.blue(item.tags.map(tag => `#${tag}`).join(' '))}`
|
|
250
137
|
);
|
|
251
138
|
}
|
|
139
|
+
|
|
140
|
+
// Dependencies
|
|
141
|
+
if (item.registryDependencies && item.registryDependencies.length > 0) {
|
|
142
|
+
console.log(
|
|
143
|
+
` ${chalk.yellow('Requires:')} ${item.registryDependencies.join(
|
|
144
|
+
', '
|
|
145
|
+
)}`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Package dependencies
|
|
150
|
+
if (item.packageDependencies && item.packageDependencies.length > 0) {
|
|
151
|
+
console.log(
|
|
152
|
+
` ${chalk.magenta('NPM packages:')} ${item.packageDependencies.join(
|
|
153
|
+
', '
|
|
154
|
+
)}`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
252
158
|
console.log();
|
|
253
159
|
}
|
|
254
160
|
}
|
|
161
|
+
|
|
162
|
+
// Show summary
|
|
163
|
+
const total = components.length;
|
|
164
|
+
const byCategory = Object.entries(grouped)
|
|
165
|
+
.map(([cat, items]) => `${items.length} ${cat}`)
|
|
166
|
+
.join(', ');
|
|
167
|
+
|
|
168
|
+
console.log(chalk.gray('─'.repeat(40)));
|
|
169
|
+
console.log(chalk.gray(`Total: ${total} components (${byCategory})`));
|
|
255
170
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { Command } from 'commander';
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { dirname, join } from 'path';
|
|
4
7
|
import { initCommand } from './commands/init.js';
|
|
5
8
|
import { listCommand } from './commands/list.js';
|
|
6
9
|
import { addCommand } from './commands/add.js';
|
|
7
10
|
import { updateCommand } from './commands/update.js';
|
|
8
11
|
import { doctorCommand } from './commands/doctor.js';
|
|
9
12
|
|
|
13
|
+
// Get package.json version
|
|
14
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
+
const __dirname = dirname(__filename);
|
|
16
|
+
const packageJson = JSON.parse(
|
|
17
|
+
readFileSync(join(__dirname, '..', 'package.json'), 'utf-8')
|
|
18
|
+
);
|
|
19
|
+
|
|
10
20
|
const program = new Command();
|
|
11
21
|
|
|
12
22
|
program
|
|
13
23
|
.name('foundrykit')
|
|
14
24
|
.description('FoundryKit CLI - Component library management tool')
|
|
15
|
-
.version(
|
|
25
|
+
.version(packageJson.version);
|
|
16
26
|
|
|
17
27
|
// Add commands
|
|
18
28
|
program.addCommand(initCommand());
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registry service for fetching and caching component data
|
|
3
|
+
* Handles communication with the FoundryKit registry
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import os from 'os';
|
|
9
|
+
import chalk from 'chalk';
|
|
10
|
+
import type {
|
|
11
|
+
ComponentMeta,
|
|
12
|
+
ComponentData,
|
|
13
|
+
RegistryIndex,
|
|
14
|
+
RegistryComponent,
|
|
15
|
+
RegistryError,
|
|
16
|
+
LocalCache,
|
|
17
|
+
CLIConfig,
|
|
18
|
+
} from '@foundrykit/types';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Default configuration
|
|
22
|
+
*/
|
|
23
|
+
const DEFAULT_CONFIG: CLIConfig = {
|
|
24
|
+
registryUrl:
|
|
25
|
+
process.env.FOUNDRYKIT_REGISTRY_URL || 'https://foundry.umiflow.com',
|
|
26
|
+
cacheDir: path.join(os.homedir(), '.foundrykit', 'cache'),
|
|
27
|
+
cacheMaxAge: 1000 * 60 * 60 * 24, // 24 hours
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Registry service class
|
|
32
|
+
*/
|
|
33
|
+
export class RegistryService {
|
|
34
|
+
private config: CLIConfig;
|
|
35
|
+
private cacheFile: string;
|
|
36
|
+
|
|
37
|
+
constructor(config?: Partial<CLIConfig>) {
|
|
38
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
39
|
+
this.cacheFile = path.join(this.config.cacheDir, 'registry.json');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Fetch all components from the registry
|
|
44
|
+
*/
|
|
45
|
+
async getComponents(
|
|
46
|
+
category?: string,
|
|
47
|
+
search?: string
|
|
48
|
+
): Promise<RegistryIndex> {
|
|
49
|
+
try {
|
|
50
|
+
// Build query params
|
|
51
|
+
const params = new URLSearchParams();
|
|
52
|
+
if (category) params.append('category', category);
|
|
53
|
+
if (search) params.append('search', search);
|
|
54
|
+
|
|
55
|
+
const url = `${this.config.registryUrl}/registry${
|
|
56
|
+
params.toString() ? '?' + params.toString() : ''
|
|
57
|
+
}`;
|
|
58
|
+
|
|
59
|
+
console.log(chalk.gray(`Fetching from: ${url}`));
|
|
60
|
+
const response = await fetch(url);
|
|
61
|
+
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Registry returned ${response.status}: ${response.statusText}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const components = (await response.json()) as RegistryIndex;
|
|
69
|
+
|
|
70
|
+
// Update cache
|
|
71
|
+
await this.updateCache(components);
|
|
72
|
+
|
|
73
|
+
return components;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// Try to load from cache
|
|
76
|
+
const cache = await this.loadCache();
|
|
77
|
+
if (cache && this.isCacheValid(cache)) {
|
|
78
|
+
console.log(chalk.yellow('Using cached registry data'));
|
|
79
|
+
return this.filterComponents(cache.components, category, search);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// No valid cache, throw error
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Failed to connect to registry: ${error instanceof Error ? error.message : 'Unknown error'}\n` +
|
|
85
|
+
`Registry URL: ${this.config.registryUrl}\n` +
|
|
86
|
+
`Try again later or check your internet connection.`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Fetch a specific component by ID
|
|
93
|
+
*/
|
|
94
|
+
async getComponent(id: string): Promise<ComponentData> {
|
|
95
|
+
try {
|
|
96
|
+
const url = `${this.config.registryUrl}/registry/${id}`;
|
|
97
|
+
console.log(chalk.gray(`Fetching component: ${url}`));
|
|
98
|
+
|
|
99
|
+
const response = await fetch(url);
|
|
100
|
+
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
if (response.status === 404) {
|
|
103
|
+
throw new Error(`Component "${id}" not found in registry`);
|
|
104
|
+
}
|
|
105
|
+
throw new Error(
|
|
106
|
+
`Registry returned ${response.status}: ${response.statusText}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const component = (await response.json()) as RegistryComponent;
|
|
111
|
+
|
|
112
|
+
// Cache the component data
|
|
113
|
+
await this.cacheComponent(component);
|
|
114
|
+
|
|
115
|
+
return component;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
// Try to load from cache
|
|
118
|
+
const cache = await this.loadCache();
|
|
119
|
+
if (cache?.componentData?.[id]) {
|
|
120
|
+
console.log(chalk.yellow(`Using cached data for component: ${id}`));
|
|
121
|
+
return cache.componentData[id];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// No cached data, throw error
|
|
125
|
+
throw new Error(
|
|
126
|
+
`Failed to fetch component "${id}": ${error instanceof Error ? error.message : 'Unknown error'}\n` +
|
|
127
|
+
`Registry URL: ${this.config.registryUrl}\n` +
|
|
128
|
+
`Try again later or check your internet connection.`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Update the local cache with new component list
|
|
135
|
+
*/
|
|
136
|
+
private async updateCache(components: RegistryIndex): Promise<void> {
|
|
137
|
+
try {
|
|
138
|
+
const cache = await this.loadCache();
|
|
139
|
+
const newCache: LocalCache = {
|
|
140
|
+
version: '1.0.0',
|
|
141
|
+
lastUpdated: new Date().toISOString(),
|
|
142
|
+
components,
|
|
143
|
+
componentData: cache?.componentData || {},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
await fs.ensureDir(this.config.cacheDir);
|
|
147
|
+
await fs.writeJson(this.cacheFile, newCache, { spaces: 2 });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
// Cache update failed, but don't throw - this is not critical
|
|
150
|
+
console.log(chalk.gray('Failed to update cache'));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Cache a specific component
|
|
156
|
+
*/
|
|
157
|
+
private async cacheComponent(component: ComponentData): Promise<void> {
|
|
158
|
+
try {
|
|
159
|
+
const cache = await this.loadCache();
|
|
160
|
+
if (!cache) return;
|
|
161
|
+
|
|
162
|
+
cache.componentData[component.id] = component;
|
|
163
|
+
cache.lastUpdated = new Date().toISOString();
|
|
164
|
+
|
|
165
|
+
await fs.writeJson(this.cacheFile, cache, { spaces: 2 });
|
|
166
|
+
} catch (error) {
|
|
167
|
+
// Cache update failed, but don't throw
|
|
168
|
+
console.log(chalk.gray('Failed to cache component'));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Load the local cache
|
|
174
|
+
*/
|
|
175
|
+
private async loadCache(): Promise<LocalCache | null> {
|
|
176
|
+
try {
|
|
177
|
+
if (await fs.pathExists(this.cacheFile)) {
|
|
178
|
+
return await fs.readJson(this.cacheFile);
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.log(chalk.gray('Failed to load cache'));
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Check if cache is still valid
|
|
188
|
+
*/
|
|
189
|
+
private isCacheValid(cache: LocalCache): boolean {
|
|
190
|
+
const lastUpdated = new Date(cache.lastUpdated).getTime();
|
|
191
|
+
const now = Date.now();
|
|
192
|
+
return now - lastUpdated < this.config.cacheMaxAge;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Filter components by category and search
|
|
197
|
+
*/
|
|
198
|
+
private filterComponents(
|
|
199
|
+
components: ComponentMeta[],
|
|
200
|
+
category?: string,
|
|
201
|
+
search?: string
|
|
202
|
+
): ComponentMeta[] {
|
|
203
|
+
let filtered = components;
|
|
204
|
+
|
|
205
|
+
if (category) {
|
|
206
|
+
filtered = filtered.filter(c => c.category === category);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (search) {
|
|
210
|
+
const searchLower = search.toLowerCase();
|
|
211
|
+
filtered = filtered.filter(
|
|
212
|
+
c =>
|
|
213
|
+
c.name.toLowerCase().includes(searchLower) ||
|
|
214
|
+
c.description.toLowerCase().includes(searchLower) ||
|
|
215
|
+
c.id.toLowerCase().includes(searchLower) ||
|
|
216
|
+
c.tags?.some(tag => tag.toLowerCase().includes(searchLower))
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return filtered;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Clear the local cache
|
|
225
|
+
*/
|
|
226
|
+
async clearCache(): Promise<void> {
|
|
227
|
+
try {
|
|
228
|
+
if (await fs.pathExists(this.cacheFile)) {
|
|
229
|
+
await fs.remove(this.cacheFile);
|
|
230
|
+
console.log(chalk.green('Cache cleared successfully'));
|
|
231
|
+
}
|
|
232
|
+
} catch (error) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
`Failed to clear cache: ${error instanceof Error ? error.message : 'Unknown error'}`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Get cache statistics
|
|
241
|
+
*/
|
|
242
|
+
async getCacheStats(): Promise<{
|
|
243
|
+
exists: boolean;
|
|
244
|
+
size: number;
|
|
245
|
+
lastUpdated: string | null;
|
|
246
|
+
componentCount: number;
|
|
247
|
+
}> {
|
|
248
|
+
try {
|
|
249
|
+
if (await fs.pathExists(this.cacheFile)) {
|
|
250
|
+
const cache = await this.loadCache();
|
|
251
|
+
const stats = await fs.stat(this.cacheFile);
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
exists: true,
|
|
255
|
+
size: stats.size,
|
|
256
|
+
lastUpdated: cache?.lastUpdated || null,
|
|
257
|
+
componentCount: cache?.components.length || 0,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
} catch (error) {
|
|
261
|
+
// Ignore errors
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
exists: false,
|
|
266
|
+
size: 0,
|
|
267
|
+
lastUpdated: null,
|
|
268
|
+
componentCount: 0,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Singleton instance
|
|
275
|
+
*/
|
|
276
|
+
let registryInstance: RegistryService | null = null;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Get the registry service instance
|
|
280
|
+
*/
|
|
281
|
+
export function getRegistry(config?: Partial<CLIConfig>): RegistryService {
|
|
282
|
+
if (!registryInstance) {
|
|
283
|
+
registryInstance = new RegistryService(config);
|
|
284
|
+
}
|
|
285
|
+
return registryInstance;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Reset the registry instance (mainly for testing)
|
|
290
|
+
*/
|
|
291
|
+
export function resetRegistry(): void {
|
|
292
|
+
registryInstance = null;
|
|
293
|
+
}
|