From f9ed68843d1b74477a3ae13e6e96d8e4f2b53b93 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Thu, 14 Sep 2023 00:20:04 +0530 Subject: [PATCH] feat(#190): Console Logs in scripts should be written into developer console --- .../providers/App/useCollectionTreeSync.js | 4 +++ .../bruno-electron/src/ipc/network/index.js | 34 +++++++++++++----- .../bruno-js/src/runtime/script-runtime.js | 35 +++++++++++++++++-- packages/bruno-js/src/runtime/test-runtime.js | 16 ++++++++- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/packages/bruno-app/src/providers/App/useCollectionTreeSync.js b/packages/bruno-app/src/providers/App/useCollectionTreeSync.js index b5d7e9393..99739ebb8 100644 --- a/packages/bruno-app/src/providers/App/useCollectionTreeSync.js +++ b/packages/bruno-app/src/providers/App/useCollectionTreeSync.js @@ -119,6 +119,9 @@ const useCollectionTreeSync = () => { const removeListener6 = ipcRenderer.on('main:collection-renamed', _collectionRenamed); const removeListener7 = ipcRenderer.on('main:run-folder-event', _runFolderEvent); const removeListener8 = ipcRenderer.on('main:run-request-event', _runRequestEvent); + const removeListener9 = ipcRenderer.on('main:console-log', (val) => { + console[val.type](...val.args); + }); return () => { removeListener1(); @@ -129,6 +132,7 @@ const useCollectionTreeSync = () => { removeListener6(); removeListener7(); removeListener8(); + removeListener9(); }; }, [isElectron]); }; diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 578bf4ed9..6a80ccbc8 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -78,6 +78,15 @@ const registerNetworkIpc = (mainWindow) => { const cancelTokenUid = uuid(); const requestUid = uuid(); + const onConsoleLog = (type, args) => { + console[type](...args); + + mainWindow.webContents.send('main:console-log', { + type, + args + }); + }; + mainWindow.webContents.send('main:run-request-event', { type: 'request-queued', requestUid, @@ -124,7 +133,7 @@ const registerNetworkIpc = (mainWindow) => { const requestScript = get(request, 'script.req'); if(requestScript && requestScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = await scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, @@ -204,7 +213,7 @@ const registerNetworkIpc = (mainWindow) => { const responseScript = get(request, 'script.res'); if(responseScript && responseScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = await scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, @@ -233,7 +242,7 @@ const registerNetworkIpc = (mainWindow) => { const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); if(testFile && testFile.length) { const testRuntime = new TestRuntime(); - const testResults = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath); + const testResults = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:run-request-event', { type: 'test-results', @@ -291,7 +300,7 @@ const registerNetworkIpc = (mainWindow) => { const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); if(testFile && testFile.length) { const testRuntime = new TestRuntime(); - const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath); + const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:run-request-event', { type: 'test-results', @@ -374,6 +383,15 @@ const registerNetworkIpc = (mainWindow) => { const collectionPath = collection.pathname; const folderUid = folder ? folder.uid : null; + const onConsoleLog = (type, args) => { + console[type](...args); + + mainWindow.webContents.send('main:console-log', { + type, + args + }); + }; + if(!folder) { folder = collection; } @@ -447,7 +465,7 @@ const registerNetworkIpc = (mainWindow) => { const requestScript = get(request, 'script.req'); if(requestScript && requestScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = await scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, @@ -504,7 +522,7 @@ const registerNetworkIpc = (mainWindow) => { const responseScript = get(request, 'script.res'); if(responseScript && responseScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = await scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, @@ -531,7 +549,7 @@ const registerNetworkIpc = (mainWindow) => { const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); if(testFile && testFile.length) { const testRuntime = new TestRuntime(); - const testResults = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath); + const testResults = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:run-folder-event', { type: 'test-results', @@ -594,7 +612,7 @@ const registerNetworkIpc = (mainWindow) => { const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); if(testFile && testFile.length) { const testRuntime = new TestRuntime(); - const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath); + const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath, onConsoleLog); mainWindow.webContents.send('main:run-folder-event', { type: 'test-results', diff --git a/packages/bruno-js/src/runtime/script-runtime.js b/packages/bruno-js/src/runtime/script-runtime.js index 30ae3e7a1..81f5cec9a 100644 --- a/packages/bruno-js/src/runtime/script-runtime.js +++ b/packages/bruno-js/src/runtime/script-runtime.js @@ -26,13 +26,29 @@ class ScriptRuntime { constructor() { } - async runRequestScript(script, request, envVariables, collectionVariables, collectionPath){ + async runRequestScript(script, request, envVariables, collectionVariables, collectionPath, onConsoleLog){ const bru = new Bru(envVariables, collectionVariables); const req = new BrunoRequest(request); + const context = { bru, req }; + + if(onConsoleLog && typeof onConsoleLog === 'function') { + const customLogger = (type) => { + return (...args) => { + onConsoleLog(type, args); + } + }; + context.console = { + log: customLogger('log'), + info: customLogger('info'), + warn: customLogger('warn'), + error: customLogger('error') + } + } + const vm = new NodeVM({ sandbox: context, require: { @@ -71,7 +87,7 @@ class ScriptRuntime { }; } - async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath) { + async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog){ const bru = new Bru(envVariables, collectionVariables); const req = new BrunoRequest(request); const res = new BrunoResponse(response); @@ -81,6 +97,21 @@ class ScriptRuntime { req, res }; + + if(onConsoleLog && typeof onConsoleLog === 'function') { + const customLogger = (type) => { + return (...args) => { + onConsoleLog(type, args); + } + }; + context.console = { + log: customLogger('log'), + info: customLogger('info'), + warn: customLogger('warn'), + error: customLogger('error') + } + } + const vm = new NodeVM({ sandbox: context, require: { diff --git a/packages/bruno-js/src/runtime/test-runtime.js b/packages/bruno-js/src/runtime/test-runtime.js index 1fe9f2027..4435d9d3a 100644 --- a/packages/bruno-js/src/runtime/test-runtime.js +++ b/packages/bruno-js/src/runtime/test-runtime.js @@ -20,7 +20,7 @@ class TestRuntime { constructor() { } - runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath) { + runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog){ const bru = new Bru(envVariables, collectionVariables); const req = new BrunoRequest(request); const res = new BrunoResponse(response); @@ -47,6 +47,20 @@ class TestRuntime { __brunoTestResults: __brunoTestResults }; + if(onConsoleLog && typeof onConsoleLog === 'function') { + const customLogger = (type) => { + return (...args) => { + onConsoleLog(type, args); + } + }; + context.console = { + log: customLogger('log'), + info: customLogger('info'), + warn: customLogger('warn'), + error: customLogger('error') + } + } + const vm = new NodeVM({ sandbox: context, require: {