-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpterodactyl_execute.js
103 lines (94 loc) · 2.83 KB
/
pterodactyl_execute.js
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
import { parse } from "./src/deps.js";
import { needsFilePrefix } from "./src/utils.js";
import * as _ from "./pterodactyl.js";
const AsyncFunction = (async () => {}).constructor;
function getNameFromFunction(f) {
if (!f.name) {
throw "Functions must be named";
}
return f.name;
}
function collectInputFile(filename, typeFunction) {
// try to read and parse as json if not treat as string
const fileContent = Deno.readTextFileSync(filename);
if (typeFunction) {
return typeFunction(fileContent);
}
try {
return JSON.parse(fileContent);
} catch {
return fileContent;
}
}
function collectInputs(inputdir, f, options) {
if (options?.paramNames && f.length != options.paramNames.length) {
throw "Provided paramNames and function parameter count do not match";
}
const inputs = new Array(f.length);
for (let i = 0; i < f.length; i++) {
inputs[i] = collectInputFile(
options?.paramNames
? `${inputdir}/${options.paramNames[i]}`
: `${inputdir}/input${i}`,
options?.paramTypes?.[i],
);
}
return inputs;
}
function writeOutput(outputdir, output, options) {
let serializationFunction = options?.outputType === String
? (v) => v
: JSON.stringify;
const fileOutput = serializationFunction(output);
const outputName = options?.outputName ?? "output0";
Deno.writeTextFileSync(`${outputdir}/${outputName}`, fileOutput);
}
function handleTaskSeenInImport(
taskSeen,
taskName,
f,
options,
) {
const functionName = getNameFromFunction(f);
if (functionName == taskName && taskSeen.length == 0) {
taskSeen.push([f, options]);
}
return f;
}
async function handleTaskExecution(inputdir, outputdir, f, options) {
const inputs = collectInputs(inputdir, f, options);
const consistentFunc = f instanceof AsyncFunction
? f
: async (...inputs) => f(...inputs);
const output = await consistentFunc(...inputs);
writeOutput(outputdir, output, options);
}
if (import.meta.main) {
const { pkgs, task, inputdir, outputdir } = parse(Deno.args);
if (!pkgs) {
console.warn("Must pass a file path to the workflow with `--pkgs`");
Deno.exit(1);
}
if (!task) {
console.warn("Must pass a task name `--task`");
Deno.exit(1);
}
if (!inputdir) {
console.warn("Must pass an input directory `--inputdir`");
Deno.exit(1);
}
if (!outputdir) {
console.warn("Must pass an input directory `--outputdir`");
Deno.exit(1);
}
const taskSeen = [];
globalThis.pterodactylConfig.taskTransformer = (f, options) => {
return handleTaskSeenInImport(taskSeen, task, f, options);
};
const userWorkflowPath = needsFilePrefix(pkgs)
? `file://${Deno.cwd()}/${pkgs}`
: pkgs;
const userWorkflow = await import(userWorkflowPath);
const [[taskFunction, options]] = taskSeen;
await handleTaskExecution(inputdir, outputdir, taskFunction, options);
}