@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
132 lines • 4.01 kB
JavaScript
import { getAppLogger } from "@intlayer/config";
import configuration from "@intlayer/config/built";
import { readFileSync } from "fs";
import { join } from "path";
import simpleGit from "simple-git";
const getGitRootDir = async () => {
try {
const git = simpleGit();
const rootDir = await git.revparse(["--show-toplevel"]);
return rootDir.trim();
} catch (error) {
const appLogger = getAppLogger(configuration);
appLogger("Error getting git root directory:" + error, {
level: "error"
});
return null;
}
};
const listGitFiles = async ({
mode,
baseRef = "origin/main",
currentRef = "HEAD",
// HEAD points to the current branch's latest commit
absolute = true
}) => {
try {
const git = simpleGit();
const diff = /* @__PURE__ */ new Set();
if (mode.includes("untracked")) {
const status = await git.status();
status.not_added.forEach((f) => diff.add(f));
}
if (mode.includes("uncommitted")) {
const uncommittedDiff = await git.diff(["--name-only", "HEAD"]);
const uncommittedFiles = uncommittedDiff.split("\n").filter(Boolean);
uncommittedFiles.forEach((file) => diff.add(file));
}
if (mode.includes("unpushed")) {
const unpushedDiff = await git.diff(["--name-only", "@{push}...HEAD"]);
const unpushedFiles = unpushedDiff.split("\n").filter(Boolean);
unpushedFiles.forEach((file) => diff.add(file));
}
if (mode.includes("gitDiff")) {
await git.fetch(baseRef);
const diffBranch = await git.diff([
"--name-only",
`${baseRef}...${currentRef}`
]);
const gitDiffFiles = diffBranch.split("\n").filter(Boolean);
gitDiffFiles.forEach((file) => diff.add(file));
}
if (absolute) {
const gitRootDir = await getGitRootDir();
if (!gitRootDir) {
return [];
}
return Array.from(diff).map((file) => join(gitRootDir, file));
}
return Array.from(diff);
} catch (error) {
console.warn("Failed to get changes list:", error);
}
};
const listGitLines = async (filePath, {
mode,
baseRef = "origin/main",
currentRef = "HEAD"
// HEAD points to the current branch's latest commit
}) => {
const git = simpleGit();
const changedLines = /* @__PURE__ */ new Set();
const collectLinesFromDiff = (diffOutput) => {
const hunkRegex = /@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/g;
let match;
while ((match = hunkRegex.exec(diffOutput)) !== null) {
const oldCount = match[2] ? Number(match[2]) : 1;
const newStart = Number(match[3]);
const newCount = match[4] ? Number(match[4]) : 1;
if (newCount > 0) {
for (let i = 0; i < newCount; i++) {
changedLines.add(newStart + i);
}
}
if (oldCount > 0 && newCount === 0) {
if (newStart > 1) {
changedLines.add(newStart - 1);
}
changedLines.add(newStart);
}
}
};
if (mode.includes("untracked")) {
const status = await git.status();
const isUntracked = status.not_added.includes(filePath);
if (isUntracked) {
try {
const content = readFileSync(filePath, "utf-8");
content.split("\n").forEach((_, idx) => changedLines.add(idx + 1));
} catch {
}
}
}
if (mode.includes("uncommitted")) {
const diffOutput = await git.diff(["--unified=0", "HEAD", "--", filePath]);
collectLinesFromDiff(diffOutput);
}
if (mode.includes("unpushed")) {
const diffOutput = await git.diff([
"--unified=0",
"@{push}...HEAD",
"--",
filePath
]);
collectLinesFromDiff(diffOutput);
}
if (mode.includes("gitDiff")) {
await git.fetch(baseRef);
const diffOutput = await git.diff([
"--unified=0",
`${baseRef}...${currentRef}`,
"--",
filePath
]);
collectLinesFromDiff(diffOutput);
}
return Array.from(changedLines).sort((a, b) => a - b);
};
export {
listGitFiles,
listGitLines
};
//# sourceMappingURL=listGitFiles.mjs.map