Config Utilities
Configuration file management can be a pain. This package contains a collection of helpers to make config management easier.
JSON Config Files
In the JavaScript/Typescript ecosystem, most configuration files are stored as JSON files.
ConfigFile
base class
This package provides a base ConfigFile
base class to make saving, loading, and typing JSON-based configuration files easier.
- Uses JSON5 for loading, so that your config files can contain comments and other JSON-incompatible features.
- Can follow
"extends"
fields.
The following is a sample for how to create a custom config class using this package's base class.
import { ConfigFile, ConfigFileOptions } from '@bscotch/config';
interface MyConfigOptions {
someValue: string;
someOtherValue: { hello: number }[];
}
class MyConfigClass extends ConfigFile<MyConfigOptions> {
constructor(
options: Omit<ConfigFileOptions<MyConfigOptions>, 'defaultBaseName'>,
) {
super({ defaultBasename: 'my-config.json', ...options });
}
async cumulativeOptions() {
const chain = await this.inheritenceChain();
const options = chain.reduce((cumulative, current) => {
Object.assign(cumulative, current);
return cumulative;
}, {});
return options;
}
}
const config = new ConfigFile<MyConfig>();
const options = await config.cumulativeOptions();
PackageJson
class
This package provides a PackageJson
class for working with package.json
files. It extends the ConfigFile
base class.
- Bump versions
- Check for and change dependencies
- Discover all files that would be included by
npm pack
- Enforce/check publish-ability
- Supports monorepos
- Follows dependencies that use the
file:
protocol. - Checks publish-ability based on local dependencies, e.g. if a local dep is private the package is treated as unpublishable.
import { PackageJson } from '@bscotch/config';
interface CustomFields {
myField: string;
myOtherField: { hello: number }[];
}
const pkg = await PackageJson.findPackageJson<CustomFields>();
const tsDep = pkg.findDependency('typescript');
await pkg.bumpVersion('minor');
TsConfig
class
Typescript configuration options are specified with tsconfig.json
files. These files are loaded and used by a wide variety of tools, though any given tool may only support a subset of options or config versions.
The TsConfig
helper class provides utilities for various features that are useful for managing a Typescript project, mostly for simplifying the creation of tools that operate on Typescript projects.
- Get cumulative options by following "extends" fields.
- Find all source files that would be included, even those included via
"references"
. - Check if a given path/module is included by a Typescript project, with alias resolution.
import { TsConfig } from '@bscotch/config';
const mainConfig = await TsConfig.resolve();
const options = await mainConfig.cumulativeConfig();
const configs = await mainConfig.resolveProjectReferenceTree();