-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseCipher.js
87 lines (76 loc) · 2.01 KB
/
parseCipher.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
const {
validationArgv,
ValidationError
} = require('./myErrors');
const {
MyWriteStream,
MyReadStream,
myTransformC,
myTransformR,
myTransformA,
} = require('./myCustomStream');
const path = require('path');
const {
pipeline
} = require('stream');
let input, output, config, arrTransform = [];
function callback() {
console.log('!Success!');
}
async function run() {
pipeline(
input,
...arrTransform,
output,
callback
);
}
try {
let map = validationArgv();
map.forEach((val, key) => {
switch (key) {
case 'o':
output = new MyWriteStream(path.resolve(process.argv[val + 1]));
break;
case 'i':
input = new MyReadStream(path.resolve(process.argv[val + 1]));
break;
case 'c':
config = process.argv[val + 1].split('-');
break;
}
});
if (!output) {
output = process.stdout;
}
if (!input) {
input = process.stdin;
process.stdin.resume();
process.stdin.on('data', (data) => {
if(data.toString().match('exit')){
process.exit(0);
}
});
}
config.forEach((el, id, config) => {
//C or R
let encode;
if (el.length == 2) {
el[1] == '1' ? encode = true : encode = false;
el[0] == 'C' ? arrTransform.push(new myTransformC(encode)) :
arrTransform.push(new myTransformR(encode));
} else {
arrTransform.push(new myTransformA());
}
});
run();
} catch (err) {
if (err instanceof ValidationError) {
process.stderr.write(err.message);
process.stderr.write(`\nThe program exited with code = ${err.code}`);
process.exit(err.code);
} else {
process.stderr.write(err.message);
process.exit(999);
}
}