-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathnormalizeErrors.js
52 lines (42 loc) · 1.22 KB
/
normalizeErrors.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
import stripAnsi from "strip-ansi";
export function removeCWD(str) {
const isWin = process.platform === "win32";
let cwd = process.cwd();
if (isWin) {
if (str.split("\n").length > 3) {
// @import '\
// \
// \
// ';
return stripAnsi(str)
.replace(/\(from .*?\)/, "(from `replaced original path`)")
.replace(new RegExp(cwd, "g"), "");
}
// eslint-disable-next-line no-param-reassign
str = str.replace(/\\/g, "/");
// eslint-disable-next-line no-param-reassign
cwd = cwd.replace(/\\/g, "/");
}
return stripAnsi(str)
.replace(/\(from .*?\)/, "(from `replaced original path`)")
.replace(new RegExp(cwd, "g"), "");
}
export default (errors, shortError, type) =>
errors.map((error) => {
let errorMessage = error.toString();
if (shortError) {
errorMessage = errorMessage.split("\n").slice(0, 2).join("\n");
}
if (type === "postcss") {
errorMessage = errorMessage
.split("\n")
.map((str) => {
if (/^\(/i.test(str)) {
return removeCWD(str);
}
return str;
})
.join("\n");
}
return removeCWD(errorMessage.split("\n").slice(0, 12).join("\n"));
});