chore: cleanup

This commit is contained in:
Ephraim Atta-Duncan
2025-10-20 11:38:09 +00:00
parent 7868527fa0
commit af492cbaaa

View File

@ -3,10 +3,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const { execFileSync } = require('child_process'); const { execFileSync } = require('child_process');
/**
* Check if we're running in CI or if Husky is disabled
* This shouldn't happen since Husky already checks, but defensive
*/
const isCI = process.env.CI === 'true' || process.env.CI === '1'; const isCI = process.env.CI === 'true' || process.env.CI === '1';
const isHuskyDisabled = process.env.HUSKY === '0'; const isHuskyDisabled = process.env.HUSKY === '0';
@ -15,12 +11,8 @@ if (isCI || isHuskyDisabled) {
process.exit(0); process.exit(0);
} }
/**
* Get list of staged .po files in translations directory
*/
const getStagedPoFiles = () => { const getStagedPoFiles = () => {
try { try {
// Use -z flag for null-terminated output to handle special characters in filenames
const output = execFileSync( const output = execFileSync(
'git', 'git',
['diff', '--cached', '--name-only', '-z', '--diff-filter=ACMR'], ['diff', '--cached', '--name-only', '-z', '--diff-filter=ACMR'],
@ -30,42 +22,31 @@ const getStagedPoFiles = () => {
}, },
); );
// Split on null character instead of newline for proper special character handling
const stagedFiles = output.split('\0').filter(Boolean); const stagedFiles = output.split('\0').filter(Boolean);
// Filter for .po files in the translations directory (using startsWith for precise matching)
return stagedFiles.filter((file) => { return stagedFiles.filter((file) => {
return file.startsWith('packages/lib/translations/') && file.endsWith('.po'); return file.startsWith('packages/lib/translations/') && file.endsWith('.po');
}); });
} catch (error) { } catch (error) {
// Fail open: if git command fails, don't block commits
// This prevents the hook from breaking the entire commit flow
console.error('Warning: Could not check staged files:', error.message); console.error('Warning: Could not check staged files:', error.message);
return []; return [];
} }
}; };
/**
* Unstage the specified files
* Handles both normal commits and initial commits (where HEAD doesn't exist)
*/
const unstageFiles = (files) => { const unstageFiles = (files) => {
if (files.length === 0) return; if (files.length === 0) return;
try { try {
// Check if HEAD exists (not an initial commit)
try { try {
execFileSync('git', ['rev-parse', '--verify', 'HEAD'], { execFileSync('git', ['rev-parse', '--verify', 'HEAD'], {
encoding: 'utf8', encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'], stdio: ['pipe', 'pipe', 'pipe'],
}); });
// HEAD exists, use normal reset
execFileSync('git', ['reset', 'HEAD', '--', ...files], { execFileSync('git', ['reset', 'HEAD', '--', ...files], {
encoding: 'utf8', encoding: 'utf8',
stdio: 'inherit', stdio: 'inherit',
}); });
} catch (headError) { } catch (headError) {
// Initial commit - HEAD doesn't exist, use git rm --cached instead
execFileSync('git', ['rm', '--cached', '--', ...files], { execFileSync('git', ['rm', '--cached', '--', ...files], {
encoding: 'utf8', encoding: 'utf8',
stdio: 'inherit', stdio: 'inherit',
@ -76,16 +57,13 @@ const unstageFiles = (files) => {
} }
}; };
// Main execution
const main = () => { const main = () => {
const poFiles = getStagedPoFiles(); const poFiles = getStagedPoFiles();
if (poFiles.length > 0) { if (poFiles.length > 0) {
// Silently unstage .po files and let commit proceed with remaining files
unstageFiles(poFiles); unstageFiles(poFiles);
} }
// Allow commit to proceed (with or without .po files unstaged)
process.exit(0); process.exit(0);
}; };