feat: removed legacy v1 bru lang auto migrate functionality

This commit is contained in:
Anoop M D 2023-10-07 00:25:26 +05:30
parent 8730c5a85b
commit 5a89d12716
2 changed files with 1 additions and 138 deletions

View File

@ -2,12 +2,10 @@ const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const { hasJsonExtension, hasBruExtension, writeFile } = require('../utils/filesystem');
const { hasBruExtension, writeFile } = require('../utils/filesystem');
const { bruToEnvJson, envJsonToBru, bruToJson, jsonToBru } = require('../bru');
const { dotenvToJson } = require('@usebruno/lang');
const { isLegacyEnvFile, migrateLegacyEnvFile, isLegacyBruFile, migrateLegacyBruFile } = require('../bru/migrate');
const { itemSchema } = require('@usebruno/schema');
const { uuid } = require('../utils/common');
const { getRequestUid } = require('../cache/requestUids');
const { decryptString } = require('../utils/encryption');
@ -87,11 +85,6 @@ const addEnvironmentFile = async (win, pathname, collectionUid, collectionPath)
let bruContent = fs.readFileSync(pathname, 'utf8');
// migrate old env json to bru file
if (isLegacyEnvFile(bruContent)) {
bruContent = await migrateLegacyEnvFile(bruContent, pathname);
}
file.data = bruToEnvJson(bruContent);
file.data.name = basename.substring(0, basename.length - 4);
file.data.uid = getRequestUid(pathname);
@ -235,27 +228,6 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
return addEnvironmentFile(win, pathname, collectionUid, collectionPath);
}
// migrate old json files to bru
if (hasJsonExtension(pathname)) {
try {
const json = fs.readFileSync(pathname, 'utf8');
const jsonData = JSON.parse(json);
await itemSchema.validate(jsonData);
const content = jsonToBru(jsonData);
const re = /(.*)\.json$/;
const subst = `$1.bru`;
const bruFilename = pathname.replace(re, subst);
await writeFile(bruFilename, content);
await fs.unlinkSync(pathname);
} catch (err) {
// do nothing
}
}
if (hasBruExtension(pathname)) {
const file = {
meta: {
@ -268,11 +240,6 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
try {
let bruContent = fs.readFileSync(pathname, 'utf8');
// migrate old bru format to new bru format
if (isLegacyBruFile(bruContent)) {
bruContent = await migrateLegacyBruFile(bruContent, pathname);
}
file.data = bruToJson(bruContent);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
@ -404,11 +371,6 @@ class Watcher {
this.watchers[watchPath].close();
}
// todo
// enable this in a future release
// once we can confirm all older json based files have been auto migrated to .bru format
// watchPath = path.join(watchPath, '**/*.bru');
const self = this;
setTimeout(() => {
const watcher = chokidar.watch(watchPath, {

View File

@ -1,99 +0,0 @@
const {
bruToEnvJson: bruToEnvJsonV1,
bruToJson: bruToJsonV1,
jsonToBruV2,
envJsonToBruV2
} = require('@usebruno/lang');
const _ = require('lodash');
const { writeFile } = require('../utils/filesystem');
const isLegacyEnvFile = (bruContent = '') => {
bruContent = bruContent.trim();
let regex = /^vars[\s\S]*\/vars$/;
return regex.test(bruContent);
};
const migrateLegacyEnvFile = async (bruContent, pathname) => {
const envJson = bruToEnvJsonV1(bruContent);
const newBruContent = envJsonToBruV2(envJson);
await writeFile(pathname, newBruContent);
return newBruContent;
};
const isLegacyBruFile = (bruContent = '') => {
bruContent = bruContent.trim();
let lines = bruContent.split(/\r?\n/);
let hasName = false;
let hasMethod = false;
let hasUrl = false;
for (let line of lines) {
line = line.trim();
if (line.startsWith('name')) {
hasName = true;
} else if (line.startsWith('method')) {
hasMethod = true;
} else if (line.startsWith('url')) {
hasUrl = true;
}
}
return hasName && hasMethod && hasUrl;
};
const migrateLegacyBruFile = async (bruContent, pathname) => {
const json = bruToJsonV1(bruContent);
let type = _.get(json, 'type');
if (type === 'http-request') {
type = 'http';
} else if (type === 'graphql-request') {
type = 'graphql';
} else {
type = 'http';
}
let script = {};
let legacyScript = _.get(json, 'request.script');
if (legacyScript && legacyScript.trim().length > 0) {
script = {
res: legacyScript
};
}
const bruJson = {
meta: {
name: _.get(json, 'name'),
type: type,
seq: _.get(json, 'seq')
},
http: {
method: _.lowerCase(_.get(json, 'request.method')),
url: _.get(json, 'request.url'),
body: _.get(json, 'request.body.mode', 'none')
},
query: _.get(json, 'request.params', []),
headers: _.get(json, 'request.headers', []),
body: _.get(json, 'request.body', {}),
script: script,
tests: _.get(json, 'request.tests', '')
};
const newBruContent = jsonToBruV2(bruJson);
await writeFile(pathname, newBruContent);
return newBruContent;
};
module.exports = {
isLegacyEnvFile,
migrateLegacyEnvFile,
isLegacyBruFile,
migrateLegacyBruFile
};