@homer0/root-file
Version:
Import or require a file for the project root
72 lines • 2.49 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
RootFile: () => RootFile,
rootFile: () => rootFile,
rootFileProvider: () => rootFileProvider
});
module.exports = __toCommonJS(index_exports);
var import_path_utils = require("@homer0/path-utils");
var import_jimple = require("@homer0/jimple");
const deps = (0, import_jimple.injectHelper)();
class RootFile {
/**
* Used to resolve the path of the files.
*/
pathUtils;
constructor({ inject = {} } = {}) {
this.pathUtils = deps.get(inject, "pathUtils", () => (0, import_path_utils.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 = (0, import_jimple.providerCreator)(
({ serviceName = "rootFile", ...rest } = {}) => (container) => {
container.set(serviceName, () => {
const { services = {} } = rest;
const inject = deps.resolve(["pathUtils"], container, services);
return new RootFile({ inject });
});
}
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RootFile,
rootFile,
rootFileProvider
});
//# sourceMappingURL=index.js.map