generate-index.js 2.0 KB

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