tweaked script node

This commit is contained in:
John Andrews
2022-05-31 18:28:08 +12:00
parent 0c84679240
commit 51bc02bd01
24 changed files with 33 additions and 2 deletions

Binary file not shown.

View File

@@ -1,5 +1,6 @@
using FileFlows.Plugin;
using Jint;
using Jint.Runtime;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
@@ -71,13 +72,28 @@ internal class JavascriptExecutor
})
.SetValue("Logger", args.Logger)
.SetValue("Variables", args.Variables)
.SetValue("Flow", args);
.SetValue("Flow", args)
.SetValue(nameof(FileInfo), new Func<string, FileInfo>((string file) => new FileInfo(file)))
.SetValue(nameof(DirectoryInfo), new Func<string, DirectoryInfo>((string path) => new DirectoryInfo(path))); ;
foreach (var arg in execArgs.AdditionalArguments)
engine.SetValue(arg.Key, arg.Value);
var result = int.Parse(engine.Evaluate(tcode).ToObject().ToString());
return result;
}
catch(JavaScriptException ex)
{
// print out the code block for debugging
int lineNumber = 0;
var lines = execArgs.Code.Split('\n');
string pad = "D" + (lines.ToString().Length);
args.Logger.DLog("Code: " + Environment.NewLine +
string.Join("\n", lines.Select(x => (++lineNumber).ToString("D3") + ": " + x)));
args.Logger?.ELog($"Failed executing script [{ex.LineNumber}, {ex.Column}]: {ex.Message}");
return -1;
}
catch (Exception ex)
{
args.Logger?.ELog("Failed executing function: " + ex.Message + Environment.NewLine + ex.StackTrace);

View File

@@ -45,7 +45,8 @@ public class ScriptNode:Node
var execArgs = new JavascriptExecutionArgs
{
Args = args,
Code = Code + "\n\n" + entryPoint
//Code = ("try\n{\n\t" + Code.Replace("\n", "\n\t") + "\n\n\t" + entryPoint + "\n} catch (err) { \n\tLogger.ELog(`Error in script [${err.line}]: ${err}`);\n\treturn -1;\n}").Replace("\t", " ")
Code = (Code + "\n\n" + entryPoint).Replace("\t", " ").Trim()
};
if (script.Parameters?.Any() == true)