Skip to content

Commit

Permalink
fix: Improve get caller file path util (#7974)
Browse files Browse the repository at this point in the history
* fix: Improve get caller file path util

* improve compatibility

* update comment

---------

Co-authored-by: Riqwan Thamir <[email protected]>
  • Loading branch information
adrien2p and riqwan authored Jul 5, 2024
1 parent de0a9c1 commit 1393f51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
32 changes: 17 additions & 15 deletions packages/core/utils/src/common/get-caller-file-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
*/
export function getCallerFilePath(position = 2) {
if (position >= Error.stackTraceLimit) {
throw new TypeError(
"getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `" +
position +
"` and Error.stackTraceLimit was: `" +
Error.stackTraceLimit +
"`"
)
return null
}

const oldPrepareStackTrace = Error.prepareStackTrace
Error.prepareStackTrace = (_, stack) => stack
const stack = new Error().stack
Error.prepareStackTrace = oldPrepareStackTrace

if (stack !== null && typeof stack === "object") {
// stack[0] holds this file
// stack[1] holds where this function was called
// stack[2] holds the file we're interested in
return stack[position] ? (stack[position] as any).getFileName() : undefined
let result!: string

Error.prepareStackTrace = (_, stack) => {
if (stack !== null && typeof stack === "object") {
// stack[0] holds this file
// stack[1] holds where this function was called
// stack[2] holds the file we're interested in, in most cases, otherwise we need to get higher
result = stack[position]
? (stack[position] as any).getFileName()
: undefined
}
return stack
}
new Error().stack
Error.prepareStackTrace = oldPrepareStackTrace

return result
}
3 changes: 1 addition & 2 deletions packages/core/utils/src/modules-sdk/joiner-config-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export function defineJoinerConfig(
loadedModels = []

let index = 1
const maxSearchIndex = 6

while (true) {
++index
Expand Down Expand Up @@ -101,7 +100,7 @@ export function defineJoinerConfig(

loadedModels = loadModels(basePath)

if (index === maxSearchIndex || loadedModels.length) {
if (loadedModels.length) {
break
}
}
Expand Down

0 comments on commit 1393f51

Please sign in to comment.