Files
jj-command-line/helpers/saveFileIfItChanged.ts
T
2024-11-10 01:06:11 -05:00

21 lines
690 B
TypeScript

import fs from 'fs-extra';
export const saveFileIfItChanged = (newContent, filePath) => {
try {
// Check if file exists and read the current content
if (fs.existsSync(filePath)) {
const currentContent = fs.readFileSync(filePath, 'utf-8');
// Compare current content with new content
if (currentContent === newContent) {
return; // Exit function if content is the same
}
}
// Write new content to the file if it is different or file doesn't exist
fs.writeFileSync(filePath, newContent, 'utf-8');
} catch (error) {
console.error('Error handling file:', error);
}
}