-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (66 loc) · 2.7 KB
/
app.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
const wordsCount = require('./lib/wordsCount');
const maxCount = require('./lib/maxCount');
const reader = require('./lib/reader');
const writer = require('./lib/writer');
const commandsHelper = require('./helper/commandsHelper');
const { availableMemory } = require('./helper/systemResources');
const readline = require('readline');
const streamer = require('./lib/streamer');
const { program } = require('commander');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
program
.version('1.0.0')
.description('Count the occurrences of each word in a file')
.option('-f, --file <filename>', 'The input file')
.option('-o, --write <output file>', 'The output file name')
.option('-s, --stream [type]', 'read the input file as a stream')
.on('--help', commandsHelper)
.parse(process.argv);
const options = program.opts();
if (!options.file) {
commandsHelper();
console.error('Please specify input filename\n');
process.exit(1);
}
const limit = 100;
const { file: inputFile, write: outputFile, stream } = options;
const main = async () => {
try {
console.time('File Reader:');
const { fileContent, fileSizeInBytes } = await reader(inputFile);
console.timeEnd('File Reader:');
console.time('Word Counter:');
listOfWords = stream | (fileSizeInBytes > availableMemory / 2) ? await streamer(inputFile) : await wordsCount(fileContent);
console.timeEnd('Word Counter:');
console.time('wordsWithMaxCount:');
const { wordsWithMaxCount, highestCount } = maxCount(listOfWords);
console.timeEnd('wordsWithMaxCount:');
const finalList = { ...listOfWords, highestWords: { wordsWithMaxCount, highestCount } };
const outputSize = Object.keys(listOfWords).length;
if (outputSize < limit) {
console.log(listOfWords);
}
rl.question(`There are more than ${limit} words. Do you want to write the output to a file instead? (Y/N)\n`, async (response) => {
if (response.toLowerCase() === 'y') {
await writer(finalList, outputFile);
console.log(`The file ${outputFile}.json has been saved successfully\n`);
} else {
console.log(finalList);
}
rl.question(`Do you want to print the words with the highest count ? (Y/N)\n`, async (response) => {
rl.close();
if (response.toLowerCase() === 'y') {
console.log({ wordsWithMaxCount, highestCount });
}
});
});
} catch (error) {
console.error(`Error: ${error}`);
process.exit(1);
}
};
main();
module.exports = main;