-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync-wrapper.js
executable file
·137 lines (124 loc) · 4.79 KB
/
rsync-wrapper.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
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
const rsync = require('rsyncwrapper')
var debugNode = null
module.exports = function(RED) {
function rsyncNode(n) {
RED.nodes.createNode(this, n);
this.source = n.source
this.sourceType = n.sourceType
this.destination = n.destination
this.destinationType = n.destinationType
this.exclude = n.exclude
this.excludeType = n.excludeType
this.include = n.include
this.includeType = n.includeType
this.dryrun = n.dryrun
this.verbose = n.verbose
this.recursive = n.recursive
this.extra = n.extra
this.extraType = n.extraType
var node = this;
debugNode = node;
node.on('input', function(msg) {
rsyncRun(node, msg, (errs, msg) => {
if (errs.length > 0) {
node.error(errs.join("; "), msg);
} else if (msg) {
node.send(msg);
}
})
});
}
RED.nodes.registerType("rsync", rsyncNode);
async function rsyncRun(node, msg, done) {
var useVerbose = msg.verbose || node.verbose
var useRecursive = msg.recursive || node.recursive
var useDryrun = msg.dryrun || node.dryrun
var { err: sourceErr, value: source } = await getArray("source", node.source, node.sourceType, msg);
var { err: destinationErr, value: destination } = await getArray("destination", node.destination, node.destinationType, msg);
var { err: excludeErr, value: exclude } = await getArray("exclude", node.exclude, node.excludeType, msg)
var { err: includeErr, value: include } = await getArray("include", node.include, node.includeType, msg)
var verbose = useVerbose === true
var recursive = useRecursive === true
var dryrun = useDryrun === true
var { err: extraErr, value: extra } = await getArray("extra", node.extra, node.extraType, msg)
var errs = [sourceErr, destinationErr, excludeErr, includeErr, extraErr]
.filter(err => err !== null)
if (errs.length > 0) {
debug(errs.length+" errors found")
done(errs, msg);
return
}
var args = []
if (verbose === true) args.push('-v')
if (source.find( el => el.indexOf("./") >= 0)) args.push('--relative')
var command = {
src: source,
dest: destination[0] || "",
exclude: exclude,
include: include,
recursive: recursive,
dryRun: dryrun,
args: args.concat(extra),
}
rsync(command, function(error, stdout, stderr, cmd) {
if (error !== null) msg.error = error
if (stdout !== "") msg.stdout = stdout
if (stderr !== "") msg.stderr = stderr
msg.cmd = cmd
msg.rsync = command
if (error) {
done([error], msg)
} else {
done([], msg)
}
})
}
async function getArray(name, input, inputType, msg) {
if ((input === null) || (input === undefined)) {
return { err: null, value: null }
} else if (['msg', 'flow', 'global'].includes(inputType)) {
try {
var value = await getProperty(name, input, inputType, msg)
return { err: null, value: (Array.isArray(value) ? value : [value]) }
} catch (e) {
return { err: e, value: null }
}
} else if (inputType === "jsonata") {
var expr = RED.util.prepareJSONataExpression(input, this);
try {
var value = await getJSONata(name, input, msg)
return { err: null, value: (Array.isArray(value) ? value : [value]) }
} catch (e) {
return { err: e, value: null }
}
} else {
return { err: null, value: [String(input)] }
}
}
function getProperty(name, input, inputType, msg) {
return new Promise((resolve, reject) => {
RED.util.evaluateNodeProperty(input, inputType, this, msg, function(err, res) {
if (err) {
reject( { error: err.message })
} else {
resolve(res)
}
});
});
}
function getJSONata(name, input, msg) {
var expr = RED.util.prepareJSONataExpression(input, this);
return new Promise((resolve, reject) => {
RED.util.evaluateJSONataExpression(expr, msg, (err, value) => {
if (err) {
reject( { error: err.message })
} else {
resolve(value)
}
});
})
}
function debug(msg) {
if (debugNode) debugNode.warn(msg)
}
}