|
1
|
/** |
|
2
|
* return the file path of the caller of the function calling this function |
|
3
|
* @param position |
|
4
|
*/ |
|
5
|
export function getCallerFilePath(position = 2) { |
|
6
|
if (position >= Error.stackTraceLimit) { |
|
7
|
return null |
|
8
|
} |
|
9
|
|
|
10
|
const oldPrepareStackTrace = Error.prepareStackTrace |
|
11
|
|
|
12
|
let result!: string |
|
13
|
|
|
14
|
Error.prepareStackTrace = (_, stack) => { |
|
15
|
if (stack !== null && typeof stack === "object") { |
|
16
|
// stack[0] holds this file |
|
17
|
// stack[1] holds where this function was called |
|
18
|
// stack[2] holds the file we're interested in, in most cases, otherwise we need to get higher |
|
19
|
result = stack[position] |
|
20
|
? (stack[position] as any).getFileName() |
|
21
|
: undefined |
|
22
|
} |
|
23
|
return stack |
|
24
|
} |
|
25
|
new Error().stack |
|
26
|
Error.prepareStackTrace = oldPrepareStackTrace |
|
27
|
|
|
28
|
return result |
|
29
|
} |