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

View File

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

View File

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