-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinvokeTypeScript.js
More file actions
161 lines (140 loc) · 4.54 KB
/
invokeTypeScript.js
File metadata and controls
161 lines (140 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Begin ---------------------------------------------------------------
/**
* Invokes TypeScript code which is stored in an action.
*
* @function invokeTypeScript
*
* @param {string} in_moduleName - Module name which contains the action
* @param {string} in_actionName - Action name which contains the TypeScript code
* @param {string} in_parameters - Parameters as JSON string
* @param {boolean} in_showOutput - Flag whether output should be displayed
* @returns {Any} result - Result of TypeScript code execution
*
* @author Stefan Schnell <mail@stefan-schnell.de>
* @license MIT
* @version 0.7.0
*
* @example
* var in_moduleName = "de.stschnell";
* var in_actionName = "testTypeScript";
* var in_params = "{\"in_name\": \"\\\"Stefan\\\"\", \"in_age\": 42}";
* var in_showOutput = false;
*
* Set com.vmware.scripting.javascript.allow-native-object in the
* system properties to true.
*
* Checked with Aria Automation 8.16.0 and 8.18.0
*/
var _invokeTypeScriptNS = {
main : function(moduleName, actionName, parameters, showOutput) {
if (typeof parameters !== "string") {
parameters = "";
}
if (typeof showOutput !== "boolean") {
showOutput = false;
}
// var cx = org.mozilla.javascript.ContextFactory.getGlobal().enterContext();
var cx = org.mozilla.javascript.Context.enter();
try {
// Reads the TypeScript source
var typescriptSource = System.getModule("de.stschnell")
.getActionAsText(moduleName, actionName);
if (showOutput) {
System.log(typescriptSource);
}
if (typescriptSource.trim() !== "") {
// Transpiles the TypeScript to JavaScript
var transpileResult = System.getModule("de.stschnell.typescript")
.transpileTypeScriptToJavaScript(typescriptSource, actionName);
if (showOutput) {
System.log(transpileResult.diagnosticMessages);
}
var javascriptSource = transpileResult.javascriptSource;
// Add input parameters to JavaScript code
if (parameters.trim() !== "") {
var addParameters = "";
var jsonParameters = JSON.parse(parameters);
Object.keys(jsonParameters).forEach( function(key) {
var parameter = "var " + key + " = " + jsonParameters[key] + ";";
addParameters += parameter + "\n";
});
javascriptSource = addParameters + "\n" + javascriptSource;
}
if (showOutput) {
System.log(javascriptSource);
}
// Executes the JavaScript
var scope = cx.initStandardObjects();
var sourceName = "export";
var lineNumber = 1;
var securityDomain = null;
var result = cx.evaluateString(
scope,
javascriptSource,
sourceName,
lineNumber,
securityDomain
);
if (showOutput) {
switch (typeof result) {
case "string" :
case "number" :
case "boolean" :
System.log(String(result));
break;
case "function" :
break;
case "object" :
if (result.constructor !== undefined) {
switch (result.constructor.name) {
case "String" :
case "Number" :
case "Boolean" :
System.log(result);
break;
case "Array" :
System.log("Array: " + String(result));
break;
case "Date" :
System.log("Date: " + String(result));
break;
case "Function" :
break;
case "Object" :
System.log(JSON.stringify(result));
break;
case "RegExp" :
System.log("RegExp: " + String(result));
break;
default :
break;
}
}
break;
default :
break;
}
}
return result;
}
} catch (exception) {
System.log(exception);
} finally {
cx.exit();
}
}
};
if (
String(in_moduleName).trim() !== "" &&
String(in_actionName).trim() !== ""
) {
return _invokeTypeScriptNS.main(
in_moduleName,
in_actionName,
in_parameters,
in_showOutput
);
} else {
throw new Error("in_moduleName or in_actionName argument can not be null");
}
// End -----------------------------------------------------------------