feat(#190): Console Logs in scripts should be written into developer console

This commit is contained in:
Anoop M D 2023-09-14 00:20:04 +05:30
parent d3a56fdc82
commit f9ed68843d
4 changed files with 78 additions and 11 deletions

View File

@ -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]);
};

View File

@ -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',

View File

@ -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: {

View File

@ -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: {