forked from snychka/javascript-string-manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
41 lines (33 loc) · 1000 Bytes
/
Copy pathcli.js
File metadata and controls
41 lines (33 loc) · 1000 Bytes
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
const { LineEndings, transformLineEnding } = require("./index.js");
const { help, usage } = require("./info.js");
// Display help text and exit if when someone passes `-h` or `--help`.
if (process.argv.includes("-h") || process.argv.includes("--help")) {
console.log(help);
process.exit(0);
}
// Gather input from stdin.
let buffer = "";
process.stdin.on("data", data => {
buffer += data;
});
// When stdin closes, operate on the gathered input.
process.stdin.on("end", () => {
let results;
switch (process.argv.slice(-1)[0]) {
case "CR":
results = transformLineEnding(buffer, LineEndings.CR);
process.stdout.write(results);
break;
case "LF":
results = transformLineEnding(buffer, LineEndings.LF);
process.stdout.write(results);
break;
case "CRLF":
results = transformLineEnding(buffer, LineEndings.CRLF);
process.stdout.write(results);
break;
default:
console.error(usage);
process.exit(1);
}
});