lockfile-guardian
Version:
Never forget to install dependencies again! Automatically detect when your lock files change after git operations and warn you (or auto-install) when your dependencies are out of sync.
145 lines • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PACKAGE_MANAGERS = void 0;
exports.createSHA256Hash = createSHA256Hash;
exports.findLockfile = findLockfile;
exports.isGitRepository = isGitRepository;
exports.getGitHooksDir = getGitHooksDir;
exports.getHuskyHooksDir = getHuskyHooksDir;
exports.getGitHooksPath = getGitHooksPath;
exports.isHuskyProject = isHuskyProject;
exports.getActiveHooksDir = getActiveHooksDir;
exports.getGuardianDataPath = getGuardianDataPath;
exports.loadConfig = loadConfig;
exports.isNodeModulesIgnored = isNodeModulesIgnored;
exports.log = log;
exports.logWarning = logWarning;
exports.logError = logError;
const crypto_1 = require("crypto");
const fs_1 = require("fs");
const path_1 = require("path");
exports.PACKAGE_MANAGERS = [
{ name: "pnpm", lockFile: "pnpm-lock.yaml", installCommand: "pnpm install" },
{ name: "yarn", lockFile: "yarn.lock", installCommand: "yarn install" },
{ name: "npm", lockFile: "package-lock.json", installCommand: "npm install" },
];
function createSHA256Hash(filePath) {
const content = (0, fs_1.readFileSync)(filePath, "utf8");
return (0, crypto_1.createHash)("sha256").update(content).digest("hex");
}
function findLockfile(cwd = process.cwd()) {
for (const pm of exports.PACKAGE_MANAGERS) {
const lockfilePath = (0, path_1.resolve)(cwd, pm.lockFile);
if ((0, fs_1.existsSync)(lockfilePath)) {
const hash = createSHA256Hash(lockfilePath);
return {
path: lockfilePath,
packageManager: pm,
hash,
};
}
}
return null;
}
function isGitRepository(cwd = process.cwd()) {
const gitDir = (0, path_1.join)(cwd, ".git");
try {
return (0, fs_1.existsSync)(gitDir) && (0, fs_1.statSync)(gitDir).isDirectory();
}
catch {
return false;
}
}
function getGitHooksDir(cwd = process.cwd()) {
return (0, path_1.join)(cwd, ".git", "hooks");
}
function getHuskyHooksDir(cwd = process.cwd()) {
return (0, path_1.join)(cwd, ".husky");
}
function getGitHooksPath(cwd = process.cwd()) {
try {
const { execSync } = require("child_process");
const result = execSync("git config --get core.hooksPath", {
cwd,
encoding: "utf8",
stdio: "pipe",
});
return (0, path_1.resolve)(cwd, result.trim());
}
catch {
// No custom hooks path set, use default
return getGitHooksDir(cwd);
}
}
function isHuskyProject(cwd = process.cwd()) {
// Check if .husky directory exists
const huskyDir = getHuskyHooksDir(cwd);
if (!(0, fs_1.existsSync)(huskyDir)) {
return false;
}
// Check if core.hooksPath is set to .husky
try {
const { execSync } = require("child_process");
const hooksPath = execSync("git config --get core.hooksPath", {
cwd,
encoding: "utf8",
stdio: "pipe",
}).trim();
const resolvedHooksPath = (0, path_1.resolve)(cwd, hooksPath);
const resolvedHuskyDir = (0, path_1.resolve)(cwd, ".husky");
return resolvedHooksPath === resolvedHuskyDir;
}
catch {
return false;
}
}
function getActiveHooksDir(cwd = process.cwd()) {
if (isHuskyProject(cwd)) {
return getHuskyHooksDir(cwd);
}
return getGitHooksPath(cwd);
}
function getGuardianDataPath(cwd = process.cwd()) {
return (0, path_1.join)(cwd, ".git", "lockfile-guardian");
}
function loadConfig(cwd = process.cwd()) {
const packageJsonPath = (0, path_1.join)(cwd, "package.json");
if (!(0, fs_1.existsSync)(packageJsonPath)) {
return { checkNodeModules: true };
}
try {
const packageJson = JSON.parse((0, fs_1.readFileSync)(packageJsonPath, "utf8"));
return {
checkNodeModules: true,
...packageJson.lockfileGuardian,
};
}
catch {
return { checkNodeModules: true };
}
}
function isNodeModulesIgnored(cwd = process.cwd()) {
const gitignorePath = (0, path_1.join)(cwd, ".gitignore");
if (!(0, fs_1.existsSync)(gitignorePath)) {
return false;
}
try {
const gitignoreContent = (0, fs_1.readFileSync)(gitignorePath, "utf8");
return gitignoreContent.includes("node_modules");
}
catch {
return false;
}
}
function log(message, silent = false) {
if (!silent) {
console.log(message);
}
}
function logWarning(message) {
console.log(message);
}
function logError(message) {
console.error(message);
}
//# sourceMappingURL=utils.js.map