-
Notifications
You must be signed in to change notification settings - Fork 0
/
myErrors.js
136 lines (121 loc) · 3.94 KB
/
myErrors.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const path = require('path');
const fs = require('fs');
//Не обессудьте вас ждёт говнокод)
class ValidationError extends Error {
constructor(message) {
Error.stackTraceLimit = 0;
super(message);
this.name = this.constructor.name;
this.code = 1;
}
}
class ArgRequiredError extends ValidationError {
constructor(Arg) {
super(`Required argument missing: ${Arg}`);
}
}
class RepeatingOptionsError extends ValidationError {
constructor(Arg) {
super(`The option ${Arg} is duplicated`);
}
}
class ExtraArgumentsError extends ValidationError {
constructor(Args) {
if (Args.length == 0) {
super(`Some options have not values`);
} else {
super(`Invalid arguments found: ${Args.splice(0,3)}`);
}
}
}
class PayloadError extends ValidationError {
constructor(message, payload) {
super(`${message} ${payload}`);
this.payload = payload;
}
}
class InvalidPathError extends PayloadError {}
class NotExistFileError extends PayloadError {}
class InvalidConfigError extends PayloadError {}
function checkPath(path) {
if (fs.existsSync(path)) {
fs.stat(path, (err, stats) => {
if (!stats.isFile()) {
throw new NotExistFileError("This is not a file", path);
}
});
} else {
throw new InvalidPathError("Nothing exists along the specified path:", path);
}
}
// Search and validation option headers
function validationArgv() {
let map = new Map();
let argCount = 0;
const cipherSequenceValidation = /(C1|C0|R1|R0|A)((-C1|-C0|-R1|-R0|-A))*\s/gm;
//Поиск всех опций и вынесение их в мап
process.argv.forEach((item, id) => {
if (item === "-c" || item === "--config") {
if (map.has("c")) {
throw new RepeatingOptionsError('CONFIG');
} else {
map.set("c", id);
argCount += 1;
}
}
if (item === "-i" || item === "--input") {
if (map.has("i")) {
throw new RepeatingOptionsError("INPUT");
} else {
map.set("i", id);
argCount += 1;
}
}
if (item === "-o" || item === "--output") {
if (map.has("o")) {
throw new RepeatingOptionsError("OUTPUT");
} else {
map.set("o", id);
argCount += 1;
}
}
});
if (!map.has('c')) {
throw new ArgRequiredError('CONFIG');
}
//Костыль на отсев лишних аргументов
if (argCount * 2 != process.argv.length - 2) {
let tempArr = [],
extraArg = [];
for (let el of map.values()) {
tempArr.push(el, el + 1);
}
process.argv.forEach((item, id) => {
if (id == 0 || id == 1) {
return;
}
if (!tempArr.includes(id)) {
extraArg.push(item);
}
});
throw new ExtraArgumentsError(extraArg);
}
//Проверка значений опций
map.forEach((value, key) => {
let arg = process.argv[map.get(key) + 1];
if (key == "o" || key == "i") {
checkPath(path.resolve(arg));
} else {
//Регулярочка
if (`${arg} `.match(cipherSequenceValidation) == null) {
throw new InvalidConfigError("Config set incorrectly:", arg);
}
}
});
//Если все проверки пройдены возвращаем map с флагами и их значениями(индексами массива process)
return map;
}
module.exports = {
validationArgv,
ValidationError
};