/** * Customization options when running {@link Config.configure} */ interface ConfigOptions { force?: boolean; } /** * The class encapsulating all the logic for simple-app-config */ export declare class Config { /** * The set of config file types that are supported. */ private static readonly configFileTypes; /** * The current environment. Defaults to development. * Set when {@link Config.determineEnvironment} is called. */ private static environment; /** * The environments of the application. * Defaults to development, testing, staging, production. * Populated with custom values when {@link Config.setEnvironmentNames} is called. */ private static environments; /** * The set of possible paths of the default config file. * Populated when {@link Config.setPaths} is called. */ private static defaultConfigPaths; /** * The directory of where the .env files reside. This can be relative or absolute. Defaults to "". * Set when {@link Config.setEnvDir} is called. */ private static envDir; /** * The directory of where the config directory resides. This can be relative or absolute. Defaults to "". * Set when {@link Config.setConfigDir} is called. */ private static configDir; /** * The Map of environments to .env paths. * Populated when {@link Config.setPaths} is called. */ private static envPaths; /** * The Map of environments to a set of possible paths for the config file. * Populated when {@link Config.setPaths} is called. */ private static configPaths; /** * Map containing converted and expanded values from the loaded config file. * Populated when {@link Config.parseConfigIntoMap} is called. */ private static configMap; /** * Flag indicating whether configuration has already been performed. * This is set to true after {@link Config.configure} is finished running. */ private static alreadyConfigured; /** * The previously set environment variables by dotenv.config * Populated when {@link Config.loadEnvFile} is called. */ private static prevDotenvValues; /** * Singleton instance of the config. This is currently unused. */ private static instance; /** * The entry point to all of the configuration functions. Configures the module in the following order: * - Returns if the module has already been configured and the force flag isn't set * - Sets the environment names * - Determines the environment * - Sets the environment dir and config dir if specified * - Sets the paths to the .env and possible config files based on the environment names * - Finds and loads the .env file * - Loads environment variables into the cache * - Finds and loads the config file * - Loads the default config file * - Sets already configured flag to true. * @param force Whether to force the module to re-configure, even if it has already been configured. */ static configure(configOptions?: ConfigOptions): void; /** * Sets environment names if they are specified in either of the following from highest priority to lowest, where higher * priority values will override lower priority values: * - Command line arguments * - Environment variables * * The environment names default to 'development', 'testing', 'staging', and 'production'. Clears any previously set environment * names if custom ones are set. */ private static setEnvironmentNames; /** * Sets the directory of the .env files. Resets any previously set dir. */ private static setEnvDir; /** * Sets the directory of the config directory. Resets any previously set dir. */ private static setConfigDir; /** * Populates the following: * - The paths of the .env files based on the environments and directory prefix * - The possible paths of the config files based on the environments and directory prefix * - The default config file * * Clears any previously set .env and config paths. */ private static setPaths; /** * Determines the environment from the following from highest priority to lowest, where higher priority values will override * lower priority values: * - Command line arguments * - NODE_ENV environment variable * * The environment defaults to 'development'. */ private static determineEnvironment; /** * Loads a .env file from the following in the following order from highest priority to lowest, where higher priority values * will override lower priority values: * - Command line args * - Environment variables * - The path corresponding to the environment * * If the path specified in each of the above doesn't exist, it will try the next highest priority value. */ private static findEnvFile; /** * Loads the .env file if it has been found and set. Clears previously loaded values from a .env file */ private static loadEnvFile; /** * Checks if a .env file is valid. A .env file is valid if all the below are true: * - It is a safe path, meaning its prefix is the root of the project * - It exists * @param path The path of the .env file to check. * @returns A boolean indicating whether the .env file is valid. */ private static isValidEnvFile; /** * Returns whether the path is safe. A path is safe if: * - Its prefix begins with the absolute path to the root of the project * @param path */ private static isSafePath; /** * Resets and values set previously by dotenv to undefined from the previous call of dotenv.config * @param Path to the .env file. */ private static resetDotEnv; /** * Loads a config file from the folloowing in the following order from highest priority to lowest, where higher priority values * will override lower priority values:: * - Command line arguments * - Environment variables * - The path corresponding to the environment * * If the path specified in each of the above doesn't exist, it will try the next highest priority value. Clears any previously */ private static findConfigFile; /** * Loads the config file if it has been found and set. Clears any previously loaded values. */ private static loadConfigFile; /** * Checks if a config file is valid. A config file is valid if all the below are true: * - It is a safe path, meaning its prefix is the root of the project * - It exists * - It isn't a directory * - It matches a valid file extension * @param filePath The path of the config file to check. * @returns A boolean indicating whether the config file is valid. */ private static isValidConfigFile; /** * Loads the default config file, and updates config values that have not yet been set. */ private static loadDefaultConfigFile; /** * Helper method to parse a config file given a path and load it into {@link Config.configMap}. Performs the following in the listed order: * - Reads the config file * - Parses the read file depending on the type of the config file. Processes the object into a Map. Nested configurations will * be recursively parsed into nested maps. * - Updates the global config map with values that haven't been set yet. This is so that default config values don't override * ones with higher priority. * * This function assumes that the file path is valid since it should be called from {@link Config.findConfigFile} and * {@link Config.loadDefaultConfigFile}, and file validation is performed there. * * @param filePath Path to load the config file from. */ private static parseConfigIntoMap; /** * Parses the text read from a JSON file and returns it as a Map. * @param file The text representing the JSON file after reading it. * @returns A Map of the key-value pairs in the JSON file. */ private static parseJSON; /** * Converts a JSON object into a Map. Performs the following in the listed order recursively: * - Converts the input to an array if it is an array * - Process each in the JSON object. If it is a string it expands any environment variables within the string. If it is an * object it will recursively process the object and treat it like a nested map of configurations * @param jsonObj The JSON object to process. * @returns a Map of the key-value pairs in the JSON file. */ private static convertJSONToMap; /** * Expands an environment variable, and performs type conversions if it is not nested in a string. * * Fields starting with $ will be treated as a special case and matched as $VAR::TYPE:SUBTYPE1:SUBTYPE2. They will be converted * to the type TYPE with SUBTYPE1 and SUBTYPE2 if it is a nested structure like an Array, Set, or Map. * * Fields that are string containing ${...} will be matched as ${VAR::TYPE:SUBTYPE1:SUBTYPE2}, and will be treated as a string * regardless of the types specified and expanded as a string. * @param input The input string to expand * @returns The expanded or converted form of the input. */ private static expandEnvVar; /** * Get a field from the config map containing processed fields from the config files. Iteratively searches for nested * configuration values. Each level of the configuration key must be separated with a '.' * @param key The name of the field in the config file. * @returns The expanded and converted value in the config file. */ static get(key: string): T; /** * Private constructor */ private constructor(); } export {};