UNPKG

1.28 kBJavaScriptView Raw
1const { sep } = require(`path`)
2
3// Removes all user paths
4const regexpEscape = str => str.replace(/[-[/{}()*+?.\\^$|]/g, `\\$&`)
5const 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// Takes an Error and returns a sanitized JSON String
23const sanitizeError = (error, pathSeparator = sep) => {
24 // Convert Buffers to Strings
25 if (error.stderr) error.stderr = String(error.stderr)
26 if (error.stdout)
27 error.stdout = String(error.stdout)
28
29 // Remove sensitive and useless keys
30 ;[`envPairs`, `options`, `output`].forEach(key => delete error[key])
31
32 // Hack because Node
33 error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)))
34
35 const errorString = JSON.stringify(error)
36
37 // Removes all user paths
38 return cleanPaths(errorString, pathSeparator)
39}
40
41module.exports = {
42 sanitizeError,
43 cleanPaths,
44}