claude-git-hooks
Version:
Git hooks with Claude CLI for code analysis and automatic commit messages
66 lines (57 loc) • 1.87 kB
JavaScript
/**
* File: file-utils.js
* Purpose: Utility functions for file system operations
*/
import fs from 'fs/promises';
import fsSync from 'fs';
import path from 'path';
import { getRepoRoot } from './git-operations.js';
import logger from './logger.js';
/**
* Ensures a directory exists (creates if not present)
*
* @param {string} dirPath - Directory path to ensure
* @returns {Promise<void>}
*/
export const ensureDir = async (dirPath) => {
const absolutePath = path.isAbsolute(dirPath)
? dirPath
: path.join(getRepoRoot(), dirPath);
try {
await fs.mkdir(absolutePath, { recursive: true });
logger.debug('file-utils - ensureDir', 'Directory ensured', { path: absolutePath });
} catch (error) {
logger.error('file-utils - ensureDir', 'Failed to create directory', error);
throw error;
}
};
/**
* Ensures the output directory exists before writing files
* Creates .claude/out/ if it doesn't exist
*
* @param {Object} config - Configuration object with output.outputDir
* @returns {Promise<void>}
*/
export const ensureOutputDir = async (config) => {
const outputDir = config?.output?.outputDir || '.claude/out';
await ensureDir(outputDir);
};
/**
* Writes a file ensuring its directory exists
*
* @param {string} filePath - File path to write
* @param {string} content - File content
* @param {Object} config - Configuration object
* @returns {Promise<void>}
*/
export const writeOutputFile = async (filePath, content, config) => {
await ensureOutputDir(config);
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.join(getRepoRoot(), filePath);
await fs.writeFile(absolutePath, content, 'utf8');
logger.debug('file-utils - writeOutputFile', 'File written', {
path: absolutePath,
size: content.length
});
};