@homer0/root-file
Version:
Import or require a file for the project root
46 lines • 1.36 kB
JavaScript
import { pathUtils } from "@homer0/path-utils";
import { providerCreator, injectHelper } from "@homer0/jimple";
const deps = injectHelper();
class RootFile {
/**
* Used to resolve the path of the files.
*/
pathUtils;
constructor({ inject = {} } = {}) {
this.pathUtils = deps.get(inject, "pathUtils", () => pathUtils());
}
/**
* Require a file with a path relative to the project root.
*
* @param filepath The path to the file, relative to the project root.
* @template FileType The type of the required file.
*/
require(filepath) {
return require(this.pathUtils.join(filepath));
}
/**
* Import a file with a path relative to the project root.
*
* @param filepath The path to the file, relative to the project root.
* @template FileType The type of the required file.
*/
import(filepath) {
return import(this.pathUtils.join(filepath));
}
}
const rootFile = (...args) => new RootFile(...args);
const rootFileProvider = providerCreator(
({ serviceName = "rootFile", ...rest } = {}) => (container) => {
container.set(serviceName, () => {
const { services = {} } = rest;
const inject = deps.resolve(["pathUtils"], container, services);
return new RootFile({ inject });
});
}
);
export {
RootFile,
rootFile,
rootFileProvider
};
//# sourceMappingURL=index.js.map