code-complexity
Version:
Measure the churn/complexity score. Higher values mean hotspots where refactorings should happen.
74 lines (73 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const micromatch = require("micromatch");
const node_child_process_1 = require("node:child_process");
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
class GitHistory {
options;
history;
files;
static build(options) {
return new GitHistory(options);
}
constructor(options) {
this.options = options;
this.history = this.buildHistory();
this.files = this.listFiles();
}
buildGitLogCommand() {
const isWindows = process.platform === "win32";
return [
"git",
`-C ${this.options.directory}`,
`log`,
`--follow`,
// Windows CMD handle quotes differently than linux, this is why we should put empty string as said in:
// https://github.com/git-for-windows/git/issues/3131
`--format=${isWindows ? "" : "''"}`,
`--name-only`,
this.options.since ? `--since="${this.options.since}"` : "",
this.options.until ? `--until="${this.options.until}"` : "",
// Windows CMD handle quotes differently
isWindows ? "*" : "'*'",
]
.filter((s) => s.length > 0)
.join(" ");
}
buildHistory() {
const gitLogCommand = this.buildGitLogCommand();
const stdout = this.executeGitLogCommand(gitLogCommand);
return stdout
.split("\n")
.filter((line) => {
if (line.trim().length === 0) {
return false;
}
if (!this.pathStillExists(line)) {
return false;
}
if (!this.filterMatches(line)) {
return false;
}
return true;
})
.sort();
}
executeGitLogCommand(gitLogCommand) {
return (0, node_child_process_1.execSync)(gitLogCommand, { encoding: "utf8", maxBuffer: 32000000 });
}
listFiles() {
return [...new Set(this.history)];
}
pathStillExists(fileName) {
return (0, node_fs_1.existsSync)((0, node_path_1.resolve)(this.options.directory, fileName));
}
filterMatches(file) {
if (this.options.filter && this.options.filter.length) {
return this.options.filter.every((f) => micromatch.isMatch(file, f));
}
return true;
}
}
exports.default = GitHistory;