-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_ciphering_cli.js
59 lines (51 loc) · 1.97 KB
/
my_ciphering_cli.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
"use strict";
require("./colorText");
const fs = require("fs");
const { pipeline } = require("stream");
const { promisify } = require("util");
const process = require("process");
const parseConfig = require("./parseConfig");
const getValue = require("./parseArgsCLI");
const addMessageStream = require("./addMessageStream");
const pipelineAsync = promisify(pipeline);
const config = getValue("-c") || getValue("--config");
const input = getValue("-i") || getValue("--input");
const output = getValue("-o") || getValue("--output");
const inStreams = input ? [fs.createReadStream(input, "utf8")] : [process.stdin];
const transStreams = parseConfig(config);
const outStreams = output ? [fs.createWriteStream(output)] : [process.stdout];
process.on("SIGINT", () => {
process.stdout.write("\n\nВыход...".color("red"));
process.exit(0);
});
(async function run() {
try {
if (!input) {
//input: console,
process.stdout.write("Для выхода нажмите 'Ctrl + C'\n".color("cyan"));
process.stdout.write("\nВведите строку: ".color("blue"));
if (output) {
//output: file
process.stdin.on("data", () => {
process.stdout.write(
`Результат записан в файл ${output}\n`.color("green") +
"Введите следующую строку: ".color("blue")
);
});
} else {
//output: console
outStreams.unshift(
new addMessageStream(" Результат: ".color("blue"), "\nВведите строку: ".color("blue"))
);
}
}
if (input && !output) {
//input: file, output: console
process.stdout.write("Результат: ".color("blue"));
}
await pipelineAsync(...inStreams, ...transStreams, ...outStreams);
process.stdout.write("Преобразование выполнено успешно".color("green"));
} catch (err) {
console.error("pipeline failed with error:", err);
}
})();