UNPKG

@homer0/package-info

Version:

Gets the content of the project's package.json

110 lines 4.13 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var index_exports = {}; __export(index_exports, { PackageInfo: () => PackageInfo, packageInfo: () => packageInfo, packageInfoProvider: () => packageInfoProvider }); module.exports = __toCommonJS(index_exports); var fsSync = __toESM(require("fs")); var fsPromises = __toESM(require("fs/promises")); var import_path_utils = require("@homer0/path-utils"); var import_deferred = require("@homer0/deferred"); var import_jimple = require("@homer0/jimple"); const deps = (0, import_jimple.injectHelper)(); class PackageInfo { /** * A deferred promise that resolves when the package.json file is read. It will be * `undefined` if the file hasn't been read yet, or the contents are already saved in * the service. */ defer; /** * This property will store the contents of the file once it is read. */ contents; /** * The absolute path to the package.json file. */ filepath; constructor({ inject = {} } = {}) { const usePathUtils = deps.get(inject, "pathUtils", () => (0, import_path_utils.pathUtils)()); this.filepath = usePathUtils.join("package.json"); } /** * Gets the contents of the implementation's package.json file. */ async get() { if (this.contents) return this.contents; if (this.defer) return this.defer.promise; this.defer = (0, import_deferred.deferred)(); const packageJson = await fsPromises.readFile(this.filepath, "utf8"); return this.updateContents(packageJson); } /** * Synchronously gets the contents of the implementation's package.json file. */ getSync() { if (this.contents) return this.contents; const packageJson = fsSync.readFileSync(this.filepath, "utf8"); return this.updateContents(packageJson); } /** * This is a helper that takes care of updating the property with the file contents, and * if the deferred promise exists, resolve it and delete it. * * @param contents The contents of the package.json file. */ updateContents(contents) { this.contents = JSON.parse(contents); if (this.defer) { this.defer.resolve(this.contents); this.defer = void 0; } return this.contents; } } const packageInfo = (...args) => new PackageInfo(...args); const packageInfoProvider = (0, import_jimple.providerCreator)( ({ serviceName = "packageInfo", ...rest } = {}) => (container) => { container.set(serviceName, () => { const { services = {} } = rest; const inject = deps.resolve(["pathUtils"], container, services); return new PackageInfo({ inject }); }); } ); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PackageInfo, packageInfo, packageInfoProvider }); //# sourceMappingURL=index.js.map