claude-git-hooks
Version:
Git hooks with Claude CLI for code analysis and automatic commit messages
76 lines (66 loc) • 2.08 kB
JavaScript
/**
* File: package-info.js
* Purpose: Utility for reading package.json information
*/
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* @typedef {Object} PackageData
* @property {string} name - Package name
* @property {string} version - Package version
* @property {string} [description] - Package description
*/
/**
* Cache for package.json to avoid repeated file reads
* @type {PackageData|undefined}
*/
let packageCache;
/**
* Gets package.json data with caching (async)
* @returns {Promise<PackageData>} Package data with name and version
* @throws {Error} If package.json cannot be read
*/
export const getPackageJson = async () => {
// Return cached value if available
if (packageCache) {
return packageCache;
}
try {
const packagePath = path.join(__dirname, '..', '..', 'package.json');
const content = await fs.readFile(packagePath, 'utf8');
packageCache = JSON.parse(content);
return packageCache;
} catch (error) {
throw new Error(`Failed to read package.json: ${error.message}`);
}
};
/**
* Gets package version (async)
* @returns {Promise<string>} Package version
*/
export const getVersion = async () => {
const pkg = await getPackageJson();
return pkg.version;
};
/**
* Gets package name (async)
* @returns {Promise<string>} Package name
*/
export const getPackageName = async () => {
const pkg = await getPackageJson();
return pkg.name;
};
/**
* Calculates batch information for subagent processing
* @param {number} fileCount - Number of files to process
* @param {number} batchSize - Size of each batch
* @returns {Object} Batch information { numBatches, shouldShowBatches }
*/
export const calculateBatches = (fileCount, batchSize) => {
const numBatches = Math.ceil(fileCount / batchSize);
const shouldShowBatches = fileCount > batchSize;
return { numBatches, shouldShowBatches };
};