check-lib-imports.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env node
  2. import fs from 'fs';
  3. import path from 'path';
  4. import process from 'process';
  5. import { fileURLToPath } from 'url';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = path.dirname(__filename);
  8. // Normalize paths to ensure cross-platform compatibility
  9. const normalizePath = p => path.normalize(p);
  10. // Check if we're running from the dashboard directory or root directory
  11. const currentDir = normalizePath(process.cwd());
  12. const isDashboardDir = currentDir.endsWith(normalizePath('packages/dashboard'));
  13. const HOOKS_DIR = isDashboardDir
  14. ? normalizePath(path.join(__dirname, '../src/lib/hooks'))
  15. : normalizePath(path.join(currentDir, 'packages/dashboard/src/lib/hooks'));
  16. const DASHBOARD_SRC_DIR = isDashboardDir
  17. ? normalizePath(path.join(__dirname, '../src'))
  18. : normalizePath(path.join(currentDir, 'packages/dashboard/src'));
  19. // Required prefix for imports in hook files
  20. const REQUIRED_PREFIX = '@/vdb';
  21. // Banned import pattern
  22. const BANNED_IMPORT = '@/vdb/index.js';
  23. function findHookFiles(dir) {
  24. const files = [];
  25. const items = fs.readdirSync(dir);
  26. for (const item of items) {
  27. const fullPath = normalizePath(path.join(dir, item));
  28. const stat = fs.statSync(fullPath);
  29. if (stat.isFile() && item.startsWith('use-') && (item.endsWith('.ts') || item.endsWith('.tsx'))) {
  30. files.push(fullPath);
  31. }
  32. }
  33. return files;
  34. }
  35. function findDashboardFiles(dir) {
  36. const files = [];
  37. function scanDirectory(currentDir) {
  38. const items = fs.readdirSync(currentDir);
  39. for (const item of items) {
  40. const fullPath = normalizePath(path.join(currentDir, item));
  41. const stat = fs.statSync(fullPath);
  42. if (stat.isDirectory()) {
  43. if (!['node_modules', '.git', 'dist', 'build', '.next'].includes(item)) {
  44. scanDirectory(fullPath);
  45. }
  46. } else if (stat.isFile() && (item.endsWith('.ts') || item.endsWith('.tsx'))) {
  47. files.push(fullPath);
  48. }
  49. }
  50. }
  51. scanDirectory(dir);
  52. return files;
  53. }
  54. function checkFileForBadImports(filePath) {
  55. const content = fs.readFileSync(filePath, 'utf8');
  56. const lines = content.split('\n');
  57. const badImports = [];
  58. for (let i = 0; i < lines.length; i++) {
  59. const line = lines[i];
  60. const trimmedLine = line.trim();
  61. if (trimmedLine.startsWith('import')) {
  62. if (trimmedLine.includes('../')) {
  63. badImports.push({
  64. line: i + 1,
  65. content: trimmedLine,
  66. reason: 'Relative imports going up directories (../) are not allowed in hook files',
  67. });
  68. }
  69. if (trimmedLine.includes('@/') && !trimmedLine.includes(REQUIRED_PREFIX)) {
  70. badImports.push({
  71. line: i + 1,
  72. content: trimmedLine,
  73. reason: `Import must start with ${REQUIRED_PREFIX}`,
  74. });
  75. }
  76. }
  77. }
  78. return badImports;
  79. }
  80. function checkFileForBannedImports(filePath) {
  81. const content = fs.readFileSync(filePath, 'utf8');
  82. const lines = content.split('\n');
  83. const badImports = [];
  84. for (let i = 0; i < lines.length; i++) {
  85. const line = lines[i];
  86. const trimmedLine = line.trim();
  87. if (trimmedLine.startsWith('import') && trimmedLine.includes(BANNED_IMPORT)) {
  88. badImports.push({
  89. line: i + 1,
  90. content: trimmedLine,
  91. reason: `Import from '${BANNED_IMPORT}' is not allowed anywhere in the dashboard app`,
  92. });
  93. }
  94. }
  95. return badImports;
  96. }
  97. function main() {
  98. console.log('🔍 Checking for import patterns in the dashboard app...\n');
  99. console.log('📁 Checking hook files (use-*.ts/tsx) in src/lib/hooks directory...');
  100. console.log('✅ Hook file requirements:');
  101. console.log(` - All imports must start with ${REQUIRED_PREFIX}`);
  102. console.log(' - Relative imports going up directories (../) are not allowed');
  103. console.log(' - Relative imports in same directory (./) are allowed');
  104. console.log('');
  105. if (!fs.existsSync(HOOKS_DIR)) {
  106. console.error('❌ src/lib/hooks directory not found!');
  107. process.exit(1);
  108. }
  109. const hookFiles = findHookFiles(HOOKS_DIR);
  110. let hasBadImports = false;
  111. let totalBadImports = 0;
  112. for (const file of hookFiles) {
  113. const relativePath = normalizePath(path.relative(process.cwd(), file));
  114. const badImports = checkFileForBadImports(file);
  115. if (badImports.length > 0) {
  116. hasBadImports = true;
  117. totalBadImports += badImports.length;
  118. console.log(`❌ ${relativePath}:`);
  119. for (const badImport of badImports) {
  120. console.log(` Line ${badImport.line}: ${badImport.content}`);
  121. console.log(` Reason: ${badImport.reason}`);
  122. }
  123. console.log('');
  124. }
  125. }
  126. if (hasBadImports) {
  127. console.log(`❌ Found ${totalBadImports} bad import(s) in ${hookFiles.length} hook file(s)`);
  128. console.log(
  129. `💡 All imports in hook files must start with ${REQUIRED_PREFIX} and must not use relative paths going up directories`,
  130. );
  131. } else {
  132. console.log(`✅ No bad imports found in ${hookFiles.length} hook file(s)`);
  133. console.log(`🎉 All imports in hook files are using ${REQUIRED_PREFIX} prefix`);
  134. }
  135. console.log('\n📁 Checking all dashboard files for banned imports...');
  136. console.log('✅ Dashboard-wide requirements:');
  137. console.log(` - Import from '${BANNED_IMPORT}' is not allowed anywhere`);
  138. console.log('');
  139. if (!fs.existsSync(DASHBOARD_SRC_DIR)) {
  140. console.error('❌ src directory not found!');
  141. process.exit(1);
  142. }
  143. const dashboardFiles = findDashboardFiles(DASHBOARD_SRC_DIR);
  144. let hasBannedImports = false;
  145. let totalBannedImports = 0;
  146. for (const file of dashboardFiles) {
  147. const relativePath = normalizePath(path.relative(process.cwd(), file));
  148. const bannedImports = checkFileForBannedImports(file);
  149. if (bannedImports.length > 0) {
  150. hasBannedImports = true;
  151. totalBannedImports += bannedImports.length;
  152. console.log(`❌ ${relativePath}:`);
  153. for (const bannedImport of bannedImports) {
  154. console.log(` Line ${bannedImport.line}: ${bannedImport.content}`);
  155. console.log(` Reason: ${bannedImport.reason}`);
  156. }
  157. console.log('');
  158. }
  159. }
  160. if (hasBannedImports) {
  161. console.log(
  162. `❌ Found ${totalBannedImports} banned import(s) in ${dashboardFiles.length} dashboard file(s)`,
  163. );
  164. console.log(`💡 Import from '${BANNED_IMPORT}' is not allowed anywhere in the dashboard app`);
  165. } else {
  166. console.log(`✅ No banned imports found in ${dashboardFiles.length} dashboard file(s)`);
  167. console.log(`🎉 All dashboard files are free of banned imports`);
  168. }
  169. if (hasBadImports || hasBannedImports) {
  170. process.exit(1);
  171. }
  172. }
  173. main();