generate-index.js 2.1 KB

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