collectTestCode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const fs = require('fs').promises;
  2. const path = require('path');
  3. async function countLinesInFile(filePath) {
  4. const fileContent = await fs.readFile(filePath, 'utf-8');
  5. const lines = fileContent.split('\n');
  6. return lines.length;
  7. }
  8. async function countTestFilesLines(directoryPath) {
  9. let totalLines = 0;
  10. const folderLineCounts = {};
  11. try {
  12. const files = await fs.readdir(directoryPath);
  13. for (const file of files) {
  14. const filePath = path.join(directoryPath, file);
  15. const stats = await fs.stat(filePath);
  16. if (stats.isDirectory()) {
  17. const testDirectoryPath = path.join(filePath, '__test__');
  18. if (await fs.access(testDirectoryPath).then(() => true).catch(() => false)) {
  19. const testFiles = await fs.readdir(testDirectoryPath);
  20. let folderLines = 0;
  21. for (const testFile of testFiles) {
  22. if (testFile.endsWith('.test.js')) {
  23. const testFilePath = path.join(testDirectoryPath, testFile);
  24. const linesInFile = await countLinesInFile(testFilePath);
  25. folderLines += linesInFile;
  26. }
  27. }
  28. if (folderLines > 0) {
  29. folderLineCounts[file] = folderLines;
  30. totalLines += folderLines;
  31. }
  32. }
  33. }
  34. }
  35. } catch (error) {
  36. console.error(`Error reading directory ${directoryPath}:`, error);
  37. }
  38. return { totalLines, folderLineCounts };
  39. }
  40. (async () => {
  41. const semiUiDirectoryPath = './packages/semi-ui'; // 替换成 semi-ui 文件夹的实际路径
  42. const { totalLines, folderLineCounts } = await countTestFilesLines(semiUiDirectoryPath);
  43. console.log(`Total lines in test files: ${totalLines}`);
  44. console.log('Lines in each folder (sorted):');
  45. // 将文件夹按行数排序
  46. const sortedFolders = Object.entries(folderLineCounts).sort(([, linesA], [, linesB]) => linesB - linesA);
  47. // 输出排序后的结果
  48. for (const [folder, lines] of sortedFolders) {
  49. console.log(` ${folder}: ${lines} lines`);
  50. }
  51. })();