-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhedit.js
More file actions
145 lines (132 loc) · 3.75 KB
/
hedit.js
File metadata and controls
145 lines (132 loc) · 3.75 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
const console = require('terminal-kit').terminal; // console = terminal-kit
const fs = require('fs');
var _PLATFORM = process.platform; // Global Platform Var
var _HOST_LOC; // Global host file location
// Function to clear terminal screen
function c_clear(){
console.clear();
}
// Set a "title"
function c_i_title(title){
console.magenta.underline.bold(title);
}
// Exit the application
function exit_c_a(code){
process.exit(code);
}
// Ensure a hosts file exists somewhere
function check_hosts_file(){
console('Checking Hosts File...\n\n');
switch(_PLATFORM){
case 'win32':
_HOST_LOC = 'C:\\Windows\\System32\\Drivers\\etc\\hosts';
break;
case 'linux':
_HOST_LOC = '/etc/hosts'
}
if(fs.existsSync(_HOST_LOC)){
console.green(_HOST_LOC+'\n');
return true;
}else{
console.red('Hosts file not found. Is your OS supported?\n');
return false;
}
}
// Parse the hosts file and only return entries that matter
function parse_hosts(with_core){
var rawHosts = fs.readFileSync(_HOST_LOC,'utf8');
var lines = rawHosts.split(/\r?\n/);
menuItems = [];
lines.forEach((line)=>{
t_line = line.trim();
if(t_line[0] != '#' && t_line){
menuItems.push(line);
}
});
if(with_core){
menuItems.push('New Entry');
menuItems.push('Exit HEDIT');
}
return menuItems;
}
// Render the main menu
function render_main_menu(menu_items){
c_clear();
c_i_title('HEDIT - Hosts Editor\n');
console.red('!!HEDIT WILL REMOVE ALL HOST FILE COMMENTS AND BLANK SPACES!!\n')
console.singleColumnMenu(menu_items, [] , (e,res)=>{
isCore = false;
if(res.selectedIndex == (menu_items.length - 1 )){
exit_c_a(1);
}
if(res.selectedText === "New Entry"){
isCore = true;
new_entry();
}
// Not a default menu item, editing existing host
if(!isCore){
select_entry(res.selectedText);
}
});
}
// Render the menu for a selected item
function select_entry(to_edit){
c_clear();
c_i_title('HEDIT - Edit '+to_edit+'\n\n');
console.singleColumnMenu(['Delete','Back'] , [] , (e, res)=>{
if(res.selectedIndex === 1){
render_main_menu(parse_hosts(true));
}
if(res.selectedIndex === 0){
var hosts = parse_hosts();
var entry_index;
hosts.forEach((host, index)=>{
if(host === to_edit){
console.red('Removing ' + to_edit+'\n\n');
entry_index = index;
}
});
hosts.splice(entry_index,1);
write_array(hosts)
render_main_menu(parse_hosts(true));
}
})
}
// Write an array to the hosts file
function write_array(hosts){
hosts = hosts.join('\r\n');
fs.writeFileSync(_HOST_LOC, hosts);
}
// Write a string to the hosts file
function append_string(host){
fs.appendFileSync(_HOST_LOC,'\r\n'+host+'\r\n');
}
// Create a new entry
function new_entry(){
c_clear();
c_i_title('HEDIT - New Host\n\n');
var addr;
var fqdn;
console('Enter Address: ');
console.inputField({default:'127.0.0.1'}, (error, input)=>{
addr = input;
console('\nEnter FQDN: ');
console.inputField({}, (error, input)=>{
fqdn = input;
append_string(addr+' '+fqdn);
render_main_menu(parse_hosts(true));
});
});
}
// Output the contents of an array
function loop_out_array(arr){
arr.forEach((item) => {
console(item+'\n');
})
}
// If a hosts file exists, render the menu
if(check_hosts_file()){
render_main_menu(parse_hosts(true));
}else{
exit_c_a(1);
}