feat: lang parser nomenclature updates

This commit is contained in:
Anoop M D 2024-04-14 15:55:34 +05:30
parent eddac73341
commit a9f2bec695
4 changed files with 66 additions and 35 deletions

View File

@ -3,7 +3,7 @@ const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const { hasBruExtension } = require('../utils/filesystem');
const { bruToEnvJson, bruToJson, collectionBruToJson } = require('../bru');
const { parseEnvironment, parseRequest, parseCollection } = require('../bru');
const { dotenvToJson } = require('@usebruno/lang');
const { uuid } = require('../utils/common');
@ -99,7 +99,7 @@ const addEnvironmentFile = async (win, pathname, collectionUid, collectionPath)
let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = bruToEnvJson(bruContent);
file.data = parseEnvironment(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);
@ -134,7 +134,7 @@ const changeEnvironmentFile = async (win, pathname, collectionUid, collectionPat
};
const bruContent = fs.readFileSync(pathname, 'utf8');
file.data = bruToEnvJson(bruContent);
file.data = parseEnvironment(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);
_.each(_.get(file, 'data.variables', []), (variable) => (variable.uid = uuid()));
@ -229,7 +229,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = collectionBruToJson(bruContent);
file.data = parseCollection(bruContent);
hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
@ -252,7 +252,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = bruToJson(bruContent);
file.data = parseRequest(bruContent);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
@ -333,7 +333,7 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = collectionBruToJson(bruContent);
file.data = parseCollection(bruContent);
hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'change', file);
@ -355,7 +355,7 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
};
const bru = fs.readFileSync(pathname, 'utf8');
file.data = bruToJson(bru);
file.data = parseRequest(bru);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'change', file);
} catch (err) {

View File

@ -4,11 +4,11 @@ const {
jsonToBruV2,
bruToEnvJsonV2,
envJsonToBruV2,
collectionBruToJson: _collectionBruToJson,
jsonToCollectionBru: _jsonToCollectionBru
parseCollection: _collectionBruToJson,
stringifyCollection: _jsonToCollectionBru
} = require('@usebruno/lang');
const collectionBruToJson = (bru) => {
const parseCollection = (bru) => {
try {
const json = _collectionBruToJson(bru);
@ -30,7 +30,7 @@ const collectionBruToJson = (bru) => {
}
};
const jsonToCollectionBru = (json) => {
const stringifyCollection = (json) => {
try {
const collectionBruJson = {
query: _.get(json, 'request.params', []),
@ -54,7 +54,7 @@ const jsonToCollectionBru = (json) => {
}
};
const bruToEnvJson = (bru) => {
const parseEnvironment = (bru) => {
try {
const json = bruToEnvJsonV2(bru);
@ -71,7 +71,7 @@ const bruToEnvJson = (bru) => {
}
};
const envJsonToBru = (json) => {
const stringifyEnvironment = (json) => {
try {
const bru = envJsonToBruV2(json);
return bru;
@ -89,7 +89,7 @@ const envJsonToBru = (json) => {
* @param {string} bru The BRU file content.
* @returns {object} The JSON representation of the BRU file.
*/
const bruToJson = (bru) => {
const parseRequest = (bru) => {
try {
const json = bruToJsonV2(bru);
@ -140,7 +140,7 @@ const bruToJson = (bru) => {
* @param {object} json The JSON representation of the BRU file.
* @returns {string} The BRU file content.
*/
const jsonToBru = (json) => {
const stringifyRequest = (json) => {
let type = _.get(json, 'type');
if (type === 'http-request') {
type = 'http';
@ -180,10 +180,10 @@ const jsonToBru = (json) => {
};
module.exports = {
bruToJson,
jsonToBru,
bruToEnvJson,
envJsonToBru,
collectionBruToJson,
jsonToCollectionBru
parseRequest,
stringifyRequest,
parseEnvironment,
stringifyEnvironment,
parseCollection,
stringifyCollection
};

View File

@ -2,7 +2,7 @@ const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const { ipcMain, shell, dialog, app } = require('electron');
const { envJsonToBru, bruToJson, jsonToBru, jsonToCollectionBru } = require('../bru');
const { stringifyEnvironment, parseRequest, stringifyRequest, stringifyCollection } = require('../bru');
const {
isValidPathname,
@ -156,7 +156,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
try {
const collectionBruFilePath = path.join(collectionPathname, 'collection.bru');
const content = jsonToCollectionBru(collectionRoot);
const content = stringifyCollection(collectionRoot);
await writeFile(collectionBruFilePath, content);
} catch (error) {
return Promise.reject(error);
@ -170,7 +170,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
throw new Error(`path: ${pathname} already exists`);
}
const content = jsonToBru(request);
const content = stringifyRequest(request);
await writeFile(pathname, content);
} catch (error) {
return Promise.reject(error);
@ -184,7 +184,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
throw new Error(`path: ${pathname} does not exist`);
}
const content = jsonToBru(request);
const content = stringifyRequest(request);
await writeFile(pathname, content);
} catch (error) {
return Promise.reject(error);
@ -202,7 +202,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
throw new Error(`path: ${pathname} does not exist`);
}
const content = jsonToBru(request);
const content = stringifyRequest(request);
await writeFile(pathname, content);
}
} catch (error) {
@ -232,7 +232,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
environmentSecretsStore.storeEnvSecrets(collectionPathname, environment);
}
const content = envJsonToBru(environment);
const content = stringifyEnvironment(environment);
await writeFile(envFilePath, content);
} catch (error) {
@ -257,7 +257,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
environmentSecretsStore.storeEnvSecrets(collectionPathname, environment);
}
const content = envJsonToBru(environment);
const content = stringifyEnvironment(environment);
await writeFile(envFilePath, content);
} catch (error) {
return Promise.reject(error);
@ -331,13 +331,13 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
// update name in file and save new copy, then delete old copy
const data = fs.readFileSync(oldPath, 'utf8');
const jsonData = bruToJson(data);
const jsonData = parseRequest(data);
jsonData.name = newName;
moveRequestUid(oldPath, newPath);
const content = jsonToBru(jsonData);
const content = stringifyRequest(jsonData);
await writeFile(newPath, content);
await fs.unlinkSync(oldPath);
} catch (error) {
@ -416,7 +416,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
const parseCollectionItems = (items = [], currentPath) => {
items.forEach((item) => {
if (['http-request', 'graphql-request'].includes(item.type)) {
const content = jsonToBru(item);
const content = stringifyRequest(item);
const filePath = path.join(currentPath, `${item.name}.bru`);
fs.writeFileSync(filePath, content);
}
@ -438,7 +438,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
}
environments.forEach((env) => {
const content = envJsonToBru(env);
const content = stringifyEnvironment(env);
const filePath = path.join(envDirPath, `${env.name}.bru`);
fs.writeFileSync(filePath, content);
});
@ -479,7 +479,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
const parseCollectionItems = (items = [], currentPath) => {
items.forEach((item) => {
if (['http-request', 'graphql-request'].includes(item.type)) {
const content = jsonToBru(item);
const content = stringifyRequest(item);
const filePath = path.join(currentPath, `${item.name}.bru`);
fs.writeFileSync(filePath, content);
}
@ -507,11 +507,11 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
try {
for (let item of itemsToResequence) {
const bru = fs.readFileSync(item.pathname, 'utf8');
const jsonData = bruToJson(bru);
const jsonData = parseRequest(bru);
if (jsonData.seq !== item.seq) {
jsonData.seq = item.seq;
const content = jsonToBru(jsonData);
const content = stringifyRequest(jsonData);
await writeFile(item.pathname, content);
}
}

View File

@ -0,0 +1,31 @@
const { parse, stringify } = require('bru-js');
const parseRequest = (bru) => {
const ast = parse(bru);
};
const stringifyRequest = (json) => {
const { type, seq, name, request } = json;
const ast = {
type: 'multimap',
value: []
};
const metaAst = {
type: 'multimap',
value: []
};
metaAst.value.push({
type: 'pair',
key: 'name',
value: name
});
ast.value.push({
type: 'pair',
key: 'meta',
value: metaAst
});
return stringify(ast);
};