Add debug logging to file moving functionality

This commit is contained in:
Jarian Cottingham 2026-02-21 15:58:08 -06:00
parent c2cd8986d0
commit 5b3f72e7ee

35
main.js
View File

@ -118,10 +118,22 @@ ipcMain.handle('rename-file', async (event, { oldPath, newName }) => {
}
});
// Create log file for debugging
const fs = require('fs');
const path = require('path');
const logFilePath = path.join(__dirname, 'file-move-debug.log');
function writeLog(message) {
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] ${message}\n`;
fs.appendFileSync(logFilePath, logEntry);
console.log(logEntry.trim());
}
// IPC handler for moving file to folder
ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) => {
try {
console.log('Move file request received:', { filePath, folderName });
writeLog(`Move file request received: ${filePath} to ${folderName}`);
const fs = require('fs');
const path = require('path');
@ -129,14 +141,14 @@ ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) =>
// Check if file exists
if (!fs.existsSync(filePath)) {
const error = 'File does not exist';
console.error(error);
writeLog(`ERROR: ${error}`);
return { success: false, error: error };
}
// Validate folder name
if (folderName !== 'extra' && folderName !== 'commentary') {
const error = 'Invalid folder name. Must be "extra" or "commentary"';
console.error(error);
writeLog(`ERROR: ${error}`);
return { success: false, error: error };
}
@ -144,12 +156,12 @@ ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) =>
const fileDir = path.dirname(filePath);
const targetFolder = path.join(fileDir, folderName);
console.log('Moving file from:', filePath);
console.log('Target folder:', targetFolder);
writeLog(`Moving file from: ${filePath}`);
writeLog(`Target folder: ${targetFolder}`);
// Create the target folder if it doesn't exist
if (!fs.existsSync(targetFolder)) {
console.log('Creating target folder:', targetFolder);
writeLog(`Creating target folder: ${targetFolder}`);
fs.mkdirSync(targetFolder, { recursive: true });
}
@ -157,23 +169,24 @@ ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) =>
const fileName = path.basename(filePath);
const newFilePath = path.join(targetFolder, fileName);
console.log('New file path:', newFilePath);
writeLog(`New file path: ${newFilePath}`);
// Check if file with same name already exists in target folder
if (fs.existsSync(newFilePath)) {
const error = 'File with same name already exists in target folder';
console.error(error);
writeLog(`ERROR: ${error}`);
return { success: false, error: error };
}
// Move the file
console.log('Moving file...');
writeLog('Moving file...');
fs.renameSync(filePath, newFilePath);
console.log('File moved successfully to:', newFilePath);
writeLog(`File moved successfully to: ${newFilePath}`);
return { success: true, message: `File moved to ${folderName} folder successfully` };
} catch (error) {
console.error('Error in move-file-to-folder:', error);
writeLog(`CRITICAL ERROR in move-file-to-folder: ${error.message}`);
writeLog(`Stack trace: ${error.stack}`);
return { success: false, error: error.message };
}
});