From 0b9554c8cc2f45aa25a1a18fb1adc762c3caf7b7 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 <54320162+Pragadesh-45@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:36:54 +0530 Subject: [PATCH] Fix/rename-collection-support-wsl (#2892) * fix: normalize wsl path for rename item * added isWSLPath, normalizeWslPath * revert normalize action on actions.js * added WSL path checking and apply Win UNC normalize --- .../ReduxStore/slices/collections/actions.js | 3 ++- packages/bruno-electron/src/ipc/collection.js | 12 +++++++++++- packages/bruno-electron/src/utils/filesystem.js | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index dd0aaf72d..054b4fbd4 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -374,6 +374,7 @@ export const newFolder = (folderName, collectionUid, itemUid) => (dispatch, getS }); }; +// rename item export const renameItem = (newName, itemUid, collectionUid) => (dispatch, getState) => { const state = getState(); const collection = findCollectionByUid(state.collections.collections, collectionUid); @@ -718,7 +719,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => { const pathParams = parsePathParams(requestUrl); each(pathParams, (pathParm) => { pathParams.enabled = true; - pathParm.type = 'path' + pathParm.type = 'path'; }); const params = [...queryParams, ...pathParams]; diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 82453ec66..945c21559 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -13,7 +13,9 @@ const { browseFiles, createDirectory, searchForBruFiles, - sanitizeDirectoryName + sanitizeDirectoryName, + isWSLPath, + normalizeWslPath, } = require('../utils/filesystem'); const { openCollectionDialog } = require('../app/collections'); const { generateUidBasedOnHash, stringifyJson, safeParseJSON, safeStringifyJSON } = require('../utils/common'); @@ -326,6 +328,14 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection // rename item ipcMain.handle('renderer:rename-item', async (event, oldPath, newPath, newName) => { try { + // Normalize paths if they are WSL paths + if (isWSLPath(oldPath)) { + oldPath = normalizeWslPath(oldPath); + } + if (isWSLPath(newPath)) { + newPath = normalizeWslPath(newPath); + } + if (!fs.existsSync(oldPath)) { throw new Error(`path: ${oldPath} does not exist`); } diff --git a/packages/bruno-electron/src/utils/filesystem.js b/packages/bruno-electron/src/utils/filesystem.js index 8216bd9c9..752cb339c 100644 --- a/packages/bruno-electron/src/utils/filesystem.js +++ b/packages/bruno-electron/src/utils/filesystem.js @@ -50,6 +50,18 @@ const normalizeAndResolvePath = (pathname) => { return path.resolve(pathname); }; +function isWSLPath(pathname) { + // Check if the path starts with the WSL prefix + // eg. "\\wsl.localhost\Ubuntu\home\user\bruno\collection\scripting\api\req\getHeaders.bru" + return pathname.startsWith('/wsl.localhost/') || pathname.startsWith('\\wsl.localhost\\'); +} + +function normalizeWslPath(pathname) { + // Replace the WSL path prefix and convert forward slashes to backslashes + // This is done to achieve WSL paths (linux style) to Windows UNC equivalent (Universal Naming Conversion) + return pathname.replace(/^\/wsl.localhost/, '\\\\wsl.localhost').replace(/\//g, '\\'); +} + const writeFile = async (pathname, content) => { try { fs.writeFileSync(pathname, content, { @@ -143,6 +155,8 @@ const searchForBruFiles = (dir) => { return searchForFiles(dir, '.bru'); }; +// const isW + const sanitizeDirectoryName = (name) => { return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-'); }; @@ -154,6 +168,8 @@ module.exports = { isFile, isDirectory, normalizeAndResolvePath, + isWSLPath, + normalizeWslPath, writeFile, writeBinaryFile, hasJsonExtension,