-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathregistry.js
More file actions
316 lines (267 loc) · 10.3 KB
/
registry.js
File metadata and controls
316 lines (267 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const fs = require('fs');
const path = require('path');
const url = process.env.CF_PAGES_URL || "https://localhost:3000";
// Configuration
const config = {
componentsDir: './registry/nextjs/components',
outputFile: './registry.json',
libraryDirs: [
'./registry/universal',
'./registry/nextjs/lib',
],
baseDepUrl: `${url}/r/base.json`,
componentDepUrlPattern: `${url}/r/\${name}.json`,
dependencies: ["lucide-react", "@material/material-color-utilities", "@csstools/normalize.css"]
};
/**
* Extract component dependencies from file content
* Only handles @ imports to OTHER components (not local files)
*/
function extractComponentDependencies(fileContent, currentComponentName) {
const deps = new Set();
// Remove comments before parsing
const contentWithoutComments = fileContent
.replace(/\/\/.*$/gm, '')
.replace(/\/\*[\s\S]*?\*\//g, '');
// Pattern 1: Direct @ imports to other components
// e.g., import { Button } from "@/registry/nextjs/components/button"
const atImportRegex = /@\/registry\/nextjs\/components\/([a-zA-Z0-9_-]+)/g;
let match;
while ((match = atImportRegex.exec(contentWithoutComments)) !== null) {
const componentName = match[1];
// Don't add self-dependency
if (componentName !== currentComponentName) {
const depUrl = config.componentDepUrlPattern.replace('${name}', componentName);
deps.add(depUrl);
}
}
// Pattern 2: General @ imports that might reference components
// e.g., import { SomeComponent } from "@/components/some-component"
const generalAtImportRegex = /import\s+[^'"]*\s+from\s+['"]@\/(?:registry\/nextjs\/)?components\/([a-zA-Z0-9_-]+)['"]/g;
while ((match = generalAtImportRegex.exec(contentWithoutComments)) !== null) {
const potentialComponentName = match[1];
// Check if this component exists in our components directory
const componentPath = path.join(config.componentsDir, potentialComponentName);
if (fs.existsSync(componentPath) && potentialComponentName !== currentComponentName) {
const depUrl = config.componentDepUrlPattern.replace('${name}', potentialComponentName);
deps.add(depUrl);
}
}
// Pattern 3: Dynamic imports to components
const dynamicImportRegex = /import\(['"]@\/(?:registry\/nextjs\/)?components\/([a-zA-Z0-9_-]+)['"]\)/g;
while ((match = dynamicImportRegex.exec(contentWithoutComments)) !== null) {
const componentName = match[1];
if (componentName !== currentComponentName) {
const depUrl = config.componentDepUrlPattern.replace('${name}', componentName);
deps.add(depUrl);
}
}
return Array.from(deps);
}
/**
* Extract all local files that should be included with the component
* This now includes ALL neighboring files, not just explicitly imported ones
*/
function extractLocalFiles(fileContent, componentDir, componentName) {
const importedFiles = new Set();
// Remove comments before parsing imports
const contentWithoutComments = fileContent
.replace(/\/\/.*$/gm, '')
.replace(/\/\*[\s\S]*?\*\//g, '');
// First, find explicitly imported files
// Pattern 1: Relative imports with 'from' (./filename)
const relativeImportRegex = /import\s+[^'"]*\s+from\s+['"]\.\/([^'"\/]+)['"]/g;
let match;
while ((match = relativeImportRegex.exec(contentWithoutComments)) !== null) {
const importPath = match[1];
// Add file extension if not present
const possibleExtensions = ['', '.ts', '.tsx', '.js', '.jsx', '.css', '.scss', '.less'];
for (const ext of possibleExtensions) {
const fullPath = path.join(componentDir, importPath + ext);
if (fs.existsSync(fullPath)) {
const fileName = path.basename(fullPath);
if (fileName !== 'index.tsx') {
importedFiles.add(fileName);
}
break;
}
}
}
// Pattern 2: CSS imports without 'from' (import "./style.css")
const cssImportRegex = /import\s+['"]\.\/([^'"\/]+\.(?:css|scss|less))['"]/g;
while ((match = cssImportRegex.exec(contentWithoutComments)) !== null) {
const fileName = path.basename(match[1]);
importedFiles.add(fileName);
}
// Pattern 3: Other import patterns (export from, etc.)
const exportFromRegex = /export\s+[^'"]*\s+from\s+['"]\.\/([^'"\/]+)['"]/g;
while ((match = exportFromRegex.exec(contentWithoutComments)) !== null) {
const importPath = match[1];
const possibleExtensions = ['', '.ts', '.tsx', '.js', '.jsx'];
for (const ext of possibleExtensions) {
const fullPath = path.join(componentDir, importPath + ext);
if (fs.existsSync(fullPath)) {
const fileName = path.basename(fullPath);
if (fileName !== 'index.tsx') {
importedFiles.add(fileName);
}
break;
}
}
}
// NEW: Also include ALL neighboring files that look like they belong to the component
// This ensures we don't miss any files that should be included
if (fs.existsSync(componentDir)) {
const allFiles = fs.readdirSync(componentDir);
for (const file of allFiles) {
const filePath = path.join(componentDir, file);
const stat = fs.statSync(filePath);
// Skip directories and the index file itself
if (stat.isDirectory() || file === 'index.tsx') {
continue;
}
// Include common file types that are likely part of the component
const isComponentFile = /\.(tsx?|jsx?|css|scss|less|json|md)$/i.test(file);
// Skip common non-component files
const isIgnoredFile = /^(\.|package\.json|tsconfig\.json|\.gitignore|README\.md)/.test(file);
if (isComponentFile && !isIgnoredFile) {
importedFiles.add(file);
}
}
}
return Array.from(importedFiles);
}
/**
* Recursively collect all files in a directory
*/
function getAllFiles(dir, baseDir) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
results = results.concat(getAllFiles(filePath, baseDir));
} else {
const relPath = path.relative(baseDir, filePath);
results.push(relPath);
}
});
return results;
}
/**
* Generate registry entries for components
*/
function generateComponentEntries() {
const componentDirs = fs.readdirSync(config.componentsDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
const entries = componentDirs.map(componentName => {
const componentDir = path.join(config.componentsDir, componentName);
const indexPath = path.join(componentDir, 'index.tsx');
let additionalDeps = [];
let importedFiles = [];
if (fs.existsSync(indexPath)) {
const content = fs.readFileSync(indexPath, 'utf-8');
additionalDeps = extractComponentDependencies(content, componentName);
importedFiles = extractLocalFiles(content, componentDir, componentName);
// Compact logging
console.log(`🔍 ${componentName}: ${additionalDeps.length} deps, ${importedFiles.length} files [${importedFiles.join(', ')}]`);
} else {
// Even without index.tsx, we should include other files in the directory
if (fs.existsSync(componentDir)) {
const allFiles = fs.readdirSync(componentDir).filter(file => {
const stat = fs.statSync(path.join(componentDir, file));
return !stat.isDirectory() && /\.(tsx?|jsx?|css|scss|less)$/i.test(file);
});
importedFiles = allFiles;
}
console.warn(`⚠️ ${componentName}: No index.tsx, including ${importedFiles.length} files [${importedFiles.join(', ')}]`);
}
// Build files array starting with the index file (if it exists)
const files = [];
if (fs.existsSync(indexPath)) {
files.push({
path: `registry/nextjs/components/${componentName}/index.tsx`,
type: "registry:component"
});
}
// Add all imported/neighboring files
importedFiles.forEach(fileName => {
files.push({
path: `registry/nextjs/components/${componentName}/${fileName}`,
type: "registry:component"
});
});
return {
name: componentName,
type: "registry:block",
title: `${componentName} Component`,
description: `A Simple ${componentName} Component`,
registryDependencies: [
config.baseDepUrl,
...additionalDeps
],
files
};
});
return { componentDirs, entries };
}
/**
* Generate base library entry with all utility files
*/
function generateBaseLibraryEntry() {
const files = config.libraryDirs.flatMap(inputDir => {
const absDir = path.resolve(inputDir);
const registryPrefix = path.relative(process.cwd(), absDir);
return getAllFiles(absDir, absDir).map(relPath => ({
path: `${registryPrefix}/${relPath}`.replace(/\\/g, '/'),
type: "registry:lib"
}));
});
return {
name: "base",
type: "registry:lib",
title: "base dependencies",
description: "base dependencies",
dependencies: config.dependencies,
files
};
}
/**
* Generate a special "all" component that depends on all other components
*/
function generateAllComponentEntry(componentNames) {
const depUrls = componentNames.map(name =>
config.componentDepUrlPattern.replace('${name}', name)
);
return {
name: "all",
type: "registry:block",
title: "All Components",
description: "A meta component that links all individual components",
registryDependencies: [config.baseDepUrl, ...depUrls],
files: [] // no files, just a virtual meta component
};
}
/**
* Main function to generate the registry
*/
function generateRegistry() {
console.log('🚀 Generating component registry...');
const { componentDirs, entries: components } = generateComponentEntries();
const baseLibrary = generateBaseLibraryEntry();
const allComponent = generateAllComponentEntry(componentDirs);
console.log(`📦 Found ${componentDirs.length} components: ${componentDirs.join(', ')}`);
const registry = {
$schema: "https://ui.shadcn.com/schema/registry.json",
name: "lift kit",
homepage: "https://liftkit.pages.dev/",
items: [...components, baseLibrary, allComponent]
};
fs.writeFileSync(config.outputFile, JSON.stringify(registry, null, 2));
console.log(`✅ Registry saved to ${config.outputFile}`);
console.log(`📊 Generated ${registry.items.length} total items (${components.length} components + base + all)`);
}
// Execute the generator
generateRegistry();