@foundrykit/cli 2.0.11 → 3.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/README.md +97 -30
- package/dist/cli.js +3485 -2030
- package/dist/cli.js.map +1 -1
- package/dist/index.js +3485 -2030
- package/dist/index.js.map +1 -1
- package/package.json +15 -8
- package/schema.json +60 -0
- package/.turbo/turbo-build.log +0 -19
- package/.turbo/turbo-test.log +0 -42
- package/.turbo/turbo-type-check.log +0 -4
- package/CHANGELOG.md +0 -245
- package/docs/DEPENDENCY_RESOLUTION.md +0 -201
- package/eslint.config.js +0 -4
- package/postcss.config.mjs +0 -6
- package/src/commands/add.ts +0 -1065
- package/src/commands/doctor.ts +0 -127
- package/src/commands/init.ts +0 -935
- package/src/commands/list.ts +0 -170
- package/src/commands/update.ts +0 -323
- package/src/index.ts +0 -34
- package/src/lib/constants.ts +0 -42
- package/src/lib/registry.ts +0 -624
- package/tests/dependency-resolution.test.ts +0 -570
- package/tsconfig.json +0 -11
- package/tsconfig.lint.json +0 -8
- package/tsup.config.ts +0 -16
package/src/lib/registry.ts
DELETED
|
@@ -1,624 +0,0 @@
|
|
|
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 {
|
|
11
|
-
DEFAULT_REGISTRY_URL,
|
|
12
|
-
CACHE_DIR,
|
|
13
|
-
CACHE_TTL,
|
|
14
|
-
FOUNDRYKIT_PACKAGE_ALIASES,
|
|
15
|
-
NPM_REGISTRY_URL,
|
|
16
|
-
} from './constants.js';
|
|
17
|
-
import type {
|
|
18
|
-
ComponentMeta,
|
|
19
|
-
ComponentData,
|
|
20
|
-
RegistryIndex,
|
|
21
|
-
RegistryComponent,
|
|
22
|
-
LocalCache,
|
|
23
|
-
CLIConfig,
|
|
24
|
-
} from '@foundrykit/types';
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Default configuration
|
|
28
|
-
*/
|
|
29
|
-
const DEFAULT_CONFIG: CLIConfig = {
|
|
30
|
-
registryUrl: process.env.FOUNDRYKIT_REGISTRY_URL || DEFAULT_REGISTRY_URL,
|
|
31
|
-
cacheDir: path.join(os.homedir(), CACHE_DIR, 'cache'),
|
|
32
|
-
cacheMaxAge: CACHE_TTL,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Registry service class
|
|
37
|
-
*/
|
|
38
|
-
export class RegistryService {
|
|
39
|
-
private config: CLIConfig;
|
|
40
|
-
private cacheFile: string;
|
|
41
|
-
private foundryKitPackagesPromise: Promise<ComponentMeta[]> | null = null;
|
|
42
|
-
|
|
43
|
-
constructor(config?: Partial<CLIConfig>) {
|
|
44
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
45
|
-
this.cacheFile = path.join(this.config.cacheDir, 'registry.json');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Fetch all components from the registry
|
|
50
|
-
*/
|
|
51
|
-
async getComponents(
|
|
52
|
-
category?: string,
|
|
53
|
-
search?: string
|
|
54
|
-
): Promise<RegistryIndex> {
|
|
55
|
-
try {
|
|
56
|
-
// Build query params
|
|
57
|
-
const params = new URLSearchParams();
|
|
58
|
-
if (category) params.append('category', category);
|
|
59
|
-
if (search) params.append('search', search);
|
|
60
|
-
|
|
61
|
-
const url = `${this.config.registryUrl}/registry${
|
|
62
|
-
params.toString() ? '?' + params.toString() : ''
|
|
63
|
-
}`;
|
|
64
|
-
|
|
65
|
-
console.log(chalk.gray(`Fetching from: ${url}`));
|
|
66
|
-
const response = await fetch(url);
|
|
67
|
-
|
|
68
|
-
if (!response.ok) {
|
|
69
|
-
throw new Error(
|
|
70
|
-
`Registry returned ${response.status}: ${response.statusText}`
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const components = (await response.json()) as RegistryIndex;
|
|
75
|
-
|
|
76
|
-
// Update cache
|
|
77
|
-
await this.updateCache(components);
|
|
78
|
-
|
|
79
|
-
return this.filterComponents(
|
|
80
|
-
await this.withFoundryKitPackages(components),
|
|
81
|
-
category,
|
|
82
|
-
search
|
|
83
|
-
);
|
|
84
|
-
} catch (error) {
|
|
85
|
-
// Try to load from cache
|
|
86
|
-
const cache = await this.loadCache();
|
|
87
|
-
if (cache && this.isCacheValid(cache)) {
|
|
88
|
-
console.log(chalk.yellow('Using cached registry data'));
|
|
89
|
-
return this.filterComponents(
|
|
90
|
-
await this.withFoundryKitPackages(cache.components),
|
|
91
|
-
category,
|
|
92
|
-
search
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const npmPackages = await this.fetchFoundryKitPackages();
|
|
97
|
-
if (npmPackages.length > 0) {
|
|
98
|
-
console.log(chalk.yellow('Using npm package data'));
|
|
99
|
-
return this.filterComponents(npmPackages, category, search);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// No valid cache, throw error
|
|
103
|
-
throw new Error(
|
|
104
|
-
`Failed to connect to registry: ${error instanceof Error ? error.message : 'Unknown error'}\n` +
|
|
105
|
-
`Registry URL: ${this.config.registryUrl}\n` +
|
|
106
|
-
`Try again later or check your internet connection.`
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Fetch a specific component by ID
|
|
113
|
-
*/
|
|
114
|
-
async getComponent(id: string, force = false): Promise<ComponentData> {
|
|
115
|
-
// Check if it's a special dependency first
|
|
116
|
-
if (this.isSpecialDependency(id)) {
|
|
117
|
-
return this.createSpecialDependency(id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const foundryKitPackage = this.resolveFoundryKitPackageName(id);
|
|
121
|
-
const isKnownFoundryKitPackage =
|
|
122
|
-
id.startsWith('@foundrykit/') ||
|
|
123
|
-
id.endsWith('-plugin') ||
|
|
124
|
-
FOUNDRYKIT_PACKAGE_ALIASES.includes(
|
|
125
|
-
id as (typeof FOUNDRYKIT_PACKAGE_ALIASES)[number]
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
const cache = await this.loadCache();
|
|
129
|
-
const cachedComponent = cache?.componentData?.[id];
|
|
130
|
-
const hasValidCache = cache ? this.isCacheValid(cache) : false;
|
|
131
|
-
|
|
132
|
-
if (!force && cachedComponent && hasValidCache) {
|
|
133
|
-
console.log(chalk.gray(`Using cached data for component: ${id}`));
|
|
134
|
-
return cachedComponent;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
const url = `${this.config.registryUrl}/registry/${id}`;
|
|
139
|
-
console.log(chalk.gray(`Fetching component: ${url}`));
|
|
140
|
-
|
|
141
|
-
const response = await fetch(url);
|
|
142
|
-
|
|
143
|
-
if (!response.ok) {
|
|
144
|
-
if (response.status === 404) {
|
|
145
|
-
if (foundryKitPackage) {
|
|
146
|
-
const npmPackage = await this.createFoundryKitPackageDependency(
|
|
147
|
-
foundryKitPackage,
|
|
148
|
-
!isKnownFoundryKitPackage
|
|
149
|
-
);
|
|
150
|
-
if (npmPackage) return npmPackage;
|
|
151
|
-
}
|
|
152
|
-
throw new Error(`Component "${id}" not found in registry`);
|
|
153
|
-
}
|
|
154
|
-
throw new Error(
|
|
155
|
-
`Registry returned ${response.status}: ${response.statusText}`
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const component = (await response.json()) as RegistryComponent;
|
|
160
|
-
|
|
161
|
-
// Cache the component data
|
|
162
|
-
await this.cacheComponent(component);
|
|
163
|
-
|
|
164
|
-
return component;
|
|
165
|
-
} catch (error) {
|
|
166
|
-
if (foundryKitPackage) {
|
|
167
|
-
const npmPackage = await this.createFoundryKitPackageDependency(
|
|
168
|
-
foundryKitPackage,
|
|
169
|
-
!isKnownFoundryKitPackage
|
|
170
|
-
);
|
|
171
|
-
if (npmPackage) return npmPackage;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Fall back to any cached component data, even if stale
|
|
175
|
-
if (cachedComponent) {
|
|
176
|
-
console.log(chalk.yellow(`Using cached data for component: ${id}`));
|
|
177
|
-
return cachedComponent;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// No cached data, throw error
|
|
181
|
-
throw new Error(
|
|
182
|
-
`Failed to fetch component "${id}": ${error instanceof Error ? error.message : 'Unknown error'}\n` +
|
|
183
|
-
`Registry URL: ${this.config.registryUrl}\n` +
|
|
184
|
-
`Try again later or check your internet connection.`
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Check if a dependency ID is a special dependency that should be handled locally
|
|
191
|
-
*/
|
|
192
|
-
private isSpecialDependency(id: string): boolean {
|
|
193
|
-
const specialDependencies = [
|
|
194
|
-
'utils',
|
|
195
|
-
'lib',
|
|
196
|
-
'helpers',
|
|
197
|
-
'cn', // Utils aliases
|
|
198
|
-
'hooks',
|
|
199
|
-
'types', // Package dependencies
|
|
200
|
-
'maps',
|
|
201
|
-
];
|
|
202
|
-
return specialDependencies.includes(id);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Create a special dependency component data
|
|
207
|
-
*/
|
|
208
|
-
private async createSpecialDependency(id: string): Promise<ComponentData> {
|
|
209
|
-
// Handle utils and its aliases
|
|
210
|
-
if (['utils', 'lib', 'helpers', 'cn'].includes(id)) {
|
|
211
|
-
return {
|
|
212
|
-
id: 'utils',
|
|
213
|
-
name: 'Utils',
|
|
214
|
-
description: 'Utility functions for className merging',
|
|
215
|
-
category: 'primitives',
|
|
216
|
-
files: [
|
|
217
|
-
{
|
|
218
|
-
path: 'lib/utils.ts',
|
|
219
|
-
content: `import { type ClassValue, clsx } from 'clsx';
|
|
220
|
-
import { twMerge } from 'tailwind-merge';
|
|
221
|
-
|
|
222
|
-
export function cn(...inputs: ClassValue[]) {
|
|
223
|
-
return twMerge(clsx(inputs));
|
|
224
|
-
}`,
|
|
225
|
-
type: 'util',
|
|
226
|
-
},
|
|
227
|
-
],
|
|
228
|
-
registryDependencies: [],
|
|
229
|
-
packageDependencies: ['clsx', 'tailwind-merge'],
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (id === 'hooks') {
|
|
234
|
-
const component =
|
|
235
|
-
await this.createFoundryKitPackageDependency('@foundrykit/hooks');
|
|
236
|
-
if (component) return component;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (id === 'types') {
|
|
240
|
-
const component =
|
|
241
|
-
await this.createFoundryKitPackageDependency('@foundrykit/types');
|
|
242
|
-
if (component) return component;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (id === 'maps') {
|
|
246
|
-
const component =
|
|
247
|
-
await this.createFoundryKitPackageDependency('@foundrykit/maps');
|
|
248
|
-
if (component) return component;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
throw new Error(`Unknown special dependency: ${id}`);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Check if a package is installed in the current project
|
|
256
|
-
*/
|
|
257
|
-
private async isPackageInstalled(packageName: string): Promise<boolean> {
|
|
258
|
-
try {
|
|
259
|
-
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
260
|
-
if (!(await fs.pathExists(packageJsonPath))) {
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const packageJson = await fs.readJson(packageJsonPath);
|
|
265
|
-
const allDeps = {
|
|
266
|
-
...packageJson.dependencies,
|
|
267
|
-
...packageJson.devDependencies,
|
|
268
|
-
...packageJson.peerDependencies,
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
return packageName in allDeps;
|
|
272
|
-
} catch (error) {
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
private resolveFoundryKitPackageName(id: string): string | null {
|
|
278
|
-
if (/^@foundrykit\/[a-z0-9][a-z0-9-]*$/i.test(id)) {
|
|
279
|
-
return id;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (
|
|
283
|
-
/^[a-z0-9][a-z0-9-]*$/i.test(id)
|
|
284
|
-
) {
|
|
285
|
-
return `@foundrykit/${id}`;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return null;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
private async createFoundryKitPackageDependency(
|
|
292
|
-
packageName: string,
|
|
293
|
-
requirePublishedPackage = false
|
|
294
|
-
): Promise<ComponentData | null> {
|
|
295
|
-
const id = packageName.replace('@foundrykit/', '');
|
|
296
|
-
const category =
|
|
297
|
-
id === 'maps' ? 'maps' : id === 'hooks' ? 'hooks' : 'packages';
|
|
298
|
-
const metadata = await this.fetchPackageMetadata(packageName);
|
|
299
|
-
if (requirePublishedPackage && !metadata) {
|
|
300
|
-
return null;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return {
|
|
304
|
-
id,
|
|
305
|
-
name: this.toDisplayName(id),
|
|
306
|
-
version: metadata?.version,
|
|
307
|
-
description: metadata?.description ?? `${packageName} npm package`,
|
|
308
|
-
category,
|
|
309
|
-
files: [],
|
|
310
|
-
registryDependencies: [],
|
|
311
|
-
packageDependencies: [packageName],
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
private async withFoundryKitPackages(
|
|
316
|
-
components: ComponentMeta[]
|
|
317
|
-
): Promise<ComponentMeta[]> {
|
|
318
|
-
const packageEntries = await this.fetchFoundryKitPackages();
|
|
319
|
-
const seenIds = new Set(components.map(component => component.id));
|
|
320
|
-
return [
|
|
321
|
-
...components,
|
|
322
|
-
...packageEntries.filter(component => !seenIds.has(component.id)),
|
|
323
|
-
];
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
private toDisplayName(id: string): string {
|
|
327
|
-
return id
|
|
328
|
-
.split('-')
|
|
329
|
-
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
|
|
330
|
-
.join(' ');
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
private async fetchFoundryKitPackages(): Promise<ComponentMeta[]> {
|
|
334
|
-
if (!this.foundryKitPackagesPromise) {
|
|
335
|
-
this.foundryKitPackagesPromise = this.fetchFoundryKitPackagesFromNpm();
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
try {
|
|
339
|
-
return await this.foundryKitPackagesPromise;
|
|
340
|
-
} catch {
|
|
341
|
-
return this.getFallbackFoundryKitPackages();
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
private async fetchFoundryKitPackagesFromNpm(): Promise<ComponentMeta[]> {
|
|
346
|
-
const url = `${NPM_REGISTRY_URL}/-/v1/search?text=scope:foundrykit&size=250`;
|
|
347
|
-
console.log(chalk.gray(`Fetching FoundryKit packages from npm: ${url}`));
|
|
348
|
-
|
|
349
|
-
const response = await fetch(url);
|
|
350
|
-
if (!response.ok) {
|
|
351
|
-
throw new Error(
|
|
352
|
-
`npm returned ${response.status}: ${response.statusText}`
|
|
353
|
-
);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
const data = (await response.json()) as {
|
|
357
|
-
objects?: Array<{
|
|
358
|
-
package?: {
|
|
359
|
-
name?: string;
|
|
360
|
-
version?: string;
|
|
361
|
-
description?: string;
|
|
362
|
-
keywords?: string[];
|
|
363
|
-
};
|
|
364
|
-
}>;
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
return (data.objects ?? [])
|
|
368
|
-
.map(item => item.package)
|
|
369
|
-
.filter((pkg): pkg is NonNullable<typeof pkg> => {
|
|
370
|
-
return Boolean(pkg?.name?.startsWith('@foundrykit/'));
|
|
371
|
-
})
|
|
372
|
-
.map(pkg => this.toPackageComponentMeta(pkg.name!, {
|
|
373
|
-
version: pkg.version,
|
|
374
|
-
description: pkg.description,
|
|
375
|
-
keywords: pkg.keywords,
|
|
376
|
-
}))
|
|
377
|
-
.sort((a, b) => a.id.localeCompare(b.id));
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
private async fetchPackageMetadata(packageName: string): Promise<{
|
|
381
|
-
version?: string;
|
|
382
|
-
description?: string;
|
|
383
|
-
keywords?: string[];
|
|
384
|
-
} | null> {
|
|
385
|
-
try {
|
|
386
|
-
const encodedName = packageName.replace('/', '%2F');
|
|
387
|
-
const response = await fetch(`${NPM_REGISTRY_URL}/${encodedName}`);
|
|
388
|
-
if (!response.ok) return null;
|
|
389
|
-
|
|
390
|
-
const data = (await response.json()) as {
|
|
391
|
-
name?: string;
|
|
392
|
-
description?: string;
|
|
393
|
-
keywords?: string[];
|
|
394
|
-
'dist-tags'?: {
|
|
395
|
-
latest?: string;
|
|
396
|
-
};
|
|
397
|
-
versions?: Record<string, unknown>;
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
return {
|
|
401
|
-
version:
|
|
402
|
-
data['dist-tags']?.latest ??
|
|
403
|
-
Object.keys(data.versions ?? {}).sort(compareVersions).at(-1),
|
|
404
|
-
description: data.description,
|
|
405
|
-
keywords: data.keywords,
|
|
406
|
-
};
|
|
407
|
-
} catch {
|
|
408
|
-
return null;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
private getFallbackFoundryKitPackages(): ComponentMeta[] {
|
|
413
|
-
return FOUNDRYKIT_PACKAGE_ALIASES.map(packageId =>
|
|
414
|
-
this.toPackageComponentMeta(`@foundrykit/${packageId}`)
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
private toPackageComponentMeta(
|
|
419
|
-
packageName: string,
|
|
420
|
-
metadata: {
|
|
421
|
-
version?: string;
|
|
422
|
-
description?: string;
|
|
423
|
-
keywords?: string[];
|
|
424
|
-
} = {}
|
|
425
|
-
): ComponentMeta {
|
|
426
|
-
const id = packageName.replace('@foundrykit/', '');
|
|
427
|
-
return {
|
|
428
|
-
id,
|
|
429
|
-
name: this.toDisplayName(id),
|
|
430
|
-
version: metadata.version,
|
|
431
|
-
category:
|
|
432
|
-
id === 'maps' ? 'maps' : id === 'hooks' ? 'hooks' : 'packages',
|
|
433
|
-
description: metadata.description ?? `${packageName} npm package`,
|
|
434
|
-
tags: metadata.keywords,
|
|
435
|
-
packageDependencies: [packageName],
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Update the local cache with new component list
|
|
441
|
-
*/
|
|
442
|
-
private async updateCache(components: RegistryIndex): Promise<void> {
|
|
443
|
-
try {
|
|
444
|
-
const cache = await this.loadCache();
|
|
445
|
-
const newCache: LocalCache = {
|
|
446
|
-
version: '1.0.0',
|
|
447
|
-
lastUpdated: new Date().toISOString(),
|
|
448
|
-
components,
|
|
449
|
-
componentData: cache?.componentData || {},
|
|
450
|
-
};
|
|
451
|
-
|
|
452
|
-
await fs.ensureDir(this.config.cacheDir);
|
|
453
|
-
await fs.writeJson(this.cacheFile, newCache, { spaces: 2 });
|
|
454
|
-
} catch (error) {
|
|
455
|
-
// Cache update failed, but don't throw - this is not critical
|
|
456
|
-
console.log(chalk.gray('Failed to update cache'));
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* Cache a specific component
|
|
462
|
-
*/
|
|
463
|
-
private async cacheComponent(component: ComponentData): Promise<void> {
|
|
464
|
-
try {
|
|
465
|
-
const cache = (await this.loadCache()) ?? {
|
|
466
|
-
version: '1.0.0',
|
|
467
|
-
lastUpdated: new Date().toISOString(),
|
|
468
|
-
components: [],
|
|
469
|
-
componentData: {},
|
|
470
|
-
};
|
|
471
|
-
|
|
472
|
-
cache.componentData[component.id] = component;
|
|
473
|
-
cache.lastUpdated = new Date().toISOString();
|
|
474
|
-
|
|
475
|
-
await fs.ensureDir(this.config.cacheDir);
|
|
476
|
-
await fs.writeJson(this.cacheFile, cache, { spaces: 2 });
|
|
477
|
-
} catch (error) {
|
|
478
|
-
// Cache update failed, but don't throw
|
|
479
|
-
console.log(chalk.gray('Failed to cache component'));
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Load the local cache
|
|
485
|
-
*/
|
|
486
|
-
private async loadCache(): Promise<LocalCache | null> {
|
|
487
|
-
try {
|
|
488
|
-
if (await fs.pathExists(this.cacheFile)) {
|
|
489
|
-
return await fs.readJson(this.cacheFile);
|
|
490
|
-
}
|
|
491
|
-
} catch (error) {
|
|
492
|
-
console.log(chalk.gray('Failed to load cache'));
|
|
493
|
-
}
|
|
494
|
-
return null;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
/**
|
|
498
|
-
* Check if cache is still valid
|
|
499
|
-
*/
|
|
500
|
-
private isCacheValid(cache: LocalCache): boolean {
|
|
501
|
-
const lastUpdated = new Date(cache.lastUpdated).getTime();
|
|
502
|
-
const now = Date.now();
|
|
503
|
-
return now - lastUpdated < this.config.cacheMaxAge;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
/**
|
|
507
|
-
* Filter components by category and search
|
|
508
|
-
*/
|
|
509
|
-
private filterComponents(
|
|
510
|
-
components: ComponentMeta[],
|
|
511
|
-
category?: string,
|
|
512
|
-
search?: string
|
|
513
|
-
): ComponentMeta[] {
|
|
514
|
-
let filtered = components;
|
|
515
|
-
|
|
516
|
-
if (category) {
|
|
517
|
-
filtered = filtered.filter(c => c.category === category);
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
if (search) {
|
|
521
|
-
const searchLower = search.toLowerCase();
|
|
522
|
-
filtered = filtered.filter(
|
|
523
|
-
c =>
|
|
524
|
-
c.name.toLowerCase().includes(searchLower) ||
|
|
525
|
-
c.description.toLowerCase().includes(searchLower) ||
|
|
526
|
-
c.id.toLowerCase().includes(searchLower) ||
|
|
527
|
-
c.tags?.some(tag => tag.toLowerCase().includes(searchLower))
|
|
528
|
-
);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
return filtered;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* Clear the local cache
|
|
536
|
-
*/
|
|
537
|
-
async clearCache(): Promise<void> {
|
|
538
|
-
try {
|
|
539
|
-
if (await fs.pathExists(this.cacheFile)) {
|
|
540
|
-
await fs.remove(this.cacheFile);
|
|
541
|
-
console.log(chalk.green('Cache cleared successfully'));
|
|
542
|
-
}
|
|
543
|
-
} catch (error) {
|
|
544
|
-
throw new Error(
|
|
545
|
-
`Failed to clear cache: ${error instanceof Error ? error.message : 'Unknown error'}`
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
/**
|
|
551
|
-
* Get cache statistics
|
|
552
|
-
*/
|
|
553
|
-
async getCacheStats(): Promise<{
|
|
554
|
-
exists: boolean;
|
|
555
|
-
size: number;
|
|
556
|
-
lastUpdated: string | null;
|
|
557
|
-
componentCount: number;
|
|
558
|
-
}> {
|
|
559
|
-
try {
|
|
560
|
-
if (await fs.pathExists(this.cacheFile)) {
|
|
561
|
-
const cache = await this.loadCache();
|
|
562
|
-
const stats = await fs.stat(this.cacheFile);
|
|
563
|
-
|
|
564
|
-
return {
|
|
565
|
-
exists: true,
|
|
566
|
-
size: stats.size,
|
|
567
|
-
lastUpdated: cache?.lastUpdated || null,
|
|
568
|
-
componentCount: cache?.components.length || 0,
|
|
569
|
-
};
|
|
570
|
-
}
|
|
571
|
-
} catch (error) {
|
|
572
|
-
// Ignore errors
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
return {
|
|
576
|
-
exists: false,
|
|
577
|
-
size: 0,
|
|
578
|
-
lastUpdated: null,
|
|
579
|
-
componentCount: 0,
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
/**
|
|
585
|
-
* Singleton instance
|
|
586
|
-
*/
|
|
587
|
-
let registryInstance: RegistryService | null = null;
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Get the registry service instance
|
|
591
|
-
*/
|
|
592
|
-
export function getRegistry(config?: Partial<CLIConfig>): RegistryService {
|
|
593
|
-
if (!registryInstance) {
|
|
594
|
-
registryInstance = new RegistryService(config);
|
|
595
|
-
}
|
|
596
|
-
return registryInstance;
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
/**
|
|
600
|
-
* Reset the registry instance (mainly for testing)
|
|
601
|
-
*/
|
|
602
|
-
export function resetRegistry(): void {
|
|
603
|
-
registryInstance = null;
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
function compareVersions(a: string, b: string): number {
|
|
607
|
-
const aParts = normalizeVersion(a).split('.').map(Number);
|
|
608
|
-
const bParts = normalizeVersion(b).split('.').map(Number);
|
|
609
|
-
const length = Math.max(aParts.length, bParts.length);
|
|
610
|
-
|
|
611
|
-
for (let index = 0; index < length; index++) {
|
|
612
|
-
const diff = (aParts[index] ?? 0) - (bParts[index] ?? 0);
|
|
613
|
-
if (diff !== 0) return diff;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return 0;
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
function normalizeVersion(version: string): string {
|
|
620
|
-
return version
|
|
621
|
-
.replace(/^[^\d]*/, '')
|
|
622
|
-
.split('-')[0]
|
|
623
|
-
?.replace(/[^\d.]/g, '') || '0';
|
|
624
|
-
}
|