Skip to content

Commit

Permalink
Fixes #99 - Handle ESM Stackframe Filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
beanaroo committed Mar 14, 2022
1 parent 0d2ccad commit d7fe0c1
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/LoggerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ import {
export class LoggerHelper {
public static cwdArray: string[] = process.cwd().split(pathSeparator);

// eslint-disable-next-line @rushstack/no-new-null
public static cleanUpFilePath(fileName: string | null): string | null {
return fileName == null
? fileName
: Object.entries(fileName.split(pathSeparator))
.reduce(
(cleanFileName: string, fileNamePart) =>
fileNamePart[1] !== LoggerHelper.cwdArray[fileNamePart[0]]
? (cleanFileName += pathSeparator + fileNamePart[1])
: cleanFileName,
""
)
.substring(1);
public static cleanUpFilePath(fileName: string): string {
return Object.entries(fileName.split(pathSeparator))
.reduce(
(cleanFileName: string, fileNamePart) =>
fileNamePart[1] !== LoggerHelper.cwdArray[fileNamePart[0]]
? (cleanFileName += pathSeparator + fileNamePart[1])
: cleanFileName,
""
)
.substring(1);
}

public static isError(e: Error | unknown): boolean {
Expand Down Expand Up @@ -72,11 +69,12 @@ export class LoggerHelper {
}

public static toStackFrameObject(stackFrame: NodeJS.CallSite): IStackFrame {
const filePath: string | null = stackFrame.getFileName();
let filePath: string = stackFrame.getFileName() || "";
filePath = filePath.replace("file://", "");
return {
filePath: LoggerHelper.cleanUpFilePath(filePath) ?? "",
fullFilePath: filePath ?? "",
fileName: fileBasename(stackFrame.getFileName() ?? ""),
filePath: LoggerHelper.cleanUpFilePath(filePath),
fullFilePath: filePath,
fileName: fileBasename(filePath),
lineNumber: stackFrame.getLineNumber() ?? undefined,
columnNumber: stackFrame.getColumnNumber() ?? undefined,
isConstructor: stackFrame.isConstructor() ?? undefined,
Expand Down

0 comments on commit d7fe0c1

Please sign in to comment.