@homer0/package-info
Version:
Gets the content of the project's package.json
91 lines (88 loc) • 3.19 kB
text/typescript
import * as _homer0_jimple from '@homer0/jimple';
import { IPackageJson } from 'package-json-type';
import { PathUtils } from '@homer0/path-utils';
import { DeferredPromise } from '@homer0/deferred';
/**
* The dictionary of dependencies that need to be injected in {@link PackageInfo}.
*/
type PackageInfoInjectOptions = {
/**
* The service that creates the path relative to the project root.
*/
pathUtils: PathUtils;
};
/**
* The options for the service constructor.
*/
type PackageInfoOptions = {
/**
* A dictionary with the dependency injections for the service. If one or more are not
* provided, the service will create new instances.
*/
inject?: Partial<PackageInfoInjectOptions>;
};
/**
* A small service that reads the contents of the implementation's package.json file.
*/
declare 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.
*/
protected defer?: DeferredPromise<IPackageJson>;
/**
* This property will store the contents of the file once it is read.
*/
protected contents?: IPackageJson;
/**
* The absolute path to the package.json file.
*/
protected filepath: string;
constructor({ inject }?: PackageInfoOptions);
/**
* Gets the contents of the implementation's package.json file.
*/
get(): Promise<Readonly<IPackageJson>>;
/**
* Synchronously gets the contents of the implementation's package.json file.
*/
getSync(): Readonly<IPackageJson>;
/**
* 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.
*/
protected updateContents(contents: string): IPackageJson;
}
/**
* Shorthand for `new PackageInfo()`.
*
* @param args The same parameters as the {@link PackageInfo} constructor.
* @returns A new instance of {@link PackageInfo}.
*/
declare const packageInfo: (...args: ConstructorParameters<typeof PackageInfo>) => PackageInfo;
/**
* The options for the {@link PackageInfo} Jimple's provider creator.
*/
type PackageInfoProviderOptions = {
/**
* The name that will be used to register the service.
*
* @default 'packageInfo'
*/
serviceName?: string;
/**
* A dictionary with the name of the services to inject. If one or more are not
* provided, the service will create new instances.
*/
services?: {
[key in keyof PackageInfoInjectOptions]?: string;
};
};
/**
* A provider creator to register {@link PackageInfo} in a Jimple container.
*/
declare const packageInfoProvider: _homer0_jimple.ResourceCreator<"provider", "register", ({ serviceName, ...rest }?: PackageInfoProviderOptions) => (container: _homer0_jimple.Jimple) => void, _homer0_jimple.ProviderRegisterFn<_homer0_jimple.Jimple>>;
export { PackageInfo, type PackageInfoOptions, type PackageInfoProviderOptions, packageInfo, packageInfoProvider };