What is @rushstack/heft-config-file?
@rushstack/heft-config-file is a package that provides utilities for reading and writing configuration files in a standardized way. It is part of the Rush Stack, a set of tools and libraries for managing large-scale TypeScript monorepos.
What are @rushstack/heft-config-file's main functionalities?
Reading Configuration Files
This feature allows you to read configuration files in a standardized way. The code sample demonstrates how to create a ConfigurationFile instance and load a configuration file asynchronously.
const { ConfigurationFile } = require('@rushstack/heft-config-file');
const myConfigFile = new ConfigurationFile({
projectRelativeFilePath: 'config/my-config.json'
});
async function loadConfig() {
const config = await myConfigFile.loadConfigurationFileForProjectAsync(__dirname);
console.log(config);
}
loadConfig();
Writing Configuration Files
This feature allows you to write configuration files in a standardized way. The code sample demonstrates how to use the JsonFile utility to save a configuration object to a file asynchronously.
const { JsonFile } = require('@rushstack/heft-config-file');
const configData = {
setting1: 'value1',
setting2: 'value2'
};
async function saveConfig() {
await JsonFile.saveAsync(configData, 'config/my-config.json');
console.log('Configuration saved.');
}
saveConfig();
Schema Validation
This feature allows you to validate configuration files against a JSON schema. The code sample demonstrates how to create a ConfigurationFile instance with a schema and load a configuration file with validation.
const { ConfigurationFile } = require('@rushstack/heft-config-file');
const { JsonSchema } = require('@rushstack/node-core-library');
const schema = JsonSchema.fromFile('config/my-config.schema.json');
const myConfigFile = new ConfigurationFile({
projectRelativeFilePath: 'config/my-config.json',
jsonSchema: schema
});
async function loadConfig() {
const config = await myConfigFile.loadConfigurationFileForProjectAsync(__dirname);
console.log(config);
}
loadConfig();
Other packages similar to @rushstack/heft-config-file
cosmiconfig
Cosmiconfig is a popular library for finding and loading configuration files. It supports a variety of file formats and locations. Compared to @rushstack/heft-config-file, cosmiconfig is more flexible in terms of file formats and locations but does not provide built-in schema validation.
config
The config package is a configuration manager for Node.js applications. It allows you to define configuration files in a hierarchical manner and supports environment-specific configurations. Compared to @rushstack/heft-config-file, the config package is more focused on managing hierarchical configurations and environment-specific settings.
rc
The rc package is a simple configuration loader that supports various file formats and locations. It is lightweight and easy to use. Compared to @rushstack/heft-config-file, rc is more lightweight and flexible but lacks advanced features like schema validation.