From 1fe7af4fadd4b337cb7ab00b40d0edf1a8eaa78e Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Tue, 1 Oct 2024 15:00:55 +0530 Subject: [PATCH] add tempDir logic to gracefully rename the parent folder in a collection. fix: `EPERM` --- packages/bruno-electron/src/ipc/collection.js | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 0aa2058c2..ff6c91e05 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -1,5 +1,7 @@ const _ = require('lodash'); const fs = require('fs'); +const fsExtra = require('fs-extra'); +const os = require('os'); const path = require('path'); const { ipcMain, shell, dialog, app } = require('electron'); const { envJsonToBru, bruToJson, jsonToBru, jsonToCollectionBru } = require('../bru'); @@ -17,7 +19,8 @@ const { isWSLPath, normalizeWslPath, normalizeAndResolvePath, - safeToRename + safeToRename, + isWindowsOS } = require('../utils/filesystem'); const { openCollectionDialog } = require('../app/collections'); const { generateUidBasedOnHash, stringifyJson, safeParseJSON, safeStringifyJSON } = require('../utils/common'); @@ -352,23 +355,31 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection const newBruFilePath = bruFile.replace(oldPath, newPath); moveRequestUid(bruFile, newBruFilePath); } - return fs.renameSync(oldPath, newPath); + + if (isWindowsOS() && !isWSLPath(oldPath)) { + const tempDir = path.join(os.tmpdir(), `temp-folder-${Date.now()}`); + + await fsExtra.copy(oldPath, tempDir); + await fsExtra.move(tempDir, newPath, { overwrite: true }); + await fsExtra.remove(oldPath); + } else { + await fs.rename(oldPath, newPath); + } + return newPath; } - const isBru = hasBruExtension(oldPath); - if (!isBru) { + if (!hasBruExtension(oldPath)) { throw new Error(`path: ${oldPath} is not a bru file`); } // update name in file and save new copy, then delete old copy - const data = fs.readFileSync(oldPath, 'utf8'); + const data = await fs.promises.readFile(oldPath, 'utf8'); // Use async read const jsonData = bruToJson(data); - jsonData.name = newName; moveRequestUid(oldPath, newPath); const content = jsonToBru(jsonData); - await fs.unlinkSync(oldPath); + await fs.promises.unlink(oldPath); await writeFile(newPath, content); return newPath;