Merge pull request #1028 from StonyTV/feature/clone-option-for-folders

feat(#1017) Added ability to clone a folder
This commit is contained in:
Anoop M D 2023-11-30 00:06:47 +05:30 committed by GitHub
commit a6a59ddbd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 22 deletions

View File

@ -306,28 +306,26 @@ const CollectionItem = ({ item, collection, searchText }) => {
>
Rename
</div>
<div
className="dropdown-item"
onClick={(e) => {
dropdownTippyRef.current.hide();
setCloneItemModalOpen(true);
}}
>
Clone
</div>
{!isFolder && (
<>
<div
className="dropdown-item"
onClick={(e) => {
dropdownTippyRef.current.hide();
setCloneItemModalOpen(true);
}}
>
Clone
</div>
<div
className="dropdown-item"
onClick={(e) => {
dropdownTippyRef.current.hide();
handleClick(null);
handleRun();
}}
>
Run
</div>
</>
<div
className="dropdown-item"
onClick={(e) => {
dropdownTippyRef.current.hide();
handleClick(null);
handleRun();
}}
>
Run
</div>
)}
{!isFolder && item.type === 'http-request' && (
<div

View File

@ -319,7 +319,20 @@ export const cloneItem = (newName, itemUid, collectionUid) => (dispatch, getStat
}
if (isItemAFolder(item)) {
throw new Error('Cloning folders is not supported yet');
const parentFolder = findParentItemInCollection(collection, item.uid) || collection;
const folderWithSameNameExists = find(
parentFolder.items,
(i) => i.type === 'folder' && trim(i.name) === trim(newName)
);
if (folderWithSameNameExists) {
return reject(new Error('Duplicate folder names under same parent folder are not allowed'));
}
const collectionPath = `${parentFolder.pathname}${PATH_SEPARATOR}${newName}`;
ipcRenderer.invoke('renderer:clone-folder', item, collectionPath).then(resolve).catch(reject);
return;
}
const parentItem = findParentItemInCollection(collectionCopy, itemUid);

View File

@ -391,6 +391,40 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
}
});
ipcMain.handle('renderer:clone-folder', async (event, itemFolder, collectionPath) => {
try {
if (fs.existsSync(collectionPath)) {
throw new Error(`folder: ${collectionPath} already exists`);
}
// Recursive function to parse the folder and create files/folders
const parseCollectionItems = (items = [], currentPath) => {
items.forEach((item) => {
if (['http-request', 'graphql-request'].includes(item.type)) {
const content = jsonToBru(item);
const filePath = path.join(currentPath, `${item.name}.bru`);
fs.writeFileSync(filePath, content);
}
if (item.type === 'folder') {
const folderPath = path.join(currentPath, item.name);
fs.mkdirSync(folderPath);
if (item.items && item.items.length) {
parseCollectionItems(item.items, folderPath);
}
}
});
};
await createDirectory(collectionPath);
// create folder and files based on another folder
await parseCollectionItems(itemFolder.items, collectionPath);
} catch (error) {
return Promise.reject(error);
}
});
ipcMain.handle('renderer:resequence-items', async (event, itemsToResequence) => {
try {
for (let item of itemsToResequence) {