cloc-graph
Version:
Track lines of code over time by language with visualization
47 lines (46 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeCommit = analyzeCommit;
exports.createRecord = createRecord;
/**
* CLOC service for analyzing code in Git repositories
*/
const child_process_1 = require("child_process");
const dateUtils_1 = require("../utils/dateUtils");
/**
* Run CLOC on a specific commit
*
* @param commitHash - Git commit hash to analyze
* @returns CLOC data with code statistics by language
*/
function analyzeCommit(commitHash) {
try {
const output = (0, child_process_1.execSync)(`cloc --quiet --json --git ${commitHash}`, {
encoding: "utf-8",
});
return JSON.parse(output);
}
catch (e) {
console.warn(`Warning: Failed to run cloc on commit ${commitHash}: ${e}`);
return {};
}
}
/**
* Convert CLOC data to a record with language statistics
*
* @param data - CLOC data from analysis
* @param date - Date of the commit
* @param languages - Set of languages to update with newly found languages
* @returns Record object with date and language stats
*/
function createRecord(data, date, languages) {
const dateStr = (0, dateUtils_1.formatDate)(date);
const rec = { date: dateStr };
for (const [lang, stats] of Object.entries(data)) {
if (lang === "SUM")
continue;
rec[lang] = stats.code;
languages.add(lang);
}
return rec;
}