Multi-source configuration resolution system that loads commitizen settings from various file formats and locations with intelligent priority ordering.
Loads commitizen configuration from multiple sources with automatic format detection.
/**
* Loads commitizen configuration from standard locations
* @param config - Optional configuration override
* @param cwd - Working directory to search (defaults to process.cwd())
* @returns Loaded configuration object or undefined if not found
*/
function load(config?: object, cwd?: string): CommitizenConfig | undefined;
interface CommitizenConfig {
path: string; // Path to adapter module
[key: string]: any; // Additional adapter-specific configuration
}Usage Examples:
const { configLoader } = require('commitizen');
// Load config from current directory
const config = configLoader.load();
if (config) {
console.log('Adapter path:', config.path);
} else {
console.log('No commitizen configuration found');
}
// Load config from specific directory
const projectConfig = configLoader.load(null, '/path/to/project');
// Override with custom config
const customConfig = configLoader.load({
path: 'cz-custom-adapter'
}, process.cwd());Lower-level loader that can be configured for different file sources and formats.
/**
* Generic configuration loader with customizable file sources
* @param configs - Array of configuration file names to search
* @param config - Optional configuration override
* @param cwd - Working directory to search
* @returns Loaded and normalized configuration object
*/
function loader(
configs: string[],
config?: object,
cwd?: string
): object | undefined;Find configuration files up the directory tree with pattern matching.
/**
* Find files matching patterns up the directory tree
* @param patterns - Array of glob patterns to match
* @param options - Search options with cwd and other glob options
* @param fn - Filter function to validate found files
* @returns Full path to first matching file or undefined
*/
function findup(
patterns: string[],
options: { cwd: string; nocase?: boolean },
fn: (configPath: string) => boolean
): string | undefined;Load and parse configuration file content with format detection.
/**
* Get content of a configuration file with automatic parsing
* @param configPath - Path to configuration file (relative or absolute)
* @param baseDirectory - Base directory to resolve relative paths
* @returns Parsed configuration object or undefined
*/
function getContent(configPath: string, baseDirectory: string): object | undefined;
/**
* Normalize configuration content based on file type and structure
* @param config - Configuration file name or path
* @param content - Raw configuration content
* @returns Normalized commitizen configuration
*/
function getNormalizedConfig(config: string, content?: object): object | undefined;Usage Examples:
const { configLoader } = require('commitizen');
// Custom configuration sources
const customConfig = configLoader.loader(
['.myrc', '.my.json', 'package.json'],
null,
process.cwd()
);
// Standard commitizen sources
const standardConfig = configLoader.loader(
['.czrc', '.cz.json', 'package.json'],
null,
process.cwd()
);
// Find configuration files manually
const configPath = configLoader.findup(
['.czrc', '.cz.json'],
{ cwd: process.cwd(), nocase: true },
(path) => {
// Validate config file
console.log('Found config at:', path);
return true;
}
);
// Load specific configuration file
if (configPath) {
const content = configLoader.getContent(configPath, process.cwd());
const normalized = configLoader.getNormalizedConfig('package.json', content);
}
// Direct content loading
const packageConfig = configLoader.getContent('package.json', './my-project');
const czrcConfig = configLoader.getContent('.czrc', process.cwd());Commitizen searches for configuration in the following files, in priority order:
.czrc FileJSON format configuration file with direct commitizen settings:
{
"path": "cz-conventional-changelog",
"maxHeaderWidth": 100,
"maxLineWidth": 100
}.cz.json FileAlternative JSON format for commitizen configuration:
{
"path": "cz-conventional-changelog",
"scopes": ["api", "ui", "docs"],
"allowCustomScopes": true
}package.json ConfigurationConfiguration embedded in package.json under the config.commitizen key:
{
"name": "my-project",
"version": "1.0.0",
"config": {
"commitizen": {
"path": "cz-conventional-changelog",
"maxHeaderWidth": 72
}
}
}Searches up the directory tree for configuration files:
/**
* Finds configuration files by searching up directory tree
* @param configs - Array of file names to search for
* @param cwd - Starting directory for search
* @returns Object mapping found files to their paths
*/
function findup(configs: string[], cwd?: string): { [filename: string]: string };Usage Examples:
const { configLoader } = require('commitizen');
// Find all config files
const foundFiles = configLoader.findup(['.czrc', '.cz.json', 'package.json']);
console.log('Found configs:', foundFiles);
// Result: { '.czrc': '/path/to/.czrc', 'package.json': '/path/to/package.json' }Loads and parses configuration file content with format detection:
/**
* Loads content from configuration file with automatic format detection
* @param filepath - Path to configuration file
* @param filename - Name of the file (used for format detection)
* @returns Parsed configuration object
*/
function getContent(filepath: string, filename: string): object | undefined;Usage Examples:
const { configLoader } = require('commitizen');
// Load .czrc file
const czrcContent = configLoader.getContent('/path/to/.czrc', '.czrc');
// Load package.json commitizen config
const packageContent = configLoader.getContent('/path/to/package.json', 'package.json');
// Automatically extracts config.commitizen sectionNormalizes loaded configuration to standard format:
/**
* Normalizes configuration object to standard commitizen format
* @param config - Raw configuration object
* @param filename - Source filename for context
* @returns Normalized configuration
*/
function getNormalizedConfig(config: object, filename: string): CommitizenConfig | undefined;.czrc, .cz.json)Direct JSON format with commitizen configuration at root level:
{
"path": "cz-conventional-changelog",
"maxHeaderWidth": 100,
"maxLineWidth": 100,
"scopes": ["feat", "fix", "docs"],
"allowCustomScopes": true,
"allowBreakingChanges": ["feat", "fix"]
}Configuration embedded under config.commitizen key:
{
"name": "my-project",
"scripts": {
"commit": "cz"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"devDependencies": {
"commitizen": "^4.0.0",
"cz-conventional-changelog": "^3.0.0"
}
}Support for JSON files with JavaScript-style comments:
{
// Primary adapter configuration
"path": "cz-conventional-changelog",
/* Display configuration */
"maxHeaderWidth": 100,
"maxLineWidth": 100
}When multiple configuration sources exist, they are processed in this priority order:
.czrc - Dedicated commitizen configuration file.cz.json - Alternative JSON configuration filepackage.json - Configuration in package.json config.commitizenHigher priority sources completely override lower priority ones (no merging).
Configuration loading handles various error conditions gracefully:
Error Examples:
const { configLoader } = require('commitizen');
// Graceful handling of missing config
const config = configLoader.load();
if (!config) {
console.log('No configuration found, using defaults');
}
// Handle specific directory
try {
const config = configLoader.load(null, '/nonexistent/path');
console.log('Config:', config); // Will be undefined
} catch (error) {
console.error('Configuration loading failed:', error.message);
}Configure loader for custom file patterns:
const { configLoader } = require('commitizen');
// Search custom files
const customSources = ['.commitizenrc', '.commit.json', 'package.json'];
const config = configLoader.loader(customSources, null, process.cwd());Validate loaded configuration:
const config = configLoader.load();
if (config && config.path) {
console.log('Valid configuration found');
console.log('Adapter:', config.path);
} else {
console.error('Invalid or missing configuration');
}