-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
158 lines (131 loc) · 4.6 KB
/
logger.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
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
const request = require('request');
const { exec } = require('child_process');
const { url } = require('./url');
const title = process.argv[2];
let commands = process.argv[3];
if(commands)
commands = commands.split(';');
else
commands = [];
let index = 0;
console.log();
if(title)
console.log("Slack Logger is now on with the title " + title);
else
console.log("Slack Logger is now on");
console.log("It will send all logs from this terminal to the logs slack channel in the Bhagat workspace");
console.log("Use the on command to test if the logger is on");
console.log("Click Ctrl C to stop the logger or type in exit");
console.log();
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : JSON.stringify({ attachments: [{ title, text: new Date().toString().split(' ').splice(0, 5).join(' ') + ": Starting Logger . . ." }]}),
},
(error, res, body) => null
);
let curDir = __dirname;
process.stdout.write(curDir + ">" + ((index < commands.length)? commands[index]: ""));
process.openStdin().addListener("data", data => {
data = data.toString().trim();
if(index < commands.length) {
data = commands[index] + data;
index++;
}
if(data === 'exit')
process.exit();
if(data === 'on') {
console.log();
console.log("Yes the logger is still on");
console.log();
process.stdout.write(curDir + ">");
return;
}
if(data.substring(0, 2) === 'cd') {
let dirCommand = data.substring(2);
if(dirCommand.substring(0, 1) == ' ')
dirCommand = dirCommand.substring(1);
const oldDir = curDir;
switch(dirCommand) {
case '.':
break;
case '':
if(process.platform === "win32")
exec('cd', {
cwd: curDir
}, (_, stdout, stderr) => {
console.log();
console.log('stdout:');
console.log(stdout);
console.log('stderr:');
console.log(stderr);
});
break;
case '..':
const splitChar = process.platform === "win32"? '\\' : '/';
curDir = curDir.split(splitChar).splice(0, curDir.split(splitChar).length - 1).join(splitChar);
break;
default:
if(dirCommand.substring(0, 2) === 'C:')
curDir = dirCommand;
else
curDir += process.platform === "win32"? "\\" : "/" + dirCommand;
}
exec('ls', {
cwd: curDir
}, (_, stdout, stderr) => {
if(stdout === '') {
console.log('The path specified does not exist or is empty.')
curDir = oldDir;
}
console.log();
process.stdout.write(curDir + ">" + ((index < commands.length)? commands[index]: ""));
})
return;
}
console.log();
console.log('stdout:');
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : JSON.stringify({ attachments: [{ title, text: new Date().toString().split(' ').splice(0, 5).join(' ') + ": Running " + curDir + ">" + data }]}),
},
(error, res, body) => null
);
exec(data, {
cwd: curDir
}, (_, stdout, stderr) => {
console.log('stderr:');
console.log(stderr);
if(stderr !== '')
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : JSON.stringify({ attachments: [{ title, text: new Date().toString().split(' ').splice(0, 5).join(' ') + ": " + stderr }]}),
},
(error, res, body) => null
);
process.stdout.write(curDir + ">" + ((index < commands.length)? commands[index]: ""));
}).stdout.on("data", data => {
console.log(data.toString().trim());
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : JSON.stringify({ attachments: [{ title, text: new Date().toString().split(' ').splice(0, 5).join(' ') + ": " + data.toString().trim() }]}),
},
(error, res, body) => null
);
});;
});
process.on('exit', () => {
console.log();
console.log("Stopping Slack Logger . . .");
});
process.on('SIGINT', () => {
console.log();
process.exit();
});