forked from extern/bruno
handle dir of files, comments in code
This commit is contained in:
parent
fff3e6d88a
commit
93661bd0d2
@ -943,10 +943,13 @@ export const cloneCollection = (collectionName, collectionFolderName, collection
|
|||||||
const { ipcRenderer } = window;
|
const { ipcRenderer } = window;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
ipcRenderer
|
ipcRenderer.invoke(
|
||||||
.invoke('renderer:clone-collection', collectionName, collectionFolderName, collectionLocation, perviousPath)
|
'renderer:clone-collection',
|
||||||
.then(resolve)
|
collectionName,
|
||||||
.catch(reject);
|
collectionFolderName,
|
||||||
|
collectionLocation,
|
||||||
|
perviousPath
|
||||||
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
export const openCollection = () => () => {
|
export const openCollection = () => () => {
|
||||||
|
@ -42,7 +42,6 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
|
|||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
'renderer:create-collection',
|
'renderer:create-collection',
|
||||||
async (event, collectionName, collectionFolderName, collectionLocation) => {
|
async (event, collectionName, collectionFolderName, collectionLocation) => {
|
||||||
try {
|
|
||||||
const dirPath = path.join(collectionLocation, collectionFolderName);
|
const dirPath = path.join(collectionLocation, collectionFolderName);
|
||||||
if (fs.existsSync(dirPath)) {
|
if (fs.existsSync(dirPath)) {
|
||||||
throw new Error(`collection: ${dirPath} already exists`);
|
throw new Error(`collection: ${dirPath} already exists`);
|
||||||
@ -65,15 +64,12 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
|
|||||||
|
|
||||||
mainWindow.webContents.send('main:collection-opened', dirPath, uid, brunoConfig);
|
mainWindow.webContents.send('main:collection-opened', dirPath, uid, brunoConfig);
|
||||||
ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid);
|
ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid);
|
||||||
} catch (error) {
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
// clone collection
|
// clone collection
|
||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
'renderer:clone-collection',
|
'renderer:clone-collection',
|
||||||
async (event, collectionName, collectionFolderName, collectionLocation, perviousPath) => {
|
async (event, collectionName, collectionFolderName, collectionLocation, previousPath) => {
|
||||||
try {
|
try {
|
||||||
const dirPath = path.join(collectionLocation, collectionFolderName);
|
const dirPath = path.join(collectionLocation, collectionFolderName);
|
||||||
if (fs.existsSync(dirPath)) {
|
if (fs.existsSync(dirPath)) {
|
||||||
@ -84,22 +80,35 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
|
|||||||
throw new Error(`collection: invalid pathname - ${dir}`);
|
throw new Error(`collection: invalid pathname - ${dir}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create dir
|
||||||
await createDirectory(dirPath);
|
await createDirectory(dirPath);
|
||||||
const uid = generateUidBasedOnHash(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');
|
const content = fs.readFileSync(brunoJsonFilePath, 'utf8');
|
||||||
|
|
||||||
|
//Chnage new name of collection
|
||||||
let json = JSON.parse(content);
|
let json = JSON.parse(content);
|
||||||
json.name = collectionName;
|
json.name = collectionName;
|
||||||
const cont = await stringifyJson(json);
|
const cont = await stringifyJson(json);
|
||||||
|
|
||||||
|
// write the bruno.json to new dir
|
||||||
await writeFile(path.join(dirPath, 'bruno.json'), cont);
|
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) {
|
for (const sourceFilePath of files) {
|
||||||
const fileName = path.basename(sourceFilePath);
|
const relativePath = path.relative(previousPath, sourceFilePath);
|
||||||
const destinationFilePath = path.join(dirPath, fileName);
|
const newFilePath = path.join(dirPath, relativePath);
|
||||||
console.log(destinationFilePath);
|
|
||||||
fs.copyFileSync(sourceFilePath, destinationFilePath);
|
// 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);
|
mainWindow.webContents.send('main:collection-opened', dirPath, uid, json);
|
||||||
ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid);
|
ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user