MCPcopy Create free account
hub / github.com/Snapchat/Valdi / resolveComponentFile

Function resolveComponentFile

npm_modules/cli/src/commands/preview.ts:29–53  ·  view source on GitHub ↗

* Resolve a component argument to an absolute .tsx file path. * Accepts: * - A file path (absolute or relative): ./valdi_modules/widgets/src/.../Checkbox.tsx * - An import path: widgets/src/components/button/Checkbox

(component: string, workspaceRoot: string)

Source from the content-addressed store, hash-verified

27 * - An import path: widgets/src/components/button/Checkbox
28 */
29function resolveComponentFile(component: string, workspaceRoot: string): string {
30 // If it already looks like a file path
31 if (component.endsWith('.tsx') || component.endsWith('.ts')) {
32 const abs = path.resolve(component);
33 if (fs.existsSync(abs)) return abs;
34 throw new CliError(`Component file not found: ${abs}`);
35 }
36
37 // Treat as an import path — search common module directories
38 const candidates = [
39 // External repos: valdi_modules/<module>/...
40 path.join(workspaceRoot, 'valdi_modules', component + '.tsx'),
41 // Valdi repo: src/valdi_modules/src/valdi/<module>/...
42 path.join(workspaceRoot, 'src/valdi_modules/src/valdi', component + '.tsx'),
43 ];
44
45 for (const candidate of candidates) {
46 if (fs.existsSync(candidate)) return candidate;
47 }
48
49 throw new CliError(
50 `Could not find component file for "${component}". Searched:\n` +
51 candidates.map(c => ` ${c}`).join('\n'),
52 );
53}
54
55/**
56 * Compute the import path from a resolved file path.

Callers 1

valdiPreviewFunction · 0.85

Calls 3

resolveMethod · 0.65
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected