UNPKG

scai

Version:

> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.

49 lines (48 loc) 2.31 kB
// utils/fileClassifier.ts import path from 'path'; /** * Determines whether a file is likely to be a *generated* or *bundled* file, * rather than handwritten source code. * * This helps filter out files that shouldn't be analyzed for user-authored logic, * like minified JS bundles, Webpack chunks, TypeScript output, etc. */ export function isGeneratedOrBundledFile(filePath) { const base = path.basename(filePath); /** * Minified file detection: * Matches file names like `something.min.js` or `app.min.ts`. * These are typically compiled output intended for production and are not original source code. */ const isMinified = /\.min\.(js|ts)$/.test(base); /** * Hash-named or chunk file detection: * Matches file names like `bundle.839abc.js`, `chunk.123abc.ts`, or `main-worker.js`. * These are often created by bundlers like Webpack, Vite, or Rollup. */ const isHashNamed = /[-_.](worker|bundle|chunk|[a-f0-9]{6,})\.(js|ts)$/.test(base); /** * Output folder detection: * These folders are commonly used to store compiled or bundled output. * Examples: `dist/`, `build/`, `assets/`, `node_modules/`, `plugins/` * * If a file is inside any of these folders, we consider it generated/bundled. */ const outputDirs = ['dist', 'build', 'assets', 'node_modules', 'plugins']; const isInKnownOutputFolder = outputDirs.some(dir => new RegExp(`[\\\\/]${dir}[\\\\/]`, 'i').test(filePath)); /** * Special case: `lib/` folder * The `lib` folder may contain either handwritten code or compiled output, depending on the project. * To avoid over-filtering, we only treat files in `lib/` as generated if they also look minified or hashed. */ const isInLib = /[\\/]lib[\\/]/i.test(filePath); const isLikelyBundledLib = isInLib && (isMinified || isHashNamed); /** * Return true if *any* of the following conditions are met: * - The file looks minified (e.g., `.min.js`) * - The file has a hash or is a known bundle/chunk/worker * - The file is located in a known output directory * - The file is in `lib/` and has signs of being generated (minified or hashed) */ return isMinified || isHashNamed || isInKnownOutputFolder || isLikelyBundledLib; }