-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.js
More file actions
84 lines (73 loc) · 2.37 KB
/
handler.js
File metadata and controls
84 lines (73 loc) · 2.37 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
// Begin ---------------------------------------------------------------
/**
* Handler to call the transpilation.
*
* @param {string} typescriptSource
* @param {string} actionName
* @returns {Properties}
*
* @author Stefan Schnell <mail@stefan-schnell.de>
* @license MIT
* @version 0.7.0
*
* Checked with Aria Automation 8.13.1, 8.16.0 and 8.18.0
*/
exports.handler = (context, inputs, callback) => {
"use strict";
const os = require("node:os");
const fs = require("node:fs");
const typescript = require('./typescript.js');
// Compiler options for transpilation to JavaScript for the Rhino engine
const compilerOptions = {
module: typescript.ModuleKind.CommonJS,
target: typescript.ScriptTarget.ES5,
strictNullChecks: true
};
// Checking the TypeScript code and error output if present
const inMemoryFileName = `${os.tmpdir()}/${inputs.actionName}.ts`;
try {
fs.accessSync(fileName, fs.constants.F_OK);
fs.rmSync(fileName, { force: true } );
} catch (exception) {
}
const host = typescript.createCompilerHost(compilerOptions);
host.writeFile(inMemoryFileName, inputs.typescriptSource);
const program =
typescript.createProgram([inMemoryFileName], compilerOptions, host);
const emitResult = program.emit();
const allDiagnostics =
typescript.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
let diagnosticMessages = "";
if (allDiagnostics.length > 0) {
allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
let { line, character } = typescript.getLineAndCharacterOfPosition(
diagnostic.file,
diagnostic.start
);
let message = typescript.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n"
);
diagnosticMessages +=
`[Line ${line + 1}, Column ${character + 1}] ${message}\n`;
} else {
diagnosticMessages += typescript.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n"
);
}
});
}
// Transpile TypeScript to JavaScript
const javascriptSource = typescript.transpileModule(
inputs.typescriptSource,
compilerOptions
);
callback(undefined, {
status: "done",
javascriptSource: javascriptSource.outputText,
diagnosticMessages: diagnosticMessages
});
};
// End -----------------------------------------------------------------