Adapter installation and project configuration functionality for setting up commitizen in new and existing projects.
Main initialization function that installs adapters and configures projects for commitizen usage.
/**
* Installs commitizen adapter and configures project
* @param repoPath - Path to repository root
* @param adapterNpmName - NPM package name of adapter to install
* @param options - Installation and configuration options
* @throws Error if required arguments are missing or installation fails
*/
function init(
repoPath: string,
adapterNpmName: string,
options?: InitOptions
): void;
interface InitOptions {
save?: boolean; // Add to dependencies (npm)
saveDev?: boolean; // Add to devDependencies (npm/pnpm)
saveExact?: boolean; // Install exact version (npm/pnpm)
force?: boolean; // Force install over existing adapter
yarn?: boolean; // Use yarn package manager
dev?: boolean; // Add to devDependencies (yarn)
exact?: boolean; // Install exact version (yarn)
pnpm?: boolean; // Use pnpm package manager
includeCommitizen?: boolean; // Also install commitizen package
}Usage Examples:
const { init } = require('commitizen');
// Basic initialization with npm
init(process.cwd(), 'cz-conventional-changelog', {
saveDev: true,
saveExact: true
});
// Force installation over existing adapter
init(process.cwd(), 'cz-conventional-changelog', {
saveDev: true,
force: true
});
// Yarn installation
init(process.cwd(), 'cz-conventional-changelog', {
yarn: true,
dev: true,
exact: true
});
// pnpm installation with commitizen included
init(process.cwd(), 'cz-conventional-changelog', {
pnpm: true,
saveDev: true,
saveExact: true,
includeCommitizen: true
});The initialization function uses sensible defaults for most scenarios:
const defaultInitOptions: InitOptions = {
save: false, // Don't add to dependencies by default
saveDev: true, // Add to devDependencies by default
saveExact: false, // Allow version ranges by default
force: false, // Don't force install by default
yarn: false, // Use npm by default
dev: true, // Add to devDependencies (yarn equivalent)
exact: false, // Allow version ranges (yarn equivalent)
pnpm: false, // Use npm by default
};Built-in validation ensures required parameters are provided:
/**
* Validates required arguments for initialization
* @param path - Repository path (must be provided)
* @param adapterNpmName - Adapter package name (must be provided)
* @throws Error if required arguments are missing
*/
function checkRequiredArguments(path: string, adapterNpmName: string): void;Loads existing adapter configuration to prevent conflicts:
/**
* Loads existing adapter configuration from project
* @param cwd - Working directory to search for config
* @returns Existing commitizen configuration or undefined
*/
function loadAdapterConfig(cwd: string): CommitizenConfig | undefined;
interface CommitizenConfig {
path: string; // Path to adapter module
}The initialization process follows these steps:
force: true)includeCommitizen: true# Generated command example
npm install cz-conventional-changelog --save-dev --save-exact// Usage
init(process.cwd(), 'cz-conventional-changelog', {
saveDev: true,
saveExact: true
});# Generated command example
yarn add cz-conventional-changelog --dev --exact// Usage
init(process.cwd(), 'cz-conventional-changelog', {
yarn: true,
dev: true,
exact: true
});# Generated command example
pnpm add cz-conventional-changelog --save-dev --save-exact// Usage
init(process.cwd(), 'cz-conventional-changelog', {
pnpm: true,
saveDev: true,
saveExact: true
});After successful initialization, the following configuration is added to package.json:
{
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}Initialization handles various error conditions:
force: trueError Examples:
// Missing required arguments
try {
init(); // Missing both arguments
} catch (error) {
console.error(error.message); // "Path is required when running init."
}
// Existing adapter without force flag
try {
init(process.cwd(), 'new-adapter'); // Existing adapter configured
} catch (error) {
console.error(error.message);
// "A previous adapter is already configured. Use --force to override"
}
// Installation failure
try {
init(process.cwd(), 'nonexistent-adapter');
} catch (error) {
console.error('Installation failed:', error.message);
}Initialize with local or custom adapter paths:
// Local adapter
init(process.cwd(), './custom-adapters/my-adapter', {
saveDev: true
});
// Scoped package
init(process.cwd(), '@company/cz-adapter', {
saveDev: true,
saveExact: true
});Install commitizen along with the adapter:
init(process.cwd(), 'cz-conventional-changelog', {
saveDev: true,
includeCommitizen: true // Also installs commitizen package
});