@atomist/automation-client
Version:
Atomist API for software low-level client
52 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const metadataReading_1 = require("./internal/metadata/metadataReading");
const string_1 = require("./internal/util/string");
const scan_1 = require("./scan");
const constructionUtils_1 = require("./util/constructionUtils");
/**
* Create a HandleCommand instance with the appropriate metadata wrapping
* the given function
* @param h handle function
* @param factory construction function
* @param {string} name can be omitted if the function isn't exported
* @param {string} description
* @param {string[]} intent
* @param {Tag[]} tags
* @return {HandleCommand<P>}
*/
function commandHandlerFrom(h, factory, name = h.name || `Command${string_1.generateHash(h.toString())}`, description = name, intent = [], tags = []) {
const handler = new FunctionWrappingCommandHandler(name, description, h, factory, tags, intent);
scan_1.registerCommand(handler);
return handler;
}
exports.commandHandlerFrom = commandHandlerFrom;
class FunctionWrappingCommandHandler {
constructor(name, description, h, parametersFactory,
// tslint:disable-next-line:variable-name
_tags = [],
// tslint:disable-next-line:variable-name
_intent = []) {
this.name = name;
this.description = description;
this.h = h;
this.parametersFactory = parametersFactory;
this._tags = _tags;
this._intent = _intent;
const newParamInstance = this.freshParametersInstance();
const md = metadataReading_1.metadataFromInstance(newParamInstance);
this.parameters = md.parameters;
this.mapped_parameters = md.mapped_parameters;
this.values = md.values;
this.secrets = md.secrets;
this.intent = string_1.toStringArray(_intent);
this.tags = string_1.toStringArray(_tags).map(t => ({ name: t, description: t }));
}
freshParametersInstance() {
return constructionUtils_1.toFactory(this.parametersFactory)();
}
handle(ctx, params) {
return this.h(ctx, params);
}
}
//# sourceMappingURL=onCommand.js.map