Skip to content

Commit 6cba273

Browse files
curusarnadikus
andauthored
Do not use require (#45)
* Do not use require Avoid warnings with webpack and when importing in browser. Fixes: #20 * Fix tests * πŸ’„ Co-authored-by: Andrej Hoos <andrej@betterstack.com> * πŸ’„ (2) --------- Co-authored-by: Andrej Hoos <andrej@betterstack.com>
1 parent 68bf86d commit 6cba273

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

β€Žpackages/bunyan/src/bunyan.test.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe("Bunyan tests", () => {
132132
logtail.setSync(async logs => {
133133
const context = logs[0].context as Context;
134134
const runtime = context.runtime as Context;
135-
expect(runtime.file).toEqual("bunyan.test.ts")
135+
expect(runtime.file).toMatch("bunyan.test.ts")
136136
done();
137137
return logs;
138138
});

β€Žpackages/node/src/context.tsβ€Ž

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { dirname, relative } from "path";
33
import stackTrace, { StackFrame } from 'stack-trace';
44
import { Node } from "./node";
55

6+
const mainFile = mainFileName();
67
/**
78
* Determines the file name and the line number from which the log
89
* was initiated (if we're able to tell).
@@ -25,7 +26,7 @@ export function getStackContext(logtail: Node, stackContextHint?: StackContextHi
2526
},
2627
system: {
2728
pid: process.pid,
28-
main_file: mainFileName()
29+
main_file: mainFile,
2930
}
3031
}
3132
};
@@ -66,5 +67,16 @@ function relativeToMainModule(fileName: string): string | null {
6667
}
6768

6869
function mainFileName(): string {
69-
return require?.main?.filename ?? '';
70+
let argv = process.argv;
71+
// return first js file argument - arg ending in .js
72+
for (const i in argv) {
73+
if (argv[i].startsWith('-')) {
74+
// break on first option
75+
break;
76+
}
77+
if (argv[i].endsWith('.js')) {
78+
return argv[i];
79+
}
80+
}
81+
return '';
7082
}

β€Žpackages/winston/src/winston.test.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,6 @@ describe("Winston logging tests", () => {
213213

214214
const context = logs[0].context as Context;
215215
const runtime = context.runtime as Context;
216-
expect(runtime.file).toEqual("winston.test.ts")
216+
expect(runtime.file).toMatch("winston.test.ts");
217217
});
218218
});

0 commit comments

Comments
Β (0)