This repository has been archived by the owner on Dec 23, 2018. It is now read-only.
/
command.api.js
115 lines (82 loc) · 2.1 KB
/
command.api.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
var Structr = require('structr'),
_ = require("underscore"),
dsync = require("dsync"),
traverse = require("traverse");
exports.name = "command.api";
exports.plugin = function(cli) {
var target = {};
function normalizeApi(target) {
if(target.desc != undefined && target.exec) return target;
var tot = typeof target;
if(/string|number/.test(tot)) return target;
for(var prop in target) {
var v = target[prop];
if(typeof v == 'function') {
target[prop] = {
exec: v,
desc: ''
}
} else {
normalizeApi(v);
}
}
return target;
}
function flattenMethods(target, path) {
var methods = [];
if(target.desc != undefined && target.exec) {
var pt = path.join('.'),
cmd = target.exec.toString().match(/function\s*\w*(\(.*?\))/)[1];
if(target.args) cmd = cmd.replace(/\(.*?\)/,'('+target.args.join(', ')+')');
methods.push( { path: pt,
command: pt + cmd,
func: target.exec,
desc: target.desc,
ignoreHelp: target.ignoreHelp });
return methods;
}
for(var prop in target) {
var v = target[prop];
if(typeof v == 'object') {
var subPath = path.concat();
subPath.push(prop);
methods = methods.concat(flattenMethods(v, subPath));
}
}
return methods;
}
function demormalizeApi(api) {
if(typeof api.exec == 'function') {
return api.exec;
}
for(var prop in api) {
api[prop] = demormalizeApi(api[prop]);
}
return api;
}
cli.onJs = function(api) {
var wapi = dsync(api);
/*flattenMethods(normalizeApi(wapi), []).forEach(function(cmd) {
cli.addHelp(cmd);
});*/
// console.log(demormalizeApi(api).cli.toString())
// Structr.copy(demormalizeApi(api), target);
_.extend(target, wapi);
console.log(target)
}
return {
testListener: function(type) {
return type.indexOf('(') > -1 && type.indexOf('.');
},
run: function(buffer, data) {
try {
var ret = eval("target." + buffer);
if(ret != undefined) console.log(ret);
return true;
} catch(e) {
console.log('\nUnable to call %s \n%s', buffer.bold, e.message.red.bold);
return false;
}
}
}
}