@homer0/root-file
Version:
Import or require a file for the project root
78 lines (75 loc) • 2.61 kB
text/typescript
import * as _homer0_jimple from '@homer0/jimple';
import { PathUtils } from '@homer0/path-utils';
/**
* The dictionary of dependencies that need to be injected in {@link RootFile}.
*/
type RootFileInjectOptions = {
/**
* The service that creates the path relative to the project root.
*/
pathUtils: PathUtils;
};
/**
* The options for the service constructor.
*/
type RootFileOptions = {
/**
* A dictionary with the dependency injections for the service. If one or more are not
* provided, the service will create new instances.
*/
inject?: Partial<RootFileInjectOptions>;
};
/**
* A small service that reads the contents of the implementation's package.json file.
*/
declare class RootFile {
/**
* Used to resolve the path of the files.
*/
pathUtils: PathUtils;
constructor({ inject }?: RootFileOptions);
/**
* 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<FileType = unknown>(filepath: string): FileType;
/**
* 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<FileType = unknown>(filepath: string): Promise<FileType>;
}
/**
* Shorthand for `new RootFile()`.
*
* @param args The same parameters as the {@link RootFile} constructor.
* @returns A new instance of {@link RootFile}.
*/
declare const rootFile: (...args: ConstructorParameters<typeof RootFile>) => RootFile;
/**
* The options for the {@link RootFile} Jimple's provider creator.
*/
type RootFileProviderOptions = {
/**
* The name that will be used to register the service.
*
* @default 'rootFile'
*/
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 RootFileInjectOptions]?: string;
};
};
/**
* A provider creator to register {@link RootFile} in a Jimple container.
*/
declare const rootFileProvider: _homer0_jimple.ResourceCreator<"provider", "register", ({ serviceName, ...rest }?: RootFileProviderOptions) => (container: _homer0_jimple.Jimple) => void, _homer0_jimple.ProviderRegisterFn<_homer0_jimple.Jimple>>;
export { RootFile, type RootFileOptions, type RootFileProviderOptions, rootFile, rootFileProvider };