generate-index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 LIB_DIR = path.join(__dirname, 'src', 'lib');
  8. const INDEX_FILE = path.join(LIB_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 (
  17. file.match(/\.(ts|tsx|js|jsx)$/) &&
  18. !file.endsWith('.d.ts') &&
  19. !file.endsWith('.spec.ts')
  20. ) {
  21. fileList.push(filePath);
  22. }
  23. });
  24. return fileList;
  25. }
  26. function generateExports() {
  27. let exportStatements = [];
  28. TARGET_DIRS.forEach(dir => {
  29. const dirPath = path.join(LIB_DIR, dir);
  30. if (!fs.existsSync(dirPath)) {
  31. console.warn(`Directory ${dirPath} does not exist`);
  32. return;
  33. }
  34. const files = getAllFiles(dirPath);
  35. files.forEach(file => {
  36. const relativePath = path.relative(LIB_DIR, file);
  37. const exportPath = relativePath.replace(/\\/g, '/');
  38. // replace the tsx with js in the export path
  39. const exportPathJs = exportPath.replace(/\.tsx?/, '.js');
  40. // Generate both named and default exports
  41. exportStatements.push(`export * from './${exportPathJs}';`);
  42. });
  43. });
  44. return exportStatements.join('\n');
  45. }
  46. function generateIndexFile() {
  47. const exports = generateExports();
  48. const content = `// This file is auto-generated. Do not edit manually.
  49. ${exports}
  50. `;
  51. fs.writeFileSync(INDEX_FILE, content);
  52. console.log(`Generated ${INDEX_FILE} successfully!`);
  53. }
  54. generateIndexFile();