redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
32 lines • 1.13 kB
JavaScript
import { dirname } from 'path';
function isStackTraces(stack) {
return Array.isArray(stack);
}
export function getCurrentDir() {
const originalPrepareStackTrace = Error.prepareStackTrace;
try {
Error.prepareStackTrace = (_, stackTraces) => stackTraces;
const err = new Error();
const stack = err.stack;
if (!isStackTraces(stack) || stack.length < 2) {
throw new Error('Invalid stack trace');
}
const callSite = stack[1];
const filename = callSite.getFileName();
if (!filename) {
throw new Error('Unable to determine filename from stack trace');
}
const cleanFilename = filename.startsWith('file://')
? filename.slice(7)
: filename;
return dirname(cleanFilename);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to get current directory: ${errorMessage}`);
}
finally {
Error.prepareStackTrace = originalPrepareStackTrace;
}
}
//# sourceMappingURL=current-dir.js.map