-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplex.js
executable file
·210 lines (178 loc) · 4.97 KB
/
splex.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
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
#!/usr/bin/env node
const chalk = require('chalk');
const meow = require('meow');
const Tail = require('tail').Tail;
const fs = require('fs');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
// Checks for available update and returns an instance
const notifier = updateNotifier({pkg});
// Notify using the built-in convenience method
notifier.notify();
// CLI Stuff
const cli = meow(
`
==================
------------------
S P L E X
------------------
==================
Usage:
------------------
$ splex [options] file_1 file_2 file_X
Options:
--table -t print as table rows
--colors -c specify custom colors as: -c color1,color2
--monochrome -m monochrome mode
Config file:
------------------
you can have per firectory config file with named .splexrc.json
with following content
{
"files": [
"logs/log-0.log",
"logs/log-1.log",
"logs/log-2.log",
"logs/log-3.log"
]
}
if this file exist you can just run splex command,
wihout list of files provided
`,
{
flags: {
table: {type: 'boolean', alias: 't'},
colors: {type: 'string', alias: 'c'},
monochrome: {type: 'boolean', alias: 'm'}
}
}
);
// ------------------------------------
let testRcFile = function () {
const path = process.cwd() + '/.splexrc.json';
if (fs.existsSync(path)) {
return true;
}
return false;
};
let readRcFile = function () {
const path = process.cwd() + '/.splexrc.json';
const raw = fs.readFileSync(path);
return JSON.parse(raw);
};
let filenames = cli.input;
// Sanity checks
if (cli.input.length === 0) {
if (testRcFile() === true) {
console.log(chalk.blueBright('INFO: File names not provided, reading from .splexrc.json file'));
let rcFIle = readRcFile();
filenames = rcFIle.files;
} else {
console.log(chalk.red('Error:'), 'No files specified.');
console.log(
chalk.yellow('Usage example:'),
'splex [options] file1 file2 file3...'
);
cli.showHelp(2);
}
}
// Options handling
const optionsMap = {
t: 1,
c: 2,
m: 4
};
let optionsSum = 0;
['t', 'c', 'm'].forEach(flag => {
if (cli.flags[flag] === true || (typeof cli.flags[flag] === 'string' && cli.flags[flag] !== '')) {
optionsSum += optionsMap[flag];
}
});
let appOptions = {
term: {
size: process.stdout.columns,
line: '-'.repeat(process.stdout.columns)
},
colors: ['red', 'green', 'blue', 'yellow', 'magenta', 'cyan'],
colorIdx: {}
};
let listeners = {};
// Provide custom colors
if (cli.flags.c) {
appOptions.colors = cli.flags.c.split(',');
}
// Create index of fileName -> color
filenames.forEach((f, idx) => {
let cIdx = idx % appOptions.colors.length;
appOptions.colorIdx[f] = appOptions.colors[cIdx];
});
// -------- START SPLEX -----------
console.log('-------------------');
console.log(' Starting SpleX ');
console.log('----- 🦈 🦈 ------');
// Start tail listeners for each file provided
filenames.forEach(f => {
listeners[f] = new Tail(f);
listeners[f].on('line', l => {
let color = appOptions.colorIdx[f];
switch (optionsSum) {
case 1: // Tables, same as 3
case 3:
// Custom colors + table
colorPrintTable(color, f, l);
break;
case 0: // No options provided
case 2: // Custom colors provided, print default
splexPrint(colorPrint(color, f, l));
break;
case 4: // Mono - no tables, same as 6
case 6:
// Mono + custom colors, invalid combination,
// just print mono
splexPrint(monoPrint(f, l));
break;
case 5: // Mono - with tables, same as 7
case 7:
// Mono + table + custom colors
// invalid combination, print mono table
monoPrintTable(f, l);
break;
default:
colorPrint(color, f, l);
break;
}
});
listeners[f].on('error', err => console.log('Error: ', err));
console.log(chalk[appOptions.colorIdx[f]]('Setting up listener for: ') + f);
});
// Color print line, with table flag for tagle format
let colorPrint = function (color, file, line) {
return (chalk[color](`> ${file}: `) + chalk.white(`${line}`));
};
let colorPrintTable = function (color, file, line) {
console.log(chalk[color](`> ${file}: `) + chalk.green('| ') + chalk.white(`${line}`));
console.log(chalk.green(appOptions.term.line));
};
// Mono print line with flag for table format
let monoPrint = function (file, line) {
return (`> ${file}: ${line}`);
};
// Mono print line with flag for table format
let monoPrintTable = function (file, line) {
console.log(`> ${file}: | ${line}`);
console.log(appOptions.term.line);
};
let splexPrint = function (line) {
console.log(line);
};
// Stuff that need to be re-calculated
// on term re-size
// ------------------------------------
let handleChange = function () {
appOptions.term.size = process.stdout.columns;
appOptions.term.line = '-'.repeat(process.stdout.columns);
};
// Wait in loop, until someone presses ctrl-c
setInterval(() => {
handleChange();
}, 1000);