-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (100 loc) · 3.75 KB
/
index.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
const fs = require('node:fs');
const zlib = require('node:zlib');
const readline = require('node:readline');
const glob = require('glob').glob;
const prompts = require('prompts');
let equalOrContain, searchString;
async function processGzipFile(filePath) {
const gunzip = zlib.createGunzip();
const rl = readline.createInterface({
input: gunzip,
crlfDelay: Infinity
});
const outputLines = [];
let fileName = filePath.split('/').pop() && filePath.split('\\').pop();
rl.on('line', (line) => {
if (equalOrContain === 'contain') {
if (line.includes(searchString)) outputLines.push(fileName + ": " + line);
} else {
if (line === searchString) outputLines.push(fileName + ": " + line);
}
});
const fileStream = fs.createReadStream(filePath);
fileStream.pipe(gunzip);
await new Promise((resolve, reject) => {
rl.on('close', resolve);
rl.on('error', reject);
});
return outputLines;
}
async function processFiles(searchFolder) {
console.log(`Searching for files in ${searchFolder}`);
const gzipFiles = await glob(`${searchFolder}/**/*.gz`)
const latestLogFile = `${searchFolder}/latest.log`;
console.log(`Searching through ${gzipFiles.length} files...`);
const allMatchingLines = [];
for (const file of gzipFiles) {
const matchingLines = await processGzipFile(file, searchString);
allMatchingLines.push(...matchingLines);
}
if (fs.existsSync(latestLogFile)) {
const latestLogLines = fs.readFileSync(latestLogFile, 'utf-8').split('\n');
allMatchingLines.push(...latestLogLines.filter(line => equalOrContain === 'contain' ? line.includes(searchString) : line === searchString).map(line => 'latest.log: ' + line));
}
return allMatchingLines;
}
(async () => {
const response = await prompts(
[
{
type: 'text',
name: 'searchFolder',
message: 'What folder do you want to search in?',
validate: path => fs.existsSync(path) ? true : 'Path does not exist'
},
{
type: 'text',
name: 'searchString',
message: 'What string do you want to search for?'
},
{
type: 'select',
name: 'equalOrContain',
message: 'Should the search string be an exact match or contain the search string?',
choices: [
{ title: 'Contain', value: 'contain' },
{ title: 'Exact match', value: 'exact' }
]
},
{
type: 'text',
name: 'outputFile',
message: 'What should the output file be called?',
initial: 'output.txt'
},
{
type: 'text',
name: 'outputPath',
message: 'Where should the output file be saved?',
initial: process.cwd(),
validate: path => fs.existsSync(path) ? true : 'Path does not exist'
}
],
{
onCancel: () => {
console.log('Process cancelled.');
process.exit(0);
}
}
);
try {
searchString = response.searchString;
equalOrContain = response.equalOrContain;
const matchingLines = await processFiles(response.searchFolder);
console.log(`Found ${matchingLines.length} matching lines!`);
fs.writeFileSync(response.outputPath + '/' + response.outputFile, matchingLines.join('\n'));
console.log(`Matching lines written to ${response.outputFile}.`);
} catch (error) {
console.error('Error occurred:', error);
}
})();