-
Notifications
You must be signed in to change notification settings - Fork 3
/
civicrm-cv.js
53 lines (46 loc) · 1.26 KB
/
civicrm-cv.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
var execPromise = require('child-process-promise').exec;
var execSync = require('child_process').execSync;
var escape = function(cmd) {
return '\'' + cmd.replace(/'/g, "'\\''") + '\'';
};
function serializeArgs(args) {
var argsStr = '';
for (var i = 0; i < args.length; i++) {
argsStr += ' ' + escape(args[i]);
}
return argsStr;
}
var jsonExecFuncs = {
sync: function jsonExecSync(cmd, env) {
var result = execSync(cmd, {env: env});
return JSON.parse(result.toString());
},
promise: function jsonExecPromise(cmd, env) {
return execPromise(cmd, {env: env}).then(function(result) {
return JSON.parse(result.stdout);
});
}
};
module.exports = function(options) {
if (!options || options.mode === undefined) {
throw "civicrm-cv: Please specify \'mode\' option.";
}
if (!jsonExecFuncs[options.mode]) {
throw "civicrm-cv: Invalid \'mode\' option";
}
return function(subcommand) {
var cmd;
if (typeof subcommand === 'string') {
cmd = 'cv ' + subcommand;
}
else {
cmd = 'cv' + serializeArgs(subcommand);
}
var env = {};
for (var key in process.env) {
env[key] = process.env[key];
}
env.CV_OUTPUT = 'json';
return jsonExecFuncs[options.mode].apply(null, [cmd, env]);
};
};