apex-code-coverage-transformer
Version:
Transform Salesforce Apex code coverage JSONs into other formats accepted by SonarQube, GitHub, GitLab, Azure, Bitbucket, etc.
103 lines • 3.06 kB
JavaScript
;
/**
* Registry for coverage format handlers.
* Provides a centralized system for registering and retrieving format handlers.
*
* @example
* ```typescript
* // Register a handler
* HandlerRegistry.register({
* name: 'myformat',
* description: 'My custom format',
* fileExtension: '.xml',
* handler: () => new MyFormatHandler(),
* });
*
* // Retrieve a handler
* const handler = HandlerRegistry.get('myformat');
* ```
*/
export class HandlerRegistry {
static handlers = new Map();
/**
* Register a new format handler.
*
* @param registration - Handler registration information
* @throws Error if a handler with the same name is already registered
*/
static register(registration) {
if (this.handlers.has(registration.name)) {
throw new Error(`Handler for format '${registration.name}' is already registered`);
}
this.handlers.set(registration.name, registration);
}
/**
* Get a handler instance for the specified format.
*
* @param format - Format identifier
* @returns New handler instance
* @throws Error if format is not supported
*/
static get(format) {
const registration = this.handlers.get(format);
if (!registration) {
const available = this.getAvailableFormats().join(', ');
throw new Error(`Unsupported format: ${format}. Available formats: ${available}`);
}
return registration.handler();
}
/**
* Get list of all registered format names.
*
* @returns Array of format identifiers
*/
static getAvailableFormats() {
return Array.from(this.handlers.keys()).sort();
}
/**
* Get file extension for a format.
*
* @param format - Format identifier
* @returns File extension including the dot (e.g., '.xml')
*/
static getExtension(format) {
const registration = this.handlers.get(format);
return registration?.fileExtension ?? '.xml';
}
/**
* Get description for a format.
*
* @param format - Format identifier
* @returns Human-readable description
*/
static getDescription(format) {
const registration = this.handlers.get(format);
return registration?.description ?? '';
}
/**
* Get compatible platforms for a format.
*
* @param format - Format identifier
* @returns Array of compatible platform names
*/
static getCompatiblePlatforms(format) {
const registration = this.handlers.get(format);
return registration?.compatibleWith ?? [];
}
/**
* Check if a format is registered.
*
* @param format - Format identifier
* @returns True if format is registered
*/
static has(format) {
return this.handlers.has(format);
}
/**
* Clear all registered handlers (primarily for testing).
*/
static clear() {
this.handlers.clear();
}
}
//# sourceMappingURL=HandlerRegistry.js.map