From 93661bd0d25aec131ddca2a111ca47090e9f7848 Mon Sep 17 00:00:00 2001 From: Akshat Khosya Date: Sat, 16 Dec 2023 13:07:51 +0530 Subject: [PATCH] handle dir of files, comments in code --- .../ReduxStore/slices/collections/actions.js | 11 ++- packages/bruno-electron/src/ipc/collection.js | 75 +++++++++++-------- 2 files changed, 49 insertions(+), 37 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 4e236b47..1a38b77c 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -943,10 +943,13 @@ export const cloneCollection = (collectionName, collectionFolderName, collection const { ipcRenderer } = window; return new Promise((resolve, reject) => { - ipcRenderer - .invoke('renderer:clone-collection', collectionName, collectionFolderName, collectionLocation, perviousPath) - .then(resolve) - .catch(reject); + ipcRenderer.invoke( + 'renderer:clone-collection', + collectionName, + collectionFolderName, + collectionLocation, + perviousPath + ); }); }; export const openCollection = () => () => { diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 38d24827..f21c56ab 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -42,38 +42,34 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection ipcMain.handle( 'renderer:create-collection', async (event, collectionName, collectionFolderName, collectionLocation) => { - try { - const dirPath = path.join(collectionLocation, collectionFolderName); - if (fs.existsSync(dirPath)) { - throw new Error(`collection: ${dirPath} already exists`); - } - - if (!isValidPathname(dirPath)) { - throw new Error(`collection: invalid pathname - ${dir}`); - } - - await createDirectory(dirPath); - - const uid = generateUidBasedOnHash(dirPath); - const brunoConfig = { - version: '1', - name: collectionName, - type: 'collection' - }; - const content = await stringifyJson(brunoConfig); - await writeFile(path.join(dirPath, 'bruno.json'), content); - - mainWindow.webContents.send('main:collection-opened', dirPath, uid, brunoConfig); - ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid); - } catch (error) { - return Promise.reject(error); + const dirPath = path.join(collectionLocation, collectionFolderName); + if (fs.existsSync(dirPath)) { + throw new Error(`collection: ${dirPath} already exists`); } + + if (!isValidPathname(dirPath)) { + throw new Error(`collection: invalid pathname - ${dir}`); + } + + await createDirectory(dirPath); + + const uid = generateUidBasedOnHash(dirPath); + const brunoConfig = { + version: '1', + name: collectionName, + type: 'collection' + }; + const content = await stringifyJson(brunoConfig); + await writeFile(path.join(dirPath, 'bruno.json'), content); + + mainWindow.webContents.send('main:collection-opened', dirPath, uid, brunoConfig); + ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid); } ); // clone collection ipcMain.handle( 'renderer:clone-collection', - async (event, collectionName, collectionFolderName, collectionLocation, perviousPath) => { + async (event, collectionName, collectionFolderName, collectionLocation, previousPath) => { try { const dirPath = path.join(collectionLocation, collectionFolderName); if (fs.existsSync(dirPath)) { @@ -84,22 +80,35 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection throw new Error(`collection: invalid pathname - ${dir}`); } + // create dir await createDirectory(dirPath); const uid = generateUidBasedOnHash(dirPath); - const brunoJsonFilePath = path.join(perviousPath, 'bruno.json'); + + // open the bruno.json of previousPath + const brunoJsonFilePath = path.join(previousPath, 'bruno.json'); const content = fs.readFileSync(brunoJsonFilePath, 'utf8'); + + //Chnage new name of collection let json = JSON.parse(content); json.name = collectionName; const cont = await stringifyJson(json); + + // write the bruno.json to new dir await writeFile(path.join(dirPath, 'bruno.json'), cont); - const files = searchForBruFiles(perviousPath); - console.log(files); + + // Now copy all the files with extension name .bru along with there dir + const files = searchForBruFiles(previousPath); + for (const sourceFilePath of files) { - const fileName = path.basename(sourceFilePath); - const destinationFilePath = path.join(dirPath, fileName); - console.log(destinationFilePath); - fs.copyFileSync(sourceFilePath, destinationFilePath); + const relativePath = path.relative(previousPath, sourceFilePath); + const newFilePath = path.join(dirPath, relativePath); + + // handle dir of files + fs.mkdirSync(path.dirname(newFilePath), { recursive: true }); + // copy each files + fs.copyFileSync(sourceFilePath, newFilePath); } + mainWindow.webContents.send('main:collection-opened', dirPath, uid, json); ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid); } catch (error) {