From d24f1a10545a6d7402a15db07931b2a1002bf52e Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sat, 4 Feb 2023 20:11:33 +0530 Subject: [PATCH] refactor: organized v1 and v2 versions inside bru-lang --- packages/bruno-lang/src/index.js | 251 +----------------- packages/bruno-lang/{ => v1}/src/body-tag.js | 0 .../bruno-lang/{ => v1}/src/env-vars-tag.js | 0 .../bruno-lang/{ => v1}/src/headers-tag.js | 0 packages/bruno-lang/v1/src/index.js | 250 +++++++++++++++++ .../bruno-lang/{ => v1}/src/inline-tag.js | 0 .../bruno-lang/{ => v1}/src/key-val-lines.js | 0 .../bruno-lang/{ => v1}/src/params-tag.js | 0 .../bruno-lang/{ => v1}/src/script-tag.js | 0 packages/bruno-lang/{ => v1}/src/tests-tag.js | 0 packages/bruno-lang/{ => v1}/src/utils.js | 0 .../{ => v1}/tests/body-tag.spec.js | 0 .../{ => v1}/tests/bru-to-env-json.spec.js | 0 .../{ => v1}/tests/bru-to-json.spec.js | 0 .../{ => v1}/tests/env-json-to-bru.spec.js | 0 .../{ => v1}/tests/fixtures/env.bru | 0 .../{ => v1}/tests/fixtures/request.bru | 0 .../{ => v1}/tests/inline-tag.spec.js | 0 .../{ => v1}/tests/json-to-bru.spec.js | 0 .../{ => v1}/tests/key-val-lines.spec.js | 0 .../{ => v1}/tests/script-tag.spec.js | 0 .../{ => v1}/tests/tests-tag.spec.js | 0 .../bruno-lang/{ => v1}/tests/utils.spec.js | 0 .../v2/src/{index.js => bruToJson.js} | 0 .../bruno-lang/v2/tests/dictionary.spec.js | 2 +- packages/bruno-lang/v2/tests/index.spec.js | 2 +- packages/bruno-lang/v2/tests/text.spec.js | 2 +- 27 files changed, 262 insertions(+), 245 deletions(-) rename packages/bruno-lang/{ => v1}/src/body-tag.js (100%) rename packages/bruno-lang/{ => v1}/src/env-vars-tag.js (100%) rename packages/bruno-lang/{ => v1}/src/headers-tag.js (100%) create mode 100644 packages/bruno-lang/v1/src/index.js rename packages/bruno-lang/{ => v1}/src/inline-tag.js (100%) rename packages/bruno-lang/{ => v1}/src/key-val-lines.js (100%) rename packages/bruno-lang/{ => v1}/src/params-tag.js (100%) rename packages/bruno-lang/{ => v1}/src/script-tag.js (100%) rename packages/bruno-lang/{ => v1}/src/tests-tag.js (100%) rename packages/bruno-lang/{ => v1}/src/utils.js (100%) rename packages/bruno-lang/{ => v1}/tests/body-tag.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/bru-to-env-json.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/bru-to-json.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/env-json-to-bru.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/fixtures/env.bru (100%) rename packages/bruno-lang/{ => v1}/tests/fixtures/request.bru (100%) rename packages/bruno-lang/{ => v1}/tests/inline-tag.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/json-to-bru.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/key-val-lines.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/script-tag.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/tests-tag.spec.js (100%) rename packages/bruno-lang/{ => v1}/tests/utils.spec.js (100%) rename packages/bruno-lang/v2/src/{index.js => bruToJson.js} (100%) diff --git a/packages/bruno-lang/src/index.js b/packages/bruno-lang/src/index.js index 877e8ce35..bea612a85 100644 --- a/packages/bruno-lang/src/index.js +++ b/packages/bruno-lang/src/index.js @@ -1,250 +1,17 @@ const { - many, - choice, - anyChar -} = require("arcsecond"); -const _ = require('lodash'); -const { - indentString, - outdentString, - get -} = require('./utils'); + bruToJson, + jsonToBru, + bruToEnvJson, + envJsonToBru +} = require('./v1/src'); -const inlineTag = require('./inline-tag'); -const paramsTag = require('./params-tag'); -const headersTag = require('./headers-tag'); -const { - bodyJsonTag, - bodyGraphqlTag, - bodyGraphqlVarsTag, - bodyTextTag, - bodyXmlTag, - bodyFormUrlEncodedTagDeprecated, - bodyFormUrlEncodedTag, - bodyMultipartFormTag -} = require('./body-tag'); -const scriptTag = require('./script-tag'); -const testsTag = require('./tests-tag'); - -const bruToJson = (fileContents) => { - const parser = many(choice([ - inlineTag, - paramsTag, - headersTag, - bodyJsonTag, - bodyGraphqlTag, - bodyGraphqlVarsTag, - bodyTextTag, - bodyXmlTag, - bodyFormUrlEncodedTagDeprecated, - bodyFormUrlEncodedTag, - bodyMultipartFormTag, - scriptTag, - testsTag, - anyChar - ])); - - const parsed = parser - .run(fileContents) - .result - .reduce((acc, item) => _.merge(acc, item), {}); - - const json = { - type: parsed.type || '', - name: parsed.name || '', - seq: parsed.seq ? Number(parsed.seq) : 1, - request: { - method: parsed.method || '', - url: parsed.url || '', - params: parsed.params || [], - headers: parsed.headers || [], - body: parsed.body || {mode: 'none'}, - script: parsed.script ? outdentString(parsed.script) : '', - tests: parsed.tests ? outdentString(parsed.tests) : '' - } - }; - - const body = get(json, 'request.body'); - - if(body && body.text) { - body.text = outdentString(body.text); - } - - if(body && body.json) { - body.json = outdentString(body.json); - } - - if(body && body.xml) { - body.xml = outdentString(body.xml); - } - - if(body && body.graphql && body.graphql.query) { - body.graphql.query = outdentString(body.graphql.query); - } - - if(body && body.graphql && body.graphql.variables) { - body.graphql.variables = outdentString(body.graphql.variables); - } - - return json; -}; - -const jsonToBru = (json) => { - const { - type, - name, - seq, - request: { - method, - url, - params, - headers, - body, - script, - tests - } - } = json; - - let bru = `name ${name} -method ${method} -url ${url} -type ${type} -body-mode ${body ? body.mode : 'none'} -seq ${seq ? seq : 1} -`; - - if(params && params.length) { - bru += ` -params -${params.map(param => ` ${param.enabled ? 1 : 0} ${param.name} ${param.value}`).join('\n')} -/params -`; - } - - if(headers && headers.length) { - bru += ` -headers -${headers.map(header => ` ${header.enabled ? 1 : 0} ${header.name} ${header.value}`).join('\n')} -/headers -`; - } - - if(body && body.json && body.json.length) { - bru += ` -body(type=json) -${indentString(body.json)} -/body -`; - } - - if(body && body.graphql && body.graphql.query) { - bru += ` -body(type=graphql) -${indentString(body.graphql.query)} -/body -`; - } - - if(body && body.graphql && body.graphql.variables) { - bru += ` -body(type=graphql-vars) -${indentString(body.graphql.variables)} -/body -`; - } - - if(body && body.text && body.text.length) { - bru += ` -body(type=text) -${indentString(body.text)} -/body -`; - } - - if(body && body.xml && body.xml.length) { - bru += ` -body(type=xml) -${indentString(body.xml)} -/body -`; - } - - if(body && body.formUrlEncoded && body.formUrlEncoded.length) { - bru += ` -body(type=form-urlencoded) -${body.formUrlEncoded.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')} -/body -`; - } - - if(body && body.multipartForm && body.multipartForm.length) { - bru += ` -body(type=multipart-form) -${body.multipartForm.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')} -/body -`; - } - - if(script && script.length) { - bru += ` -script -${indentString(script)} -/script -`; - } - - if(tests && tests.length) { - bru += ` -tests -${indentString(tests)} -/tests -`; - } - - return bru; -}; - -// env -const envVarsTag = require('./env-vars-tag'); - -const bruToEnvJson = (fileContents) => { - const parser = many(choice([ - envVarsTag, - anyChar - ])); - - const parsed = parser - .run(fileContents) - .result - .reduce((acc, item) => _.merge(acc, item), {}); - - const json = { - variables: parsed.variables || [] - }; - - return json; -}; - -const envJsonToBru = (json) => { - const { - variables - } = json; - - let bru = ''; - - if(variables && variables.length) { - bru += `vars -${variables.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')} -/vars -`; - } - - return bru; -}; +const bruToJsonV2 = require('./v2/src/bruToJson'); module.exports = { bruToJson, jsonToBru, bruToEnvJson, - envJsonToBru + envJsonToBru, + + bruToJsonV2 }; \ No newline at end of file diff --git a/packages/bruno-lang/src/body-tag.js b/packages/bruno-lang/v1/src/body-tag.js similarity index 100% rename from packages/bruno-lang/src/body-tag.js rename to packages/bruno-lang/v1/src/body-tag.js diff --git a/packages/bruno-lang/src/env-vars-tag.js b/packages/bruno-lang/v1/src/env-vars-tag.js similarity index 100% rename from packages/bruno-lang/src/env-vars-tag.js rename to packages/bruno-lang/v1/src/env-vars-tag.js diff --git a/packages/bruno-lang/src/headers-tag.js b/packages/bruno-lang/v1/src/headers-tag.js similarity index 100% rename from packages/bruno-lang/src/headers-tag.js rename to packages/bruno-lang/v1/src/headers-tag.js diff --git a/packages/bruno-lang/v1/src/index.js b/packages/bruno-lang/v1/src/index.js new file mode 100644 index 000000000..877e8ce35 --- /dev/null +++ b/packages/bruno-lang/v1/src/index.js @@ -0,0 +1,250 @@ +const { + many, + choice, + anyChar +} = require("arcsecond"); +const _ = require('lodash'); +const { + indentString, + outdentString, + get +} = require('./utils'); + +const inlineTag = require('./inline-tag'); +const paramsTag = require('./params-tag'); +const headersTag = require('./headers-tag'); +const { + bodyJsonTag, + bodyGraphqlTag, + bodyGraphqlVarsTag, + bodyTextTag, + bodyXmlTag, + bodyFormUrlEncodedTagDeprecated, + bodyFormUrlEncodedTag, + bodyMultipartFormTag +} = require('./body-tag'); +const scriptTag = require('./script-tag'); +const testsTag = require('./tests-tag'); + +const bruToJson = (fileContents) => { + const parser = many(choice([ + inlineTag, + paramsTag, + headersTag, + bodyJsonTag, + bodyGraphqlTag, + bodyGraphqlVarsTag, + bodyTextTag, + bodyXmlTag, + bodyFormUrlEncodedTagDeprecated, + bodyFormUrlEncodedTag, + bodyMultipartFormTag, + scriptTag, + testsTag, + anyChar + ])); + + const parsed = parser + .run(fileContents) + .result + .reduce((acc, item) => _.merge(acc, item), {}); + + const json = { + type: parsed.type || '', + name: parsed.name || '', + seq: parsed.seq ? Number(parsed.seq) : 1, + request: { + method: parsed.method || '', + url: parsed.url || '', + params: parsed.params || [], + headers: parsed.headers || [], + body: parsed.body || {mode: 'none'}, + script: parsed.script ? outdentString(parsed.script) : '', + tests: parsed.tests ? outdentString(parsed.tests) : '' + } + }; + + const body = get(json, 'request.body'); + + if(body && body.text) { + body.text = outdentString(body.text); + } + + if(body && body.json) { + body.json = outdentString(body.json); + } + + if(body && body.xml) { + body.xml = outdentString(body.xml); + } + + if(body && body.graphql && body.graphql.query) { + body.graphql.query = outdentString(body.graphql.query); + } + + if(body && body.graphql && body.graphql.variables) { + body.graphql.variables = outdentString(body.graphql.variables); + } + + return json; +}; + +const jsonToBru = (json) => { + const { + type, + name, + seq, + request: { + method, + url, + params, + headers, + body, + script, + tests + } + } = json; + + let bru = `name ${name} +method ${method} +url ${url} +type ${type} +body-mode ${body ? body.mode : 'none'} +seq ${seq ? seq : 1} +`; + + if(params && params.length) { + bru += ` +params +${params.map(param => ` ${param.enabled ? 1 : 0} ${param.name} ${param.value}`).join('\n')} +/params +`; + } + + if(headers && headers.length) { + bru += ` +headers +${headers.map(header => ` ${header.enabled ? 1 : 0} ${header.name} ${header.value}`).join('\n')} +/headers +`; + } + + if(body && body.json && body.json.length) { + bru += ` +body(type=json) +${indentString(body.json)} +/body +`; + } + + if(body && body.graphql && body.graphql.query) { + bru += ` +body(type=graphql) +${indentString(body.graphql.query)} +/body +`; + } + + if(body && body.graphql && body.graphql.variables) { + bru += ` +body(type=graphql-vars) +${indentString(body.graphql.variables)} +/body +`; + } + + if(body && body.text && body.text.length) { + bru += ` +body(type=text) +${indentString(body.text)} +/body +`; + } + + if(body && body.xml && body.xml.length) { + bru += ` +body(type=xml) +${indentString(body.xml)} +/body +`; + } + + if(body && body.formUrlEncoded && body.formUrlEncoded.length) { + bru += ` +body(type=form-urlencoded) +${body.formUrlEncoded.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')} +/body +`; + } + + if(body && body.multipartForm && body.multipartForm.length) { + bru += ` +body(type=multipart-form) +${body.multipartForm.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')} +/body +`; + } + + if(script && script.length) { + bru += ` +script +${indentString(script)} +/script +`; + } + + if(tests && tests.length) { + bru += ` +tests +${indentString(tests)} +/tests +`; + } + + return bru; +}; + +// env +const envVarsTag = require('./env-vars-tag'); + +const bruToEnvJson = (fileContents) => { + const parser = many(choice([ + envVarsTag, + anyChar + ])); + + const parsed = parser + .run(fileContents) + .result + .reduce((acc, item) => _.merge(acc, item), {}); + + const json = { + variables: parsed.variables || [] + }; + + return json; +}; + +const envJsonToBru = (json) => { + const { + variables + } = json; + + let bru = ''; + + if(variables && variables.length) { + bru += `vars +${variables.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')} +/vars +`; + } + + return bru; +}; + +module.exports = { + bruToJson, + jsonToBru, + bruToEnvJson, + envJsonToBru +}; \ No newline at end of file diff --git a/packages/bruno-lang/src/inline-tag.js b/packages/bruno-lang/v1/src/inline-tag.js similarity index 100% rename from packages/bruno-lang/src/inline-tag.js rename to packages/bruno-lang/v1/src/inline-tag.js diff --git a/packages/bruno-lang/src/key-val-lines.js b/packages/bruno-lang/v1/src/key-val-lines.js similarity index 100% rename from packages/bruno-lang/src/key-val-lines.js rename to packages/bruno-lang/v1/src/key-val-lines.js diff --git a/packages/bruno-lang/src/params-tag.js b/packages/bruno-lang/v1/src/params-tag.js similarity index 100% rename from packages/bruno-lang/src/params-tag.js rename to packages/bruno-lang/v1/src/params-tag.js diff --git a/packages/bruno-lang/src/script-tag.js b/packages/bruno-lang/v1/src/script-tag.js similarity index 100% rename from packages/bruno-lang/src/script-tag.js rename to packages/bruno-lang/v1/src/script-tag.js diff --git a/packages/bruno-lang/src/tests-tag.js b/packages/bruno-lang/v1/src/tests-tag.js similarity index 100% rename from packages/bruno-lang/src/tests-tag.js rename to packages/bruno-lang/v1/src/tests-tag.js diff --git a/packages/bruno-lang/src/utils.js b/packages/bruno-lang/v1/src/utils.js similarity index 100% rename from packages/bruno-lang/src/utils.js rename to packages/bruno-lang/v1/src/utils.js diff --git a/packages/bruno-lang/tests/body-tag.spec.js b/packages/bruno-lang/v1/tests/body-tag.spec.js similarity index 100% rename from packages/bruno-lang/tests/body-tag.spec.js rename to packages/bruno-lang/v1/tests/body-tag.spec.js diff --git a/packages/bruno-lang/tests/bru-to-env-json.spec.js b/packages/bruno-lang/v1/tests/bru-to-env-json.spec.js similarity index 100% rename from packages/bruno-lang/tests/bru-to-env-json.spec.js rename to packages/bruno-lang/v1/tests/bru-to-env-json.spec.js diff --git a/packages/bruno-lang/tests/bru-to-json.spec.js b/packages/bruno-lang/v1/tests/bru-to-json.spec.js similarity index 100% rename from packages/bruno-lang/tests/bru-to-json.spec.js rename to packages/bruno-lang/v1/tests/bru-to-json.spec.js diff --git a/packages/bruno-lang/tests/env-json-to-bru.spec.js b/packages/bruno-lang/v1/tests/env-json-to-bru.spec.js similarity index 100% rename from packages/bruno-lang/tests/env-json-to-bru.spec.js rename to packages/bruno-lang/v1/tests/env-json-to-bru.spec.js diff --git a/packages/bruno-lang/tests/fixtures/env.bru b/packages/bruno-lang/v1/tests/fixtures/env.bru similarity index 100% rename from packages/bruno-lang/tests/fixtures/env.bru rename to packages/bruno-lang/v1/tests/fixtures/env.bru diff --git a/packages/bruno-lang/tests/fixtures/request.bru b/packages/bruno-lang/v1/tests/fixtures/request.bru similarity index 100% rename from packages/bruno-lang/tests/fixtures/request.bru rename to packages/bruno-lang/v1/tests/fixtures/request.bru diff --git a/packages/bruno-lang/tests/inline-tag.spec.js b/packages/bruno-lang/v1/tests/inline-tag.spec.js similarity index 100% rename from packages/bruno-lang/tests/inline-tag.spec.js rename to packages/bruno-lang/v1/tests/inline-tag.spec.js diff --git a/packages/bruno-lang/tests/json-to-bru.spec.js b/packages/bruno-lang/v1/tests/json-to-bru.spec.js similarity index 100% rename from packages/bruno-lang/tests/json-to-bru.spec.js rename to packages/bruno-lang/v1/tests/json-to-bru.spec.js diff --git a/packages/bruno-lang/tests/key-val-lines.spec.js b/packages/bruno-lang/v1/tests/key-val-lines.spec.js similarity index 100% rename from packages/bruno-lang/tests/key-val-lines.spec.js rename to packages/bruno-lang/v1/tests/key-val-lines.spec.js diff --git a/packages/bruno-lang/tests/script-tag.spec.js b/packages/bruno-lang/v1/tests/script-tag.spec.js similarity index 100% rename from packages/bruno-lang/tests/script-tag.spec.js rename to packages/bruno-lang/v1/tests/script-tag.spec.js diff --git a/packages/bruno-lang/tests/tests-tag.spec.js b/packages/bruno-lang/v1/tests/tests-tag.spec.js similarity index 100% rename from packages/bruno-lang/tests/tests-tag.spec.js rename to packages/bruno-lang/v1/tests/tests-tag.spec.js diff --git a/packages/bruno-lang/tests/utils.spec.js b/packages/bruno-lang/v1/tests/utils.spec.js similarity index 100% rename from packages/bruno-lang/tests/utils.spec.js rename to packages/bruno-lang/v1/tests/utils.spec.js diff --git a/packages/bruno-lang/v2/src/index.js b/packages/bruno-lang/v2/src/bruToJson.js similarity index 100% rename from packages/bruno-lang/v2/src/index.js rename to packages/bruno-lang/v2/src/bruToJson.js diff --git a/packages/bruno-lang/v2/tests/dictionary.spec.js b/packages/bruno-lang/v2/tests/dictionary.spec.js index 46d5b31f7..4a855a32a 100644 --- a/packages/bruno-lang/v2/tests/dictionary.spec.js +++ b/packages/bruno-lang/v2/tests/dictionary.spec.js @@ -2,7 +2,7 @@ * This test file is used to test the text parser. */ -const parser = require("../src/index"); +const parser = require("../src/bruToJson"); const assertSingleHeader = (input) => { const output = parser(input); diff --git a/packages/bruno-lang/v2/tests/index.spec.js b/packages/bruno-lang/v2/tests/index.spec.js index c33cd7630..764a95da5 100644 --- a/packages/bruno-lang/v2/tests/index.spec.js +++ b/packages/bruno-lang/v2/tests/index.spec.js @@ -1,6 +1,6 @@ const fs = require("fs"); const path = require("path"); -const parser = require("../src/index"); +const parser = require("../src/bruToJson"); describe("parser", () => { it("should parse the bru file", () => { diff --git a/packages/bruno-lang/v2/tests/text.spec.js b/packages/bruno-lang/v2/tests/text.spec.js index caefa0874..4eeb87ce8 100644 --- a/packages/bruno-lang/v2/tests/text.spec.js +++ b/packages/bruno-lang/v2/tests/text.spec.js @@ -1,7 +1,7 @@ /** * This test file is used to test the text parser. */ -const parser = require("../src/index"); +const parser = require("../src/bruToJson"); describe("script parser", () => { it("should parse script body", () => {