1 | const { sep } = require(`path`)
|
2 |
|
3 |
|
4 | const regexpEscape = str => str.replace(/[-[/{}()*+?.\\^$|]/g, `\\$&`)
|
5 | const cleanPaths = (str, separator = sep) => {
|
6 | const stack = process.cwd().split(separator)
|
7 |
|
8 | while (stack.length > 1) {
|
9 | const currentPath = stack.join(separator)
|
10 | const currentRegex = new RegExp(regexpEscape(currentPath), `g`)
|
11 | str = str.replace(currentRegex, `$SNIP`)
|
12 |
|
13 | const currentPath2 = stack.join(separator + separator)
|
14 | const currentRegex2 = new RegExp(regexpEscape(currentPath2), `g`)
|
15 | str = str.replace(currentRegex2, `$SNIP`)
|
16 |
|
17 | stack.pop()
|
18 | }
|
19 | return str
|
20 | }
|
21 |
|
22 |
|
23 | const sanitizeError = (error, pathSeparator = sep) => {
|
24 |
|
25 | if (error.stderr) error.stderr = String(error.stderr)
|
26 | if (error.stdout)
|
27 | error.stdout = String(error.stdout)
|
28 |
|
29 |
|
30 | ;[`envPairs`, `options`, `output`].forEach(key => delete error[key])
|
31 |
|
32 |
|
33 | error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)))
|
34 |
|
35 | const errorString = JSON.stringify(error)
|
36 |
|
37 |
|
38 | return cleanPaths(errorString, pathSeparator)
|
39 | }
|
40 |
|
41 | module.exports = {
|
42 | sanitizeError,
|
43 | cleanPaths,
|
44 | }
|