generate-index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { fileURLToPath } from 'url';
  4. const __filename = fileURLToPath(import.meta.url);
  5. const __dirname = path.dirname(__filename);
  6. const TARGET_DIRS = ['components', 'framework', 'hooks', 'lib'];
  7. const SRC_DIR = path.join(__dirname, 'src');
  8. const INDEX_FILE = path.join(SRC_DIR, 'index.ts');
  9. function getAllFiles(dir, fileList = []) {
  10. const files = fs.readdirSync(dir);
  11. files.forEach(file => {
  12. const filePath = path.join(dir, file);
  13. const stat = fs.statSync(filePath);
  14. if (stat.isDirectory()) {
  15. getAllFiles(filePath, fileList);
  16. } else if (file.match(/\.(ts|tsx|js|jsx)$/) && !file.endsWith('.d.ts') && !file.endsWith('.spec.ts')) {
  17. fileList.push(filePath);
  18. }
  19. });
  20. return fileList;
  21. }
  22. function generateExports() {
  23. let exportStatements = [];
  24. TARGET_DIRS.forEach(dir => {
  25. const dirPath = path.join(SRC_DIR, dir);
  26. if (!fs.existsSync(dirPath)) {
  27. console.warn(`Directory ${dirPath} does not exist`);
  28. return;
  29. }
  30. const files = getAllFiles(dirPath);
  31. files.forEach(file => {
  32. const relativePath = path.relative(SRC_DIR, file);
  33. const exportPath = relativePath.replace(/\\/g, '/');
  34. // replace the tsx with js in the export path
  35. const exportPathJs = exportPath.replace(/\.tsx?/, '.js');
  36. // Generate both named and default exports
  37. exportStatements.push(`export * from './${exportPathJs}';`);
  38. });
  39. });
  40. return exportStatements.join('\n');
  41. }
  42. function generateIndexFile() {
  43. const exports = generateExports();
  44. const content = `// This file is auto-generated. Do not edit manually.
  45. // Generated on: ${new Date().toISOString()}
  46. ${exports}
  47. `;
  48. fs.writeFileSync(INDEX_FILE, content);
  49. console.log(`Generated ${INDEX_FILE} successfully!`);
  50. }
  51. generateIndexFile();