Update javascript executor to work w/ compiled ts scripts that export a main function; add output to javalin storeRecordAssociatedScript

This commit is contained in:
2022-11-09 09:49:58 -06:00
parent 2975b90505
commit 955294ae18
3 changed files with 42 additions and 2 deletions

View File

@ -80,11 +80,20 @@ public class QJavaScriptExecutor implements QCodeExecutor
////////////////////////////////////////////////////////////////////////
// wrap the user's code in an immediately-invoked function expression //
// if the user's code (%s below) returns - then our IIFE is done. //
// if the user's code doesn't return, but instead created a 'script' //
// variable, with a 'main' function on it (e.g., from a compiled //
// type script file), then call main function and return its result. //
////////////////////////////////////////////////////////////////////////
String codeToRun = """
(function userDefinedFunction()
{
%s
%s
if(script && script.main)
{
return (script.main());
}
})();
""".formatted(code);

View File

@ -100,6 +100,34 @@ class ExecuteCodeActionTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testCompiledFromTypeScript() throws QException
{
OneTestOutput oneTestOutput = testOne(4, """
var script = (function (exports) {
function cooling() {
output.setD(7);
return (output);
}
cooling();
exports.cooling = cooling;
return exports;
})({});
""");
assertEquals(7, oneTestOutput.testOutput().getD());
assertTrue(oneTestOutput.executeCodeOutput().getOutput() instanceof TestOutput);
assertEquals(7, ((TestOutput) oneTestOutput.executeCodeOutput().getOutput()).getD());
}
/*******************************************************************************
**
*******************************************************************************/

View File

@ -109,6 +109,7 @@ import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
import io.javalin.Javalin;
import io.javalin.apibuilder.EndpointGroup;
import io.javalin.http.ContentType;
import io.javalin.http.Context;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
@ -1113,6 +1114,8 @@ public class QJavalinImplementation
*******************************************************************************/
private static void storeRecordAssociatedScript(Context context)
{
context.contentType(ContentType.APPLICATION_JSON);
try
{
StoreAssociatedScriptInput input = new StoreAssociatedScriptInput(qInstance);
@ -1128,7 +1131,7 @@ public class QJavalinImplementation
StoreAssociatedScriptAction storeAssociatedScriptAction = new StoreAssociatedScriptAction();
storeAssociatedScriptAction.run(input, output);
context.result(JsonUtils.toJson("OK"));
context.result(JsonUtils.toJson(output));
}
catch(Exception e)
{