-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexec_termux.js
47 lines (40 loc) · 905 Bytes
/
exec_termux.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
var child_process = require('child_process');
const VOID = function (res) {
return true;
}
const OBJ = function (res) {
return JSON.parse(res);
}
const STR = function (res) {
return res.substr(0,res.length-1);
}
const execTermux = function (cmd, opts, parser) {
if (opts === undefined) {
var opts = [];
}
if (parser === undefined) {
var parser = OBJ;
}
return new Promise(function (resolve, reject) {
child_process.execFile(cmd, opts, function (err, out, code) {
console.log('Command:',cmd,opts);
try{
var res = parser(out);
if (typeof res === 'object' && res.error !== undefined) {
reject(res.error);
}else{
resolve(res);
}
}catch(e){
console.log('Termux Error:',out);
reject(e);
}
});
})
};
module.exports = {
VOID: VOID,
STR: STR,
OBJ: OBJ,
exec: execTermux
};