mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-22 16:03:39 +01:00
feat(#190): Console Logs in scripts should be written into developer console
This commit is contained in:
parent
d3a56fdc82
commit
f9ed68843d
@ -119,6 +119,9 @@ const useCollectionTreeSync = () => {
|
|||||||
const removeListener6 = ipcRenderer.on('main:collection-renamed', _collectionRenamed);
|
const removeListener6 = ipcRenderer.on('main:collection-renamed', _collectionRenamed);
|
||||||
const removeListener7 = ipcRenderer.on('main:run-folder-event', _runFolderEvent);
|
const removeListener7 = ipcRenderer.on('main:run-folder-event', _runFolderEvent);
|
||||||
const removeListener8 = ipcRenderer.on('main:run-request-event', _runRequestEvent);
|
const removeListener8 = ipcRenderer.on('main:run-request-event', _runRequestEvent);
|
||||||
|
const removeListener9 = ipcRenderer.on('main:console-log', (val) => {
|
||||||
|
console[val.type](...val.args);
|
||||||
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
removeListener1();
|
removeListener1();
|
||||||
@ -129,6 +132,7 @@ const useCollectionTreeSync = () => {
|
|||||||
removeListener6();
|
removeListener6();
|
||||||
removeListener7();
|
removeListener7();
|
||||||
removeListener8();
|
removeListener8();
|
||||||
|
removeListener9();
|
||||||
};
|
};
|
||||||
}, [isElectron]);
|
}, [isElectron]);
|
||||||
};
|
};
|
||||||
|
@ -78,6 +78,15 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const cancelTokenUid = uuid();
|
const cancelTokenUid = uuid();
|
||||||
const requestUid = 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', {
|
mainWindow.webContents.send('main:run-request-event', {
|
||||||
type: 'request-queued',
|
type: 'request-queued',
|
||||||
requestUid,
|
requestUid,
|
||||||
@ -124,7 +133,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const requestScript = get(request, 'script.req');
|
const requestScript = get(request, 'script.req');
|
||||||
if(requestScript && requestScript.length) {
|
if(requestScript && requestScript.length) {
|
||||||
const scriptRuntime = new ScriptRuntime();
|
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', {
|
mainWindow.webContents.send('main:script-environment-update', {
|
||||||
envVariables: result.envVariables,
|
envVariables: result.envVariables,
|
||||||
@ -204,7 +213,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const responseScript = get(request, 'script.res');
|
const responseScript = get(request, 'script.res');
|
||||||
if(responseScript && responseScript.length) {
|
if(responseScript && responseScript.length) {
|
||||||
const scriptRuntime = new ScriptRuntime();
|
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', {
|
mainWindow.webContents.send('main:script-environment-update', {
|
||||||
envVariables: result.envVariables,
|
envVariables: result.envVariables,
|
||||||
@ -233,7 +242,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if(testFile && testFile.length) {
|
if(testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
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', {
|
mainWindow.webContents.send('main:run-request-event', {
|
||||||
type: 'test-results',
|
type: 'test-results',
|
||||||
@ -291,7 +300,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if(testFile && testFile.length) {
|
if(testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
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', {
|
mainWindow.webContents.send('main:run-request-event', {
|
||||||
type: 'test-results',
|
type: 'test-results',
|
||||||
@ -374,6 +383,15 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const collectionPath = collection.pathname;
|
const collectionPath = collection.pathname;
|
||||||
const folderUid = folder ? folder.uid : null;
|
const folderUid = folder ? folder.uid : null;
|
||||||
|
|
||||||
|
const onConsoleLog = (type, args) => {
|
||||||
|
console[type](...args);
|
||||||
|
|
||||||
|
mainWindow.webContents.send('main:console-log', {
|
||||||
|
type,
|
||||||
|
args
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if(!folder) {
|
if(!folder) {
|
||||||
folder = collection;
|
folder = collection;
|
||||||
}
|
}
|
||||||
@ -447,7 +465,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const requestScript = get(request, 'script.req');
|
const requestScript = get(request, 'script.req');
|
||||||
if(requestScript && requestScript.length) {
|
if(requestScript && requestScript.length) {
|
||||||
const scriptRuntime = new ScriptRuntime();
|
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', {
|
mainWindow.webContents.send('main:script-environment-update', {
|
||||||
envVariables: result.envVariables,
|
envVariables: result.envVariables,
|
||||||
@ -504,7 +522,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const responseScript = get(request, 'script.res');
|
const responseScript = get(request, 'script.res');
|
||||||
if(responseScript && responseScript.length) {
|
if(responseScript && responseScript.length) {
|
||||||
const scriptRuntime = new ScriptRuntime();
|
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', {
|
mainWindow.webContents.send('main:script-environment-update', {
|
||||||
envVariables: result.envVariables,
|
envVariables: result.envVariables,
|
||||||
@ -531,7 +549,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if(testFile && testFile.length) {
|
if(testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
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', {
|
mainWindow.webContents.send('main:run-folder-event', {
|
||||||
type: 'test-results',
|
type: 'test-results',
|
||||||
@ -594,7 +612,7 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
|
||||||
if(testFile && testFile.length) {
|
if(testFile && testFile.length) {
|
||||||
const testRuntime = new TestRuntime();
|
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', {
|
mainWindow.webContents.send('main:run-folder-event', {
|
||||||
type: 'test-results',
|
type: 'test-results',
|
||||||
|
@ -26,13 +26,29 @@ class ScriptRuntime {
|
|||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath){
|
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath, onConsoleLog){
|
||||||
const bru = new Bru(envVariables, collectionVariables);
|
const bru = new Bru(envVariables, collectionVariables);
|
||||||
const req = new BrunoRequest(request);
|
const req = new BrunoRequest(request);
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
bru,
|
bru,
|
||||||
req
|
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({
|
const vm = new NodeVM({
|
||||||
sandbox: context,
|
sandbox: context,
|
||||||
require: {
|
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 bru = new Bru(envVariables, collectionVariables);
|
||||||
const req = new BrunoRequest(request);
|
const req = new BrunoRequest(request);
|
||||||
const res = new BrunoResponse(response);
|
const res = new BrunoResponse(response);
|
||||||
@ -81,6 +97,21 @@ class ScriptRuntime {
|
|||||||
req,
|
req,
|
||||||
res
|
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({
|
const vm = new NodeVM({
|
||||||
sandbox: context,
|
sandbox: context,
|
||||||
require: {
|
require: {
|
||||||
|
@ -20,7 +20,7 @@ class TestRuntime {
|
|||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath) {
|
runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog){
|
||||||
const bru = new Bru(envVariables, collectionVariables);
|
const bru = new Bru(envVariables, collectionVariables);
|
||||||
const req = new BrunoRequest(request);
|
const req = new BrunoRequest(request);
|
||||||
const res = new BrunoResponse(response);
|
const res = new BrunoResponse(response);
|
||||||
@ -47,6 +47,20 @@ class TestRuntime {
|
|||||||
__brunoTestResults: __brunoTestResults
|
__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({
|
const vm = new NodeVM({
|
||||||
sandbox: context,
|
sandbox: context,
|
||||||
require: {
|
require: {
|
||||||
|
Loading…
Reference in New Issue
Block a user