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