UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

167 lines 5.84 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var listGitFiles_exports = {}; __export(listGitFiles_exports, { listGitFiles: () => listGitFiles, listGitLines: () => listGitLines }); module.exports = __toCommonJS(listGitFiles_exports); var import_config = require("@intlayer/config"); var import_built = __toESM(require("@intlayer/config/built")); var import_fs = require("fs"); var import_path = require("path"); var import_simple_git = __toESM(require("simple-git")); const getGitRootDir = async () => { try { const git = (0, import_simple_git.default)(); const rootDir = await git.revparse(["--show-toplevel"]); return rootDir.trim(); } catch (error) { const appLogger = (0, import_config.getAppLogger)(import_built.default); 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 = (0, import_simple_git.default)(); 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) => (0, import_path.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 = (0, import_simple_git.default)(); 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 = (0, import_fs.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); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { listGitFiles, listGitLines }); //# sourceMappingURL=listGitFiles.cjs.map