UNPKG

code-complexity

Version:

Measure the churn/complexity score. Higher values mean hotspots where refactorings should happen.

37 lines (36 loc) 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const sloc = require("node-sloc"); const node_fs_1 = require("node:fs"); async function compute(absolutePath) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const result = await sloc({ path: absolutePath }); if (result?.sloc) { return result?.sloc; } else { return countLineNumber(absolutePath); } } exports.default = compute; async function countLineNumber(absolutePath) { const ASCII_FOR_NEW_LINE = "10"; const ASCII_FOR_CARRIAGE_RETURN = "13"; let count = 0; return new Promise((resolve) => { (0, node_fs_1.createReadStream)(absolutePath) .on("data", function (chunk) { for (let i = 0; i < chunk.length; ++i) { const char = chunk[i]; // Use "==" instead of "===" to use type coercion if (char == ASCII_FOR_NEW_LINE || char == ASCII_FOR_CARRIAGE_RETURN) { count += 1; } } }) .on("end", function () { resolve(count); }); }); }