-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (73 loc) · 1.47 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
const lineByLine = require("line-by-line");
runLineByLineDemo();
// Main function.
function runLineByLineDemo()
{
var targetFilePath = readTargetPath();
var streamOpts = {encoding: "utf8", skipEmptyLines: true};
var demoResult = {linesDisplayed: 0, successful: true, message: ""};
var lineStream = new lineByLine(targetFilePath, streamOpts);
// Line stream error.
lineStream.on('error', function (readErr)
{
demoResult.successful = false;
demoResult.message = readErr.message;
lineStream.close();
});
// Display current line.
lineStream.on('line', function (currentLine)
{
console.log(currentLine);
demoResult.linesDisplayed += 1;
});
// End reached.
lineStream.on('end', function()
{
handleDivider(demoResult.linesDisplayed);
handleEndResult(demoResult);
});
}
// Displays output divider.
function handleDivider(dispCount)
{
if (dispCount > 0)
{
console.log("");
console.log("---");
console.log("");
}
}
// Displays result message.
function handleEndResult(resObj)
{
if (resObj.successful === true)
{
console.log("Complete");
}
else
{
console.log(resObj.message);
}
}
// Reads file path argument.
function readTargetPath()
{
var retArg = "";
var pathRes = "";
if (process.argv.length > 2)
{
// Argument exists.
retArg = process.argv[2];
}
if (typeof retArg === "string" && retArg.length > 0)
{
// Use entered path.
pathRes = retArg;
}
else
{
// Default.
pathRes = "./example.txt";
}
return pathRes;
}