-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (68 loc) · 2.06 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
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
const fs = require("fs");
const path = require("path");
const { DSLink, RootNode, ActionNode, Permission, DsError } = require("dslink");
class ExportNodes extends ActionNode {
constructor(path, provider) {
super(path, provider,
Permission.CONFIG
);
}
initialize() {
this.setConfig('$params', [{ name: 'dslink', type: 'string' }]);
this.setConfig('$columns', [{ name: 'output', type: 'string' }]);
}
onInvoke(params) {
let { dslink } = params;
if (typeof dslink !== 'string' || dslink.includes('.')) {
return new DsError('invalidInput', { msg: 'invalid dslink' });
}
let nodeJsonPath = path.join(__dirname, `../${dslink}/nodes.json`);
try {
let data = fs.readFileSync(nodeJsonPath, { encoding: 'utf8' });
return { output: data };
} catch (e) {
return new DsError('failed', { msg: 'failed to open nodes.json' });
}
}
}
class ImportNodes extends ActionNode {
constructor(path, provider) {
super(path, provider,
Permission.CONFIG
);
}
initialize() {
this.setConfig('$params', [{ name: 'dslink', type: 'string' }, { name: 'data', type: 'string' }]);
}
onInvoke(params) {
let { dslink, data } = params;
if (typeof dslink !== 'string' || dslink.includes('.')) {
return new DsError('invalidInput', { msg: 'invalid dslink' });
}
try {
let parsedData = JSON.parse(data);
if (parsedData.constructor !== Object) {
throw new Error();
}
} catch (e) {
return new DsError('invalidInput', { msg: 'invalid data' });
}
let nodeJsonPath = path.join(__dirname, `../${dslink}/nodes.json`);
try {
fs.writeFileSync(nodeJsonPath, data);
} catch (e) {
return new DsError('failed', { msg: 'failed to save nodes.json' });
}
}
}
function main() {
// create a root node
let rootNode = new RootNode();
// add child to root
rootNode.createChild('export', ExportNodes);
rootNode.createChild('import', ImportNodes);
// create the link
let link = new DSLink('export-link', { rootNode });
link.connect();
}
main();