@cucumber/cucumber
Version:
The official JavaScript implementation of Cucumber.
132 lines • 4.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
class PluginManager {
environment;
handlers = {
message: [],
'paths:resolve': [],
'publish:url': [],
};
transformers = {
'pickles:filter': [],
'pickles:order': [],
};
cleanupFns = [];
constructor(environment) {
this.environment = environment;
}
async registerHandler(event, handler, specifier) {
if (!this.handlers[event]) {
throw new Error(`Cannot register handler for unknown event "${event}"`);
}
this.handlers[event].push({
handler,
specifier,
});
}
async registerTransformer(event, transformer, specifier) {
if (!this.transformers[event]) {
throw new Error(`Cannot register transformer for unknown event "${event}"`);
}
this.transformers[event].push({
transformer,
specifier,
});
}
async initFormatter(plugin, options, stream, write, directory, specifier) {
const cleanupFn = await plugin.formatter({
on: (key, handler) => this.registerHandler(key, handler, specifier),
options: plugin.optionsKey
? (options[plugin.optionsKey] ?? {})
: options,
logger: this.environment.logger,
stream,
write,
directory,
});
if (typeof cleanupFn === 'function') {
this.cleanupFns.push({
cleanupFn: cleanupFn,
specifier,
});
}
}
async initCoordinatorExternal(operation, plugin, options, specifier) {
const context = this.makeCoordinatorContext(operation, plugin, options, specifier);
await this.initCoordinator(plugin, context, specifier);
}
async initCoordinatorInternal(operation, plugin, options) {
const context = {
...this.makeCoordinatorContext(operation, plugin, options),
emit: this.emit.bind(this),
};
await this.initCoordinator(plugin, context);
}
async initCoordinator(plugin, context, specifier) {
const cleanupFn = await wrapErrorAsync(async () => await plugin.coordinator(context), `${formatCulprit(specifier)} errored when trying to init`);
if (typeof cleanupFn === 'function') {
this.cleanupFns.push({
cleanupFn: cleanupFn,
specifier,
});
}
}
makeCoordinatorContext(operation, plugin, options, specifier) {
return {
operation,
on: (event, handler) => this.registerHandler(event, handler, specifier),
transform: (event, transformer) => this.registerTransformer(event, transformer, specifier),
options: 'optionsKey' in plugin && plugin.optionsKey
? (options[plugin.optionsKey] ?? {})
: options,
logger: this.environment.logger,
environment: {
cwd: this.environment.cwd,
stderr: this.environment.stderr,
env: { ...this.environment.env },
},
};
}
emit(event, value) {
this.handlers[event].forEach(({ handler, specifier }) => {
wrapError(() => handler(value), `${formatCulprit(specifier)} errored when trying to handle a "${event}" event`);
});
}
async transform(event, value) {
let transformed = value;
for (const { transformer, specifier } of this.transformers[event]) {
const returned = await wrapErrorAsync(async () => await transformer(transformed), `${formatCulprit(specifier)} errored when trying to do a "${event}" transform`);
if (typeof returned !== 'undefined') {
transformed = returned;
}
}
return transformed;
}
async cleanup() {
for (const { cleanupFn, specifier } of this.cleanupFns) {
await wrapErrorAsync(async () => await cleanupFn(), `${formatCulprit(specifier)} errored when trying to cleanup`);
}
}
}
exports.PluginManager = PluginManager;
function formatCulprit(specifier) {
return specifier ? `Plugin "${specifier}"` : 'Cucumber';
}
function wrapError(fn, message) {
try {
return fn();
}
catch (error) {
throw new Error(message, { cause: error });
}
}
async function wrapErrorAsync(fn, message) {
try {
return await fn();
}
catch (error) {
throw new Error(message, { cause: error });
}
}
//# sourceMappingURL=plugin_manager.js.map