From 5012685aa9061e8ab966f1ed9d89d97661dccdf7 Mon Sep 17 00:00:00 2001 From: Joel Date: Tue, 2 Dec 2025 16:50:07 +0800 Subject: [PATCH] feat: detect all files --- web/.gitignore | 1 + web/calculate-complexity.js | 63 ++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/web/.gitignore b/web/.gitignore index 048c5f6485..24e80a566d 100644 --- a/web/.gitignore +++ b/web/.gitignore @@ -54,3 +54,4 @@ package-lock.json # mise mise.toml +complexity-report.csv diff --git a/web/calculate-complexity.js b/web/calculate-complexity.js index 3d7666e286..3b95254edb 100644 --- a/web/calculate-complexity.js +++ b/web/calculate-complexity.js @@ -1,5 +1,6 @@ // https://www.sonarsource.com/blog/5-clean-code-tips-for-reducing-cognitive-complexity/ const fs = require('fs'); +const path = require('path'); const { Linter } = require('eslint'); const sonarPlugin = require('eslint-plugin-sonarjs'); const tsParser = require('@typescript-eslint/parser'); @@ -59,7 +60,7 @@ function getFileComplexity(filePath) { return { file: filePath, totalComplexity: totalFileComplexity, - maxFileComplexity: functionComplexities[maxFileComplexityIndex], + maxComplexityInfo: functionComplexities[maxFileComplexityIndex], details: functionComplexities }; @@ -69,11 +70,59 @@ function getFileComplexity(filePath) { } } -// const targetFile = './app/components/share/text-generation/run-once/index.tsx'; // "totalComplexity": 22 -const targetFile = './app/components/share/text-generation/index.tsx'; // 90, max: 41 -// const targetFile = './app/components/workflow/nodes/_base/components/workflow-panel/index.tsx'; // 33 -// const targetFile = './app/components/app/configuration/index.tsx'; // 111, max: 26 +function collectTsxFiles(baseDir) { + const entries = fs.readdirSync(baseDir, { withFileTypes: true }); + const files = []; + entries.forEach((entry) => { + const fullPath = path.join(baseDir, entry.name); -const result = getFileComplexity(targetFile); -console.log(JSON.stringify(result, null, 2)); + if (entry.isDirectory()) { + if ( + entry.name === 'node_modules' || + entry.name.startsWith('.') || + entry.name === '__test__' || + entry.name === '__tests__' + ) { + return; + } + files.push(...collectTsxFiles(fullPath)); + } else if ( + entry.isFile() && + entry.name.endsWith('.tsx') && + !entry.name.endsWith('.spec.tsx') && + !entry.name.endsWith('.test.tsx') + ) { + files.push(fullPath); + } + }); + + return files; +} + +function writeCsv(results, outputPath) { + const header = 'File,Total Complexity,Max Complexity,Max Complexity Line'; + const rows = results.map(({ file, totalComplexity, maxComplexityInfo }) => { + const maxScore = maxComplexityInfo?.score ?? 0; + const maxLine = maxComplexityInfo?.line ?? ''; + const targetFile = JSON.stringify(path.relative(process.cwd(), file)); + return `${targetFile},${totalComplexity},${maxScore},${maxLine}`; + }); + + fs.writeFileSync(outputPath, [header, ...rows].join('\n'), 'utf8'); +} + +function main() { + const projectRoot = process.cwd(); + const tsxFiles = collectTsxFiles(projectRoot); + const results = tsxFiles + .map(getFileComplexity) + .filter((item) => item && item.totalComplexity > 0) + .sort((a, b) => b.maxComplexityInfo?.score - a.maxComplexityInfo?.score); + + const outputPath = path.join(projectRoot, 'complexity-report.csv'); + writeCsv(results, outputPath); + console.log(`CSV report written to ${outputPath}`); +} + +main();