Skip to content

Commit

Permalink
Fix/rename-collection-support-wsl (#2892)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Pragadesh-45 authored Aug 23, 2024
1 parent 8b76ece commit 0b9554c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
12 changes: 11 additions & 1 deletion packages/bruno-electron/src/ipc/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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`);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/bruno-electron/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -143,6 +155,8 @@ const searchForBruFiles = (dir) => {
return searchForFiles(dir, '.bru');
};

// const isW

const sanitizeDirectoryName = (name) => {
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
};
Expand All @@ -154,6 +168,8 @@ module.exports = {
isFile,
isDirectory,
normalizeAndResolvePath,
isWSLPath,
normalizeWslPath,
writeFile,
writeBinaryFile,
hasJsonExtension,
Expand Down

0 comments on commit 0b9554c

Please sign in to comment.