@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.
@@ -1,570 +0,0 @@
1
- /**
2
- * Tests for dependency resolution in the CLI
3
- */
4
-
5
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
6
- import fs from 'fs-extra';
7
- import path from 'path';
8
- import os from 'os';
9
- import { RegistryService } from '../src/lib/registry.js';
10
- import {
11
- getImportPath,
12
- transformImportsForConfig,
13
- } from '../src/commands/add.js';
14
- import type { ComponentData } from '@foundrykit/types';
15
-
16
- describe('Dependency Resolution', () => {
17
- let testDir: string;
18
- let registry: RegistryService;
19
-
20
- beforeEach(async () => {
21
- // Create a temporary test directory
22
- testDir = path.join(os.tmpdir(), 'foundrykit-test-' + Date.now());
23
- await fs.ensureDir(testDir);
24
-
25
- // Create a registry instance with test config
26
- registry = new RegistryService({
27
- registryUrl: 'https://test.registry.com',
28
- cacheDir: path.join(testDir, '.cache'),
29
- cacheMaxAge: 1000 * 60 * 60,
30
- });
31
-
32
- // Mock fetch
33
- global.fetch = vi.fn();
34
- });
35
-
36
- afterEach(async () => {
37
- // Clean up test directory
38
- if (await fs.pathExists(testDir)) {
39
- await fs.remove(testDir);
40
- }
41
- vi.restoreAllMocks();
42
- });
43
-
44
- describe('Special Dependencies', () => {
45
- it('should create utils dependency locally without fetching', async () => {
46
- const component = await registry.getComponent('utils');
47
-
48
- expect(component).toBeDefined();
49
- expect(component.id).toBe('utils');
50
- expect(component.files).toHaveLength(1);
51
- expect(component.files[0]!.path).toBe('lib/utils.ts');
52
- expect(component.files[0]!.content).toContain('cn');
53
- expect(component.files[0]!.content).toContain('clsx');
54
- expect(component.files[0]!.content).toContain('twMerge');
55
- });
56
-
57
- it('should handle lib alias for utils', async () => {
58
- const component = await registry.getComponent('lib');
59
-
60
- expect(component).toBeDefined();
61
- expect(component.id).toBe('utils');
62
- expect(component.files[0]!.path).toBe('lib/utils.ts');
63
- });
64
-
65
- it('should handle helpers alias for utils', async () => {
66
- const component = await registry.getComponent('helpers');
67
-
68
- expect(component).toBeDefined();
69
- expect(component.id).toBe('utils');
70
- expect(component.files[0]!.path).toBe('lib/utils.ts');
71
- });
72
-
73
- it('should handle cn alias for utils', async () => {
74
- const component = await registry.getComponent('cn');
75
-
76
- expect(component).toBeDefined();
77
- expect(component.id).toBe('utils');
78
- expect(component.files[0]!.path).toBe('lib/utils.ts');
79
- });
80
- });
81
-
82
- describe('Registry Component Fetching', () => {
83
- it('should merge scoped npm packages into the component list', async () => {
84
- (global.fetch as any)
85
- .mockResolvedValueOnce({
86
- ok: true,
87
- json: async () => [
88
- {
89
- id: 'button',
90
- name: 'Button',
91
- category: 'primitives',
92
- description: 'Button component',
93
- },
94
- ],
95
- })
96
- .mockResolvedValueOnce({
97
- ok: true,
98
- json: async () => ({
99
- objects: [
100
- {
101
- package: {
102
- name: '@foundrykit/menu-plugin',
103
- version: '3.1.0',
104
- description: 'Menu plugin',
105
- keywords: ['foundrykit', 'plugin'],
106
- },
107
- },
108
- ],
109
- }),
110
- });
111
-
112
- const components = await registry.getComponents();
113
-
114
- expect(components).toEqual(
115
- expect.arrayContaining([
116
- expect.objectContaining({
117
- id: 'menu-plugin',
118
- version: '3.1.0',
119
- category: 'packages',
120
- packageDependencies: ['@foundrykit/menu-plugin'],
121
- }),
122
- ])
123
- );
124
- });
125
-
126
- it('should resolve explicit scoped npm packages when absent from the registry', async () => {
127
- (global.fetch as any)
128
- .mockResolvedValueOnce({
129
- ok: false,
130
- status: 404,
131
- statusText: 'Not Found',
132
- })
133
- .mockResolvedValueOnce({
134
- ok: true,
135
- json: async () => ({
136
- name: '@foundrykit/menu-plugin',
137
- description: 'Menu plugin',
138
- 'dist-tags': {
139
- latest: '3.1.0',
140
- },
141
- }),
142
- });
143
-
144
- const component = await registry.getComponent('@foundrykit/menu-plugin');
145
-
146
- expect(component).toEqual(
147
- expect.objectContaining({
148
- id: 'menu-plugin',
149
- version: '3.1.0',
150
- category: 'packages',
151
- files: [],
152
- packageDependencies: ['@foundrykit/menu-plugin'],
153
- })
154
- );
155
- });
156
-
157
- it('should resolve bare npm package IDs surfaced by the package list', async () => {
158
- (global.fetch as any)
159
- .mockResolvedValueOnce({
160
- ok: false,
161
- status: 404,
162
- statusText: 'Not Found',
163
- })
164
- .mockResolvedValueOnce({
165
- ok: true,
166
- json: async () => ({
167
- name: '@foundrykit/menu-plugin',
168
- 'dist-tags': {
169
- latest: '3.1.0',
170
- },
171
- }),
172
- });
173
-
174
- const component = await registry.getComponent('menu-plugin');
175
-
176
- expect(component).toEqual(
177
- expect.objectContaining({
178
- id: 'menu-plugin',
179
- version: '3.1.0',
180
- packageDependencies: ['@foundrykit/menu-plugin'],
181
- })
182
- );
183
- });
184
-
185
- it('should fetch regular components from registry', async () => {
186
- const mockComponent: ComponentData = {
187
- id: 'button',
188
- name: 'Button',
189
- category: 'components',
190
- description: 'A button component',
191
- files: [
192
- {
193
- path: 'components/ui/button.tsx',
194
- type: 'component',
195
- content: 'export function Button() { return <button />; }',
196
- },
197
- ],
198
- };
199
-
200
- (global.fetch as any).mockResolvedValueOnce({
201
- ok: true,
202
- json: async () => mockComponent,
203
- });
204
-
205
- const component = await registry.getComponent('button');
206
-
207
- expect(component).toEqual(mockComponent);
208
- expect(global.fetch).toHaveBeenCalledWith(
209
- 'https://test.registry.com/registry/button'
210
- );
211
- });
212
-
213
- it('should handle 404 errors for non-existent components', async () => {
214
- (global.fetch as any).mockResolvedValueOnce({
215
- ok: false,
216
- status: 404,
217
- statusText: 'Not Found',
218
- });
219
-
220
- await expect(registry.getComponent('non-existent')).rejects.toThrow(
221
- 'Component "non-existent" not found in registry'
222
- );
223
- });
224
-
225
- it('should handle network errors gracefully', async () => {
226
- (global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
227
-
228
- await expect(registry.getComponent('button')).rejects.toThrow(
229
- 'Failed to fetch component "button"'
230
- );
231
- });
232
- });
233
-
234
- describe('Cache Management', () => {
235
- it('should cache fetched components', async () => {
236
- const mockComponent: ComponentData = {
237
- id: 'card',
238
- name: 'Card',
239
- category: 'components',
240
- description: 'A card component',
241
- files: [
242
- {
243
- path: 'components/ui/card.tsx',
244
- type: 'component',
245
- content: 'export function Card() { return <div />; }',
246
- },
247
- ],
248
- };
249
-
250
- (global.fetch as any).mockResolvedValueOnce({
251
- ok: true,
252
- json: async () => mockComponent,
253
- });
254
-
255
- // First fetch - should call API
256
- await registry.getComponent('card');
257
- expect(global.fetch).toHaveBeenCalledTimes(1);
258
-
259
- // Second fetch - should use cache
260
- (global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
261
- const cached = await registry.getComponent('card');
262
-
263
- expect(cached).toEqual(mockComponent);
264
- });
265
-
266
- it('should clear cache when requested', async () => {
267
- const cacheFile = path.join(testDir, '.cache', 'registry.json');
268
-
269
- // Create a cache file
270
- await fs.ensureDir(path.dirname(cacheFile));
271
- await fs.writeJson(cacheFile, { test: 'data' });
272
-
273
- await registry.clearCache();
274
-
275
- expect(await fs.pathExists(cacheFile)).toBe(false);
276
- });
277
-
278
- it('should provide cache statistics', async () => {
279
- const stats = await registry.getCacheStats();
280
-
281
- expect(stats).toHaveProperty('exists');
282
- expect(stats).toHaveProperty('size');
283
- expect(stats).toHaveProperty('lastUpdated');
284
- expect(stats).toHaveProperty('componentCount');
285
- });
286
- });
287
-
288
- describe('Component Installation', () => {
289
- it('should not overwrite existing utils file', async () => {
290
- const utilsPath = path.join(testDir, 'lib', 'utils.ts');
291
- const customContent = '// Custom utils implementation';
292
-
293
- await fs.ensureDir(path.dirname(utilsPath));
294
- await fs.writeFile(utilsPath, customContent);
295
-
296
- // Simulate checking for existing file
297
- const exists = await fs.pathExists(utilsPath);
298
- expect(exists).toBe(true);
299
-
300
- const content = await fs.readFile(utilsPath, 'utf-8');
301
- expect(content).toBe(customContent);
302
- });
303
-
304
- it('should create utils file if it does not exist', async () => {
305
- const utilsPath = path.join(testDir, 'lib', 'utils.ts');
306
-
307
- expect(await fs.pathExists(utilsPath)).toBe(false);
308
-
309
- // Get utils component
310
- const utils = await registry.getComponent('utils');
311
-
312
- // Simulate installation
313
- await fs.ensureDir(path.dirname(utilsPath));
314
- await fs.writeFile(utilsPath, utils.files[0]!.content);
315
-
316
- expect(await fs.pathExists(utilsPath)).toBe(true);
317
-
318
- const content = await fs.readFile(utilsPath, 'utf-8');
319
- expect(content).toContain('cn');
320
- expect(content).toContain('clsx');
321
- });
322
- });
323
-
324
- describe('Dependency Chain Resolution', () => {
325
- it('should resolve multiple dependencies in order', async () => {
326
- const dependencies = ['utils', 'button', 'card'];
327
- const resolved: string[] = [];
328
-
329
- for (const dep of dependencies) {
330
- if (dep === 'utils') {
331
- const component = await registry.getComponent(dep);
332
- expect(component.id).toBe('utils');
333
- resolved.push(dep);
334
- } else {
335
- // Mock registry fetch for other components
336
- (global.fetch as any).mockResolvedValueOnce({
337
- ok: true,
338
- json: async () => ({
339
- id: dep,
340
- name: dep.charAt(0).toUpperCase() + dep.slice(1),
341
- category: 'components',
342
- description: `A ${dep} component`,
343
- files: [],
344
- }),
345
- });
346
-
347
- const component = await registry.getComponent(dep);
348
- expect(component.id).toBe(dep);
349
- resolved.push(dep);
350
- }
351
- }
352
-
353
- expect(resolved).toEqual(dependencies);
354
- });
355
-
356
- it('should continue installation when a dependency fails', async () => {
357
- const dependencies = ['utils', 'non-existent', 'button'];
358
- const resolved: string[] = [];
359
- const failed: string[] = [];
360
-
361
- for (const dep of dependencies) {
362
- try {
363
- if (dep === 'utils') {
364
- await registry.getComponent(dep);
365
- resolved.push(dep);
366
- } else if (dep === 'non-existent') {
367
- (global.fetch as any).mockResolvedValueOnce({
368
- ok: false,
369
- status: 404,
370
- });
371
- await registry.getComponent(dep);
372
- } else {
373
- (global.fetch as any).mockResolvedValueOnce({
374
- ok: true,
375
- json: async () => ({
376
- id: dep,
377
- name: 'Button',
378
- category: 'components',
379
- description: 'A button',
380
- files: [],
381
- }),
382
- });
383
- await registry.getComponent(dep);
384
- resolved.push(dep);
385
- }
386
- } catch (error) {
387
- failed.push(dep);
388
- }
389
- }
390
-
391
- expect(resolved).toEqual(['utils', 'button']);
392
- expect(failed).toEqual(['non-existent']);
393
- });
394
- });
395
-
396
- describe('Configured Import Rewriting', () => {
397
- const marketingConfig = {
398
- version: '1.0.16',
399
- registry: 'https://foundry.umiflow.com',
400
- paths: {
401
- utils: './src/lib/utils.ts',
402
- primitives: './src/components/marketing/primitives',
403
- components: './src/components/marketing/ui',
404
- blocks: './src/components/marketing/blocks',
405
- hooks: './src/hooks',
406
- animations: './src/components/marketing/animations',
407
- },
408
- packageManager: 'pnpm',
409
- typescript: true,
410
- framework: 'next',
411
- tailwind: {
412
- css: './src/app/(frontend)/globals.css',
413
- },
414
- aliases: {
415
- '@': './src',
416
- '@/components': './src/components/marketing',
417
- '@/lib': './src/lib',
418
- '@/hooks': './src/hooks',
419
- },
420
- };
421
-
422
- it('should preserve configured component aliases when rewriting primitive imports', () => {
423
- const source = `import { Input } from "@foundrykit/primitives";
424
- import { Button } from "@foundrykit/primitives";
425
- import { Separator } from "@foundrykit/primitives";
426
- import { Badge } from "@foundrykit/primitives";
427
- import { ScrollArea } from "@foundrykit/primitives";
428
- import { cn } from "@foundrykit/utils";`;
429
-
430
- const transformed = transformImportsForConfig(source, marketingConfig);
431
-
432
- expect(transformed).toContain(
433
- 'import { Input } from "@/components/marketing/primitives/input";'
434
- );
435
- expect(transformed).toContain(
436
- 'import { Button } from "@/components/marketing/primitives/button";'
437
- );
438
- expect(transformed).toContain(
439
- 'import { Separator } from "@/components/marketing/primitives/separator";'
440
- );
441
- expect(transformed).toContain(
442
- 'import { Badge } from "@/components/marketing/primitives/badge";'
443
- );
444
- expect(transformed).toContain(
445
- 'import { ScrollArea } from "@/components/marketing/primitives/scroll-area";'
446
- );
447
- expect(transformed).toContain('import { cn } from "@/lib/utils";');
448
- expect(transformed).not.toContain(
449
- '@/components/marketing/primitives/scrollarea'
450
- );
451
- expect(transformed).not.toContain('@/src/components');
452
- });
453
-
454
- it('should keep related primitive exports grouped on their component path', () => {
455
- const source =
456
- 'import { Button, buttonVariants, Sheet, SheetContent } from "@/components/primitives";';
457
-
458
- const transformed = transformImportsForConfig(source, marketingConfig);
459
-
460
- expect(transformed).toBe(
461
- 'import { Button, buttonVariants } from "@/components/marketing/primitives/button";\n' +
462
- 'import { Sheet, SheetContent } from "@/components/marketing/primitives/sheet";'
463
- );
464
- });
465
-
466
- it('should discover component paths from installed exports', async () => {
467
- const absoluteMarketingConfig = {
468
- ...marketingConfig,
469
- paths: {
470
- ...marketingConfig.paths,
471
- primitives: path.join(testDir, 'src/components/marketing/primitives'),
472
- },
473
- aliases: {
474
- ...marketingConfig.aliases,
475
- '@': path.join(testDir, 'src'),
476
- '@/components': path.join(testDir, 'src/components/marketing'),
477
- '@/lib': path.join(testDir, 'src/lib'),
478
- '@/hooks': path.join(testDir, 'src/hooks'),
479
- },
480
- };
481
-
482
- await fs.outputFile(
483
- path.join(
484
- testDir,
485
- 'src/components/marketing/primitives/fancy-picker/index.tsx'
486
- ),
487
- 'export { FancyPicker, FancyPickerItem, fancyPickerVariants };'
488
- );
489
-
490
- const transformed = transformImportsForConfig(
491
- 'import { FancyPickerItem, fancyPickerVariants } from "@foundrykit/primitives";',
492
- absoluteMarketingConfig
493
- );
494
-
495
- expect(transformed).toBe(
496
- 'import { FancyPickerItem, fancyPickerVariants } from "@/components/marketing/primitives/fancy-picker";'
497
- );
498
- });
499
-
500
- it('should build usage imports from the configured component path, not index.tsx', () => {
501
- const componentData: ComponentData = {
502
- id: 'advanced-search-bar',
503
- name: 'Advanced Search Bar',
504
- category: 'components',
505
- description: 'Search component',
506
- files: [
507
- {
508
- path: 'components/ui/advanced-search-bar/index.tsx',
509
- type: 'component',
510
- content: '',
511
- },
512
- ],
513
- };
514
-
515
- expect(getImportPath(componentData, marketingConfig)).toBe(
516
- '@/components/marketing/ui/advanced-search-bar'
517
- );
518
- });
519
-
520
- it('should build primitive usage imports from the configured primitive path', () => {
521
- const componentData: ComponentData = {
522
- id: 'input-group',
523
- name: 'Input Group',
524
- category: 'primitives',
525
- description: 'Input group primitive',
526
- files: [
527
- {
528
- path: 'components/primitives/input-group/index.tsx',
529
- type: 'component',
530
- content: '',
531
- },
532
- ],
533
- };
534
-
535
- expect(getImportPath(componentData, marketingConfig)).toBe(
536
- '@/components/marketing/primitives/input-group'
537
- );
538
- });
539
-
540
- it('should preserve package imports when hooks are configured as a package', () => {
541
- const packageHooksConfig = {
542
- ...marketingConfig,
543
- paths: {
544
- ...marketingConfig.paths,
545
- hooks: '@foundrykit/hooks',
546
- },
547
- aliases: {
548
- ...marketingConfig.aliases,
549
- '@/hooks': '@foundrykit/hooks',
550
- },
551
- };
552
-
553
- const source = `import {
554
- useAdvancedSearch,
555
- SearchContext,
556
- type EmptyStateBehavior,
557
- } from '@foundrykit/hooks';`;
558
-
559
- const transformed = transformImportsForConfig(
560
- source,
561
- packageHooksConfig
562
- );
563
-
564
- expect(transformed).toBe(source);
565
- expect(transformed).not.toContain('@/hooks/use-advanced-search');
566
- expect(transformed).not.toContain('@/hooks/search-context');
567
- expect(transformed).not.toContain('@/hooks/empty-state-behavior');
568
- });
569
- });
570
- });
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "@foundrykit/typescript-config/react-library.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "paths": {
6
- "@foundrykit/cli/*": ["./src/*"]
7
- }
8
- },
9
- "include": ["."],
10
- "exclude": ["node_modules", "dist"]
11
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "@foundrykit/typescript-config/react-library.json",
3
- "compilerOptions": {
4
- "outDir": "dist"
5
- },
6
- "include": ["src", "turbo"],
7
- "exclude": ["node_modules", "dist"]
8
- }
package/tsup.config.ts DELETED
@@ -1,16 +0,0 @@
1
- import { defineConfig } from 'tsup';
2
-
3
- export default defineConfig({
4
- entry: {
5
- index: 'src/index.ts',
6
- cli: 'src/index.ts',
7
- },
8
- format: ['esm'],
9
- dts: {
10
- entry: 'src/index.ts',
11
- },
12
- splitting: false,
13
- sourcemap: true,
14
- clean: true,
15
- external: [],
16
- });