Skip to content

Commit

Permalink
Skip files with more than 50000 LOC
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser committed Dec 7, 2023
1 parent 6302025 commit 4caf141
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,32 @@ const IGNORE_DIRS = [
];

const IGNORE_FILE_PATTERN = new RegExp("(conf|test|spec|[.-]min|\\.d)\\.(js|ts|jsx|tsx)$", "i");
const MAX_LOC_IN_FILE = 50000;

const getAllFiles = (dir, extn, files, result, regex) => {
files = files || fs.readdirSync(dir);
result = result || [];
regex = regex || new RegExp(`\\${extn}$`);
function countFileLines(filePath){
return new Promise((resolve, reject) => {
let lineCount = 0;
fs.createReadStream(filePath)
.on("data", (buffer) => {
let idx = -1;
lineCount--; // Because the loop will run once for idx=-1
do {
idx = buffer.indexOf(10, idx+1);
lineCount++;
} while (idx !== -1);
}).on("end", () => {
resolve(lineCount);
}).on("error", reject);
});
};

async function getAllFiles(dir, extn, files, result, regex) {
var allFiles = files || fs.readdirSync(dir);
var allResults = result || [];
var extRegex = regex || new RegExp(`\\${extn}$`);

for (let i = 0; i < files.length; i++) {
const file = files[i];
for (let i = 0; i < allFiles.length; i++) {
const file = allFiles[i];
if (
file.startsWith(".") ||
file.startsWith("__") ||
Expand All @@ -54,16 +72,19 @@ const getAllFiles = (dir, extn, files, result, regex) => {
continue;
}
try {
result = getAllFiles(fileWithDir, extn, fs.readdirSync(fileWithDir), result, regex);
allResults = await getAllFiles(fileWithDir, extn, fs.readdirSync(fileWithDir), allResults, extRegex);
} catch (error) {
}
} else {
if (regex.test(fileWithDir)) {
result.push(fileWithDir);
let lines = await countFileLines(fileWithDir);
if (extRegex.test(fileWithDir)) {
if (lines > MAX_LOC_IN_FILE) {
console.warn(fileWithDir, "more than", MAX_LOC_IN_FILE, "lines of code");
} else allResults.push(fileWithDir);
}
}
}
return result;
return allResults;
};

const babelParserOptions = {
Expand Down

0 comments on commit 4caf141

Please sign in to comment.