Add debug logging to file moving functionality

This commit is contained in:
Jarian Cottingham 2026-02-21 15:58:08 -06:00
parent b4a23b5fd7
commit ce07fdbb74

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 // IPC handler for moving file to folder
ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) => { ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) => {
try { try {
console.log('Move file request received:', { filePath, folderName }); writeLog(`Move file request received: ${filePath} to ${folderName}`);
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -129,14 +141,14 @@ ipcMain.handle('move-file-to-folder', async (event, { filePath, folderName }) =>
// Check if file exists // Check if file exists
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
const error = 'File does not exist'; const error = 'File does not exist';
console.error(error); writeLog(`ERROR: ${error}`);
return { success: false, error: error }; return { success: false, error: error };
} }
// Validate folder name // Validate folder name
if (folderName !== 'extra' && folderName !== 'commentary') { if (folderName !== 'extra' && folderName !== 'commentary') {
const error = 'Invalid folder name. Must be "extra" or "commentary"'; const error = 'Invalid folder name. Must be "extra" or "commentary"';
console.error(error); writeLog(`ERROR: ${error}`);
return { success: false, 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 fileDir = path.dirname(filePath);
const targetFolder = path.join(fileDir, folderName); const targetFolder = path.join(fileDir, folderName);
console.log('Moving file from:', filePath); writeLog(`Moving file from: ${filePath}`);
console.log('Target folder:', targetFolder); writeLog(`Target folder: ${targetFolder}`);
// Create the target folder if it doesn't exist // Create the target folder if it doesn't exist
if (!fs.existsSync(targetFolder)) { if (!fs.existsSync(targetFolder)) {
console.log('Creating target folder:', targetFolder); writeLog(`Creating target folder: ${targetFolder}`);
fs.mkdirSync(targetFolder, { recursive: true }); 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 fileName = path.basename(filePath);
const newFilePath = path.join(targetFolder, fileName); 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 // Check if file with same name already exists in target folder
if (fs.existsSync(newFilePath)) { if (fs.existsSync(newFilePath)) {
const error = 'File with same name already exists in target folder'; const error = 'File with same name already exists in target folder';
console.error(error); writeLog(`ERROR: ${error}`);
return { success: false, error: error }; return { success: false, error: error };
} }
// Move the file // Move the file
console.log('Moving file...'); writeLog('Moving file...');
fs.renameSync(filePath, newFilePath); 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` }; return { success: true, message: `File moved to ${folderName} folder successfully` };
} catch (error) { } 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 }; return { success: false, error: error.message };
} }
}); });