mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-30 16:14:06 +00:00
Merge pull request #6069 from pooja-bruno/feat/add-edit-variable-in-place
feat: edit variable in place
This commit is contained in:
@@ -286,6 +286,73 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
|
||||
}
|
||||
});
|
||||
|
||||
// Helper: Parse file content based on scope type
|
||||
const parseFileByType = async (fileContent, scopeType) => {
|
||||
switch (scopeType) {
|
||||
case 'request':
|
||||
return await parseRequestViaWorker(fileContent);
|
||||
case 'folder':
|
||||
return parseFolder(fileContent);
|
||||
case 'collection':
|
||||
return parseCollection(fileContent);
|
||||
default:
|
||||
throw new Error(`Invalid scope type: ${scopeType}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Helper: Stringify data based on scope type
|
||||
const stringifyByType = async (data, scopeType) => {
|
||||
switch (scopeType) {
|
||||
case 'request':
|
||||
return await stringifyRequestViaWorker(data);
|
||||
case 'folder':
|
||||
return stringifyFolder(data);
|
||||
case 'collection':
|
||||
return stringifyCollection(data);
|
||||
default:
|
||||
throw new Error(`Invalid scope type: ${scopeType}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Helper: Update or create variable in array
|
||||
const updateOrCreateVariable = (variables, variable) => {
|
||||
const existingVar = variables.find((v) => v.name === variable.name);
|
||||
|
||||
if (existingVar) {
|
||||
// Update existing variable
|
||||
return variables.map((v) => (v.name === variable.name ? variable : v));
|
||||
}
|
||||
|
||||
// Create new variable
|
||||
return [...variables, variable];
|
||||
};
|
||||
|
||||
// update variable in request/folder/collection file
|
||||
ipcMain.handle('renderer:update-variable-in-file', async (event, pathname, variable, scopeType) => {
|
||||
try {
|
||||
if (!fs.existsSync(pathname)) {
|
||||
throw new Error(`path: ${pathname} does not exist`);
|
||||
}
|
||||
|
||||
// Read and parse the file
|
||||
const fileContent = fs.readFileSync(pathname, 'utf8');
|
||||
const parsedData = await parseFileByType(fileContent, scopeType);
|
||||
|
||||
// Update the specific variable or create it if it doesn't exist
|
||||
const varsPath = 'request.vars.req';
|
||||
const variables = _.get(parsedData, varsPath, []);
|
||||
const updatedVariables = updateOrCreateVariable(variables, variable);
|
||||
|
||||
_.set(parsedData, varsPath, updatedVariables);
|
||||
|
||||
// Stringify and write back
|
||||
const content = await stringifyByType(parsedData, scopeType);
|
||||
await writeFile(pathname, content);
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
// create environment
|
||||
ipcMain.handle('renderer:create-environment', async (event, collectionPathname, name, variables) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user