check-lib-imports.js 7.4 KB

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