This repository was archived by the owner on Aug 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.js
More file actions
183 lines (159 loc) · 6.62 KB
/
exec.js
File metadata and controls
183 lines (159 loc) · 6.62 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
var exec = require('child_process').execFile;
var remote = require('remote');
var dialog = remote.require('dialog');
var run = require('./handler.js').startTest;
var path = require("path");
var fileName = null;
var args = null;
exports.bindPluginGrid = function(state){
list = document.getElementsByClassName("plugin");
var i = list.length - 1;
list[i].addEventListener('click', function(object){
state.run.set("selected", object.currentTarget.id);
toggleOverlay();
bindRunButton(state);
}, true);
}
var bindRunButton = function(state) {
document.getElementById("run-button").addEventListener("click",
function() {
if (formValid(state)) {
var arguments = collectArgs(state);
execute(arguments, state);
toggleOverlay();
}
});
};
/**
* Reads arguments from the modal and generates a command
* to be run. Uses the template from plugins[x].command
* and replaces flags as appropriate.
* Return list of supplied arguments.
*/
var collectArgs = function(state) {
var selectedIndex = state.run.get("selected");
var containers = document.getElementsByClassName("argument-container");
var args = [];
for(var i = 0; i < containers.length; i++) {
// activation refers to a checkbox that signifies if the user has activated
// the argument (only for optional arguments)
var activation = containers[i].getElementsByClassName("activation")[0];
if ((activation && activation.checked) || !activation) {
var option = containers[i].getElementsByClassName("arg-buttons")[0]
.getElementsByTagName("input");
// to handle drop down menu
if (!option.length) {
if (containers[i].getElementsByTagName("select")) {
var option = containers[i].getElementsByTagName("select")[0];
} else {
throw "No input tags found and object is not a select dialog"
}
}
// to handle radio buttons
else if (option[0].type == "radio") {
for(var k = 0; k < option.length; k++) {
if (option[k].checked) {
var option = option[k]
break;
}
}
}
else
// for all other inputs, relevant input is always the last one
option = option[option.length - 1];
var optionName = getRNameFromElement(option);
var optionValue;
// if the arg has a flag, prepend the flag
var flagArray = state.run.get("plugins[" + selectedIndex + "].args");
var flagIndex = getIndexFromArgName(optionName, state);
var flag = flagArray[flagIndex].flag;
if (option.value == "on" && option.type == "checkbox") {
optionValue = flag? flag + " " + option.value.replace("on", "") : null;
} else
optionValue = flag ? flag + " " + option.value : option.value;
args.push([optionName, optionValue]);
}
}
return args
};
/**
* Returns name Ractive can understand from
* DOM element
*/
var getRNameFromElement = function(element) {
var result = element.name.replace("arg-", "");
result = result.replace("-activated", "");
return result;
}
var getIndexFromArgName = function(elementName, state) {
var selectedIndex = state.run.get("selected");
var flagArray = state.run.get("plugins[" + selectedIndex + "].args");
for(var z = 0; z < flagArray.length; z++) {
if (flagArray[z].name == elementName)
return z
}
}
/**
* Takes arguments and reference to state, executes
* plugin with given values
*/
var execute = function(args, state) {
var selectedIndex = state.run.get("selected");
var name = state.run.get("plugins[" + selectedIndex + "].name");
var executable = state.run.get("plugins["+ selectedIndex + "].exec");
var command = state.run.get("plugins[" + selectedIndex + "].command");
for(var x = 0; x < args.length; x++) {
// args is an array of (flag name, flag value) tuples
command = command.replace("$" + args[x][0], args[x][1]);
}
// gets rid of any variables that have not been specified
command = executable + " " + command.replace(new RegExp(/\$[a-z][a-z0-9_]*\s/g), "");
run(name, command, path.resolve("plugins") + path.sep + name, state);
}
/* Toggles an overlay display with arguments for the user
* to enter */
var toggleOverlay = function() {
el = document.getElementById("overlay");
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
window.scrollTo(0,0);
};
var formValid = function(state) {
var selectedIndex = state.run.get("selected");
var elements = document.getElementsByClassName("argument-container");
for(var i = 0; i < elements.length; i++) {
var textElement = elements[i].getElementsByTagName("input")[0];
if (textElement.type == "text") {
var textName = getRNameFromElement(textElement);
var textIndex = getIndexFromArgName(textName, state);
var regexType = state.run.get("plugins[" + selectedIndex +
"].args[" + textIndex + "].regex");
var regex = placeholderToRegex(regexType);
if (regex.test(textElement.value) == false)
return false;
}
}
return true
}
var placeholderToRegex = function(input) {
switch (input) {
case "email":
return /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
break;
case "IP":
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
break;
case "domain":
return /^(?!:\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/i
break;
case "hostname":
return /(^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)|(^(?!:\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$)/i
break;
case "URL":
return /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i
break;
default:
if (input != "")
console.warn("Invalid regex found");
return
}
}