I'm writing a class decorator for my controllers. It looks like:
export function Controller<T extends { new(...args: any[]): {} }> (ctor: T) {
return class extends ctor {
public readonly name = name;
}
}
ctor
is a constructor of a class decorated with @Controller
.
Full path to the controller's file is src/modules/{module}/controllers/{ctrl}Controller.ts
. I need to get parts in curly braces and concatenate them into {module}.{ctrl}
.
To do so I need a filepath of module from which ctor
is imported. How can I obtain it?
There is no way to get file path information from
ctor
parameter. It's just a function that was defined somewhere.Basically,
module
andctrl
preferably have to be provided to controller class on registration, since the path is known at this moment, i.e.:The only and hacky workarount is to get file path of a place where
Controller
was called. This is achieved by getting stacktrace, e.g:The problem is that it's the path where
Controller
is called:.../foo.ts
.../bar.ts
A less hacky and more reliable way is to provide file path explicitly from the module where it is available:
There is
import.meta
proposal which is supported by TypeScript. It depends on Node project configuration because it works withesnext
target:import.meta
that was passed to@Controller
can be consumed asmeta.__dirname
.