-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
227 lines (194 loc) · 5 KB
/
index.js
File metadata and controls
227 lines (194 loc) · 5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
'use strict';
var main_prompt = require("prompt")
, argv = require('minimist')(process.argv.slice(2))
, shell = require('shelljs')
, shell_location = require('path').dirname(process.argv[1]);
var current_version = '1.1.0';
//GLOBAL rewrites
// add remove function to array
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
}
// add find function to array
Array.prototype.find = function(search_string) {
var i = this.length;
while (i--) {
if (typeof(this[i]) != Object && search_string === this[i]) {
//found return index position
return i;
}
}
// not found
return -1;
}
var home = process.env[(process.platform == 'win32') ? "USERPROFILE" : "HOME"];
var cmd = '';
var cmd_modules = [];
var mod_context = '/';
var current_module = {};
var cmd_history = [];
//prompt initializer
main_prompt.start();
main_prompt.override = argv;
main_prompt.message = "CH Command Shell";
process.stdin.on('keypress', function (ch, key) {
//character let prompt take it
if (typeof(key) == 'undefined') return;
//console.log('got "keypress"', key);
switch(key.name) {
case 'up':
// console.log('up');
break;
case 'down':
// console.log('down');
break;
}
});
cmd_modules.findFileByName = function(search_string) {
var i = this.length;
while (i--) {
if (search_string === this[i].name) {
//found return index position
return this[i].file;
}
}
// not found
return null;
}
cmd_modules.loadModuleList = function() {
cmd_modules.length = 0;
// console.log(shell_location);
shell.ls(shell_location+'/cmdsh-modules/*.js').forEach(function(file) {
var mod = require(file);
cmd_modules.push({"name":mod.name, "file":file});
});
shell.ls(home+'/cmdsh-modules/*.js').forEach(function(file){
var mod = require(file);
cmd_modules.push({"name":mod.name, "file":file});
});
}
cmd_modules.getNames = function() {
var retArr = [];
var i = this.length;
while (i--) {
retArr.push(this[i].name);
}
return retArr;
}
cmd_modules.getHelp = function(module_name) {
var fname = cmd_modules.findFileByName(module_name);
var tmp_mod = require(fname);
return tmp_mod.help;
}
var setCurrentModule = function(module_name) {
mod_context = module_name;
current_module = require(cmd_modules.findFileByName(module_name));
}
var displayHelp = function() {
shell.echo("\n----------------------------------------");
shell.echo("Current Module: "+current_module.name);
shell.echo(current_module.help);
shell.echo("----------------------------------------");
if (mod_context == '/') {
cmd_modules.getNames().forEach(function(mod_name){
shell.echo(mod_name.blue + " - " + cmd_modules.getHelp(mod_name));
});
return;
}
for (var obj in current_module) {
if (typeof(current_module[obj]) == "object") {
if (hasOwnProperty.call(current_module[obj], "shell_name")) {
shell.echo(current_module[obj].shell_name.blue + " - " + current_module[obj].help);
}
}
}
}
var setCurrentToRoot = function() {
mod_context = '/';
current_module = {};
current_module.name = "Root";
current_module.help = "There is no module currently selected";
}
/**
* Searches to see if the first line is a module.
*/
var matchModules = function() {
var mod_pos = cmd_modules.getNames().find(cmd[0]);
if (mod_pos > -1){
setCurrentModule(cmd[0]);
return true;
}
return false;
}
/**
* Searches for a module function specified by user and executes it.
*/
var matchFunctions = function(cmd_seq) {
if (current_module.name == "Root") return false; //root has no functions
for (var obj_name in current_module) {
var shell_func = current_module[obj_name];
if (shell_func.shell_name == cmd[cmd_seq]) {
if (hasOwnProperty.call(shell_func, "runScript")) { //this function is a module function
var params = cmd.slice(cmd_seq+1);
shell_func.runScript(params);
return true;
}
}
}
return false;
}
var processCommand = function(input) {
// main_prompt.pause();
cmd_history.push(input);
cmd = input.split(' ');
cmd_modules.loadModuleList();
switch(cmd[0]) { //internal shell functions
case 'quit':
case 'exit':
return;
break;
case 'help':
case '?':
displayHelp();
shell.echo();
break;
case '/':
setCurrentToRoot();
break;
case 'version':
shell.echo("Choice Hotel Command Shell")
shell.echo('Version: '+current_version);
break;
default:
if (matchFunctions(0) == false) { // check function in current module
if (matchModules()) { // match a module
if (cmd.length > 1 && matchFunctions(1) == false){
shell.echo("Command not found.");
}
} else {
shell.echo("Command not found.");
}
}
break;
}
if (Object.keys(argv).length == 1) {
cmdPrompt();
}
}
var cmdPrompt = function() {
// main_prompt.resume();
main_prompt.get({
properties: {
cmd: {
description: mod_context.cyan
}
}
}, function(err, result) {
processCommand(result.cmd);
});
}
setCurrentToRoot();
cmdPrompt();