confinode
Version:
Node application configuration reader
177 lines (176 loc) • 7.54 kB
TypeScript
import { ConfigDescriptionParameter } from '../ConfigDescription';
import ConfinodeResult from '../ConfinodeResult';
import ConfinodeOptions from './ConfinodeOptions';
declare type SearchFunctionType<T extends object, M extends 'async' | 'sync'> = M extends 'async' ? {
(searchStart?: string): Promise<ConfinodeResult<T> | undefined>;
sync: (searchStart?: string) => ConfinodeResult<T> | undefined;
} : {
(searchStart?: string): ConfinodeResult<T> | undefined;
async: (searchStart?: string) => Promise<ConfinodeResult<T> | undefined>;
};
declare type LoadFunctionType<T extends object, M extends 'async' | 'sync'> = M extends 'async' ? {
(name: string): Promise<ConfinodeResult<T> | undefined>;
sync: (name: string) => ConfinodeResult<T> | undefined;
} : {
(name: string): ConfinodeResult<T> | undefined;
async: (name: string) => Promise<ConfinodeResult<T> | undefined>;
};
/**
* The main Confinode class.
*/
export default class Confinode<T extends object = any, M extends 'async' | 'sync' = 'async'> {
readonly name: string;
private readonly description;
readonly search: SearchFunctionType<T, M>;
readonly load: LoadFunctionType<T, M>;
private readonly parameters;
private readonly loaderManager;
private readonly folderCache;
private readonly contentCache;
private readonly configCache;
constructor(name: string, description?: ConfigDescriptionParameter<T>, options?: ConfinodeOptions<M>);
/**
* Clear the cache.
*/
clearCache(): void;
/**
* Asynchronously search for configuration.
*
* @param searchStart - The place where search will start, current folder by default.
* @returns A promise resolving to the configuration if found, undefined otherwise.
*/
private asyncSearch;
/**
* Synchronously search for configuration.
*
* @param searchStart - The place where search will start, current folder by default.
* @returns The configuration if found, undefined otherwise.
*/
private syncSearch;
/**
* Search for configuration.
*
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param searchStart - The place where search will start, current folder by default.
* @returns A promise resolving to the configuration if found, undefined otherwise.
*/
private searchConfig;
/**
* Search for configuration in given folder. This is a recursive method.
*
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param folder - The folder to search in.
* @param ignoreAbsolute - True to ignore absolute file names.
* @returns A promise resolving to the configuration if found, undefined otherwise.
*/
private searchConfigInFolder;
/**
* Search for configuration using options descriptions.
*
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param folder - The folder in which to search.
* @param ignoreAbsolute - True to ignore absolute file names.
* @returns The found elements or undefined.
*/
private searchConfigUsingDescriptions;
/**
* Search a file and its loader matching the given description in the given folder.
*
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param folder - The folder in which to search.
* @param description - The file description.
* @param ignoreAbsolute - True to ignore absolute file names.
* @returns The found file and loader (and possible loader name), or undefined if none.
*/
private searchFileAndLoader;
/**
* Simulate a generator for the known loader.
*
* @param loader - The loader to use.
* @returns The known loader, formatted as if returned by loader manager.
*/
private buildKnownLoaderGenerator;
/**
* Build the configuration file name (possibly without extension) based on the file name and the folder.
*
* @param folder - The folder in which to search for the file.
* @param fileName - The name of the file.
* @param ignoreAbsolute - True to ignore absolute file names.
* @returns The built file name or undefined if should be ignored.
*/
private buildConfigurationFileName;
/**
* Find the loader data for the given files.
*
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param folder - The folder in which search is done.
* @param baseName - The base name (start) of the file, including the `.` preceding extension.
* @param fileNames - The name of the files for which to find loaders.
* @returns All found loader data as: full path to file, loader generator.
*/
private findLoaderData;
/**
* Asynchronously load the configuration file.
*
* @param name - The name of the configuration file. The name may be an absolute file path, a relative
* file path, or a module name and an optional file path.
* @returns A promise resolving to the configuration if loaded, undefined otherwise.
*/
private asyncLoad;
/**
* Synchronously load the configuration file.
*
* @param name - The name of the configuration file. The name may be an absolute file path, a relative
* file path, or a module name and an optional file path.
* @returns The configuration if loader, undefined otherwise.
*/
private syncLoad;
/**
* Load configuration from file with given name.
*
* @param name - The name of the configuration file. The name may be an absolute file path, a relative
* file path, or a module name and an optional file path.
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param folder - The folder to resolve name from, defaults to current directory.
* @param loadingParameters - The parameters for the current loading process.
* @returns The configuration if loaded, undefined otherwise.
*/
private loadConfig;
/**
* Load the configuration file.
*
* @param fileName - The name of the file to load.
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param loaderOrLoading - The loader to use (if already found, search file cases) or the loading
* parameters (loading in progress).
* @returns The method will return the configuration, or undefined if content is empty (the meaning of
* empty depends on the loader). May throw an error if loading problem.
*/
private loadConfigFile;
/**
* Load configuration file (raw) content or throw an exception if no loader can load the file.
*
* @param fileName - The name of the file whose content needs to be loaded.
* @param loaders - The loader generators.
* @returns The file content.
*/
private loadConfigFileContent;
/**
* Parse the file content.
*
* @param fileName - The name of file being parsed.
* @param syncOnly - Do not use loaders if they cannot load synchronously.
* @param loadingParameters - The loading parameters.
* @param content - The content of the file.
* @returns The parsing result.
*/
private parseConfigFileContent;
/**
* Log the exception as an error of given type.
*
* @param isException - Indicate that this is an exception.
* @param exception - The exception to log.
*/
private log;
}
export {};