-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·54 lines (50 loc) · 1.63 KB
/
index.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
#!/usr/bin/env node
"use strict"
const Flow = require(__dirname + "/node_modules/flow");
const fs = require("fs");
const presolve = require("path").resolve;
const promisify = require("util").promisify;
const exec = promisify(require("child_process").exec);
const access = promisify(fs.access);
const read = promisify(fs.readFile);
const sequence_id = process.argv[2];
const app_base_path = presolve(process.argv[3] || ".");
const role = process.argv[4];
const cache = {};
if (!sequence_id) {
process.stderr.write("Start sequence missing.\n");
process.exit(0);
}
process.env.NODE_PATH = app_base_path + "/node_modules";
require("module").Module._initPaths();
global.require = require;
Flow({
abp: app_base_path,
set: (key, val) => {
return cache[key] = val;
},
get: (key) => {
return cache[key];
},
del: (key) => {
delete cache[key];
},
seq: (sequence_id, role) => {
return read(app_base_path + "/sequences/" + sequence_id + ".json").then(JSON.parse);
},
fnc: (fn_iri, role) => {
return read(app_base_path + "/handlers/" + fn_iri + ".js").then((script) => {
return new Function("Adapter", "flow", script.toString());
});
},
dep: (name, dependency, role) => {
return access(presolve(app_base_path + "/node_modules/" + name))
.catch((err) => {
return err.code === "ENOENT" ? exec("npm i --production --prefix " + app_base_path + " " + dependency.trim()) : Promise.reject(err);
});
}
})(sequence_id, role)
.catch((err) => {
err = err.stack ? err.stack : err;
process.stderr.write(err.toString() + "\n");
});