generate-index.js 2.0 KB

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