fix: fix windows filepath issues #89

This commit is contained in:
Anoop M D 2023-02-12 21:46:42 +05:30
parent ddd39e630d
commit 943e74c327
5 changed files with 44 additions and 10 deletions

View File

@ -33,6 +33,10 @@ const useCollectionTreeSync = () => {
}; };
const _collectionTreeUpdated = (type, val) => { const _collectionTreeUpdated = (type, val) => {
if(window.__IS_DEV__) {
console.log(type);
console.log(val);
}
if (type === 'addDir') { if (type === 'addDir') {
dispatch( dispatch(
collectionAddDirectoryEvent({ collectionAddDirectoryEvent({

View File

@ -22,6 +22,7 @@ import {
} from 'utils/collections'; } from 'utils/collections';
import { collectionSchema, itemSchema, environmentSchema, environmentsSchema } from '@usebruno/schema'; import { collectionSchema, itemSchema, environmentSchema, environmentsSchema } from '@usebruno/schema';
import { waitForNextTick } from 'utils/common'; import { waitForNextTick } from 'utils/common';
import { getDirectoryName } from 'utils/common/platform';
import { sendNetworkRequest, cancelNetworkRequest } from 'utils/network'; import { sendNetworkRequest, cancelNetworkRequest } from 'utils/network';
import { import {
@ -232,14 +233,14 @@ export const renameItem = (newName, itemUid, collectionUid) => (dispatch, getSta
return reject(new Error('Unable to locate item')); return reject(new Error('Unable to locate item'));
} }
const dirname = path.dirname(item.pathname); const dirname = getDirectoryName(item.pathname);
let newPathname = ''; let newPathname = '';
if (item.type === 'folder') { if (item.type === 'folder') {
newPathname = `${dirname}${PATH_SEPARATOR}${trim(newName)}`; newPathname = path.join(dirname, trim(newName));
} else { } else {
const filename = resolveRequestFilename(newName); const filename = resolveRequestFilename(newName);
newPathname = `${dirname}${PATH_SEPARATOR}${filename}`; newPathname = path.join(dirname, filename);
} }
const { ipcRenderer } = window; const { ipcRenderer } = window;
@ -291,8 +292,8 @@ export const cloneItem = (newName, itemUid, collectionUid) => (dispatch, getStat
} else { } else {
const reqWithSameNameExists = find(parentItem.items, (i) => i.type !== 'folder' && trim(i.filename) === trim(filename)); const reqWithSameNameExists = find(parentItem.items, (i) => i.type !== 'folder' && trim(i.filename) === trim(filename));
if (!reqWithSameNameExists) { if (!reqWithSameNameExists) {
const dirname = path.dirname(item.pathname); const dirname = getDirectoryName(item.pathname);
const fullName = `${dirname}${PATH_SEPARATOR}${filename}`; const fullName = path.join(dirname, filename);
const { ipcRenderer } = window; const { ipcRenderer } = window;
const requestItems = filter(parentItem.items, (i) => i.type !== 'folder'); const requestItems = filter(parentItem.items, (i) => i.type !== 'folder');
itemToSave.seq = requestItems ? (requestItems.length + 1) : 1; itemToSave.seq = requestItems ? (requestItems.length + 1) : 1;

View File

@ -23,7 +23,7 @@ import {
areItemsTheSameExceptSeqUpdate areItemsTheSameExceptSeqUpdate
} from 'utils/collections'; } from 'utils/collections';
import { parseQueryParams, stringifyQueryParams } from 'utils/url'; import { parseQueryParams, stringifyQueryParams } from 'utils/url';
import { getSubdirectoriesFromRoot } from 'utils/common/platform'; import { getSubdirectoriesFromRoot, getDirectoryName } from 'utils/common/platform';
const PATH_SEPARATOR = path.sep; const PATH_SEPARATOR = path.sep;
@ -712,7 +712,7 @@ export const collectionsSlice = createSlice({
const collection = findCollectionByUid(state.collections, file.meta.collectionUid); const collection = findCollectionByUid(state.collections, file.meta.collectionUid);
if (collection) { if (collection) {
const dirname = path.dirname(file.meta.pathname); const dirname = getDirectoryName(file.meta.pathname);
const subDirectories = getSubdirectoriesFromRoot(collection.pathname, dirname); const subDirectories = getSubdirectoriesFromRoot(collection.pathname, dirname);
let currentPath = collection.pathname; let currentPath = collection.pathname;
let currentSubItems = collection.items; let currentSubItems = collection.items;

View File

@ -1,5 +1,6 @@
import trim from 'lodash/trim'; import trim from 'lodash/trim';
import path from 'path'; import path from 'path';
import slash from './slash';
export const isElectron = () => { export const isElectron = () => {
if (!window) { if (!window) {
@ -18,9 +19,17 @@ export const resolveRequestFilename = (name) => {
}; };
export const getSubdirectoriesFromRoot = (rootPath, pathname) => { export const getSubdirectoriesFromRoot = (rootPath, pathname) => {
if (!path.isAbsolute(pathname)) { // convert to unix style path
throw new Error('Invalid path!'); pathname = slash(pathname);
} rootPath = slash(rootPath);
const relativePath = path.relative(rootPath, pathname); const relativePath = path.relative(rootPath, pathname);
return relativePath ? relativePath.split(path.sep) : []; return relativePath ? relativePath.split(path.sep) : [];
}; };
export const getDirectoryName = (pathname) => {
// convert to unix style path
pathname = slash(pathname);
return path.dirname(pathname);
}

View File

@ -0,0 +1,20 @@
/**
* MIT License
*
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const slash = (path) => {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, '/');
}
export default slash;