@atomist/rug
Version:
TypeScript model for Atomist Rugs, see http://docs.atomist.com/
304 lines (303 loc) • 9.95 kB
JavaScript
;
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
function set_metadata(obj, key, value) {
var target = obj;
if (obj.prototype !== undefined) {
// should only be true for class Decorators
target = obj.prototype;
}
Object.defineProperty(target, key, {
value: value,
writable: false,
enumerable: false,
configurable: false,
});
}
function get_metadata(obj, key) {
if (obj == null) {
return null;
}
var desc = Object.getOwnPropertyDescriptor(obj, key);
if ((desc == null || desc === undefined) && (Object.getPrototypeOf(obj) !== undefined)) {
desc = get_metadata(Object.getPrototypeOf(obj), key);
}
if (desc != null && desc !== undefined) {
return desc.value;
}
return null;
}
/**
* Decorator for parameters. Adds to object properties
*/
function Parameter(details) {
return function (target, propertyKey) {
declareParameter(target, propertyKey, details);
};
}
exports.Parameter = Parameter;
function declareParameter(target, propertyKey, details) {
var params = get_metadata(target, "__parameters");
if (params == null) {
params = [];
}
else {
// remove any that have the same name already (i.e. if folk are calling declareParameter)
// use a cheeky method so that we can reuse the same array
var found = params.filter(function (p) { return p.name === propertyKey; });
if (found != null && found.length > 0) {
var index = params.indexOf(found[0]);
params.splice(index, 1);
}
}
var copy = __assign({}, details);
copy.name = propertyKey;
copy.decorated = true;
params.push(copy);
// merge parameters from parent if it has some
var protoParams = get_metadata(Object.getPrototypeOf(target), "__parameters");
if (protoParams != null) {
protoParams.forEach(function (protoParam) {
// if we don't already have a parameter with the same name
if (!params.some(function (param) { return param.name === protoParam.name; })) {
params.push(protoParam);
}
});
}
set_metadata(target, "__parameters", params);
return target;
}
exports.declareParameter = declareParameter;
/**
* Map a local field to some other configuration item in a different system
*/
function MappedParameter(foreignKey) {
return function (target, localKey) {
declareMappedParameter(target, localKey, foreignKey);
};
}
exports.MappedParameter = MappedParameter;
function declareMappedParameter(target, localKey, foreignKey) {
var params = get_metadata(target, "__mappedParameters");
if (params == null) {
params = [];
}
else {
// remove any that have the same name already (i.e. if folk are calling declareParameter)
// use a cheeky method so that we can reuse the same array
var found = params.filter(function (p) { return p.localKey === localKey; });
if (found != null && found.length > 0) {
var index = params.indexOf(found[0]);
params.splice(index, 1);
}
}
var param = { localKey: localKey, foreignKey: foreignKey };
params.push(param);
// merge parameters from parent if it has some
var protoParams = get_metadata(Object.getPrototypeOf(target), "__mappedParameters");
if (protoParams != null) {
protoParams.forEach(function (protoParam) {
// if we don't already have a parameter with the same name
if (!params.some(function (p) { return p.localKey === protoParam.localKey; })) {
params.push(protoParam);
}
});
}
set_metadata(target, "__mappedParameters", params);
return target;
}
exports.declareMappedParameter = declareMappedParameter;
function ResponseHandler(nameOrDescription, description) {
return function (obj) {
// for backwards compatibility
if (description === undefined) {
declareResponseHandler(obj, nameOrDescription);
}
else {
declareResponseHandler(obj, description, nameOrDescription);
}
};
}
exports.ResponseHandler = ResponseHandler;
function declareResponseHandler(obj, description, name) {
declareRug(obj, "response-handler", description, name);
return obj;
}
exports.declareResponseHandler = declareResponseHandler;
function CommandHandler(nameOrDescription, description) {
return function (obj) {
// for backwards compatibility
if (description === undefined) {
declareCommandHandler(obj, nameOrDescription);
}
else {
declareCommandHandler(obj, description, nameOrDescription);
}
};
}
exports.CommandHandler = CommandHandler;
function IntegrationTest(description) {
return function (obj) {
if (typeof description === "string") {
declareCommandHandler(obj, description);
declareIntegrationTest(obj, { description: description, kind: "integration" });
}
else {
declareCommandHandler(obj, description.description);
declareIntegrationTest(obj, description);
}
};
}
exports.IntegrationTest = IntegrationTest;
/**
* Describe to whom this rug is visible (not to be confused by ACLs)
* This stuff is not enforced, but specifies developer intent.
*
* @param obj - a rug
* @param scope
*/
function setScope(obj, scope) {
set_metadata(obj, "__scope", scope);
return obj;
}
exports.setScope = setScope;
function declareIntegrationTest(obj, descriptor) {
set_metadata(obj, "__test", descriptor);
}
exports.declareIntegrationTest = declareIntegrationTest;
function declareCommandHandler(obj, description, name) {
declareRug(obj, "command-handler", description, name);
return obj;
}
exports.declareCommandHandler = declareCommandHandler;
function Generator(nameOrDescription, description) {
return function (obj) {
// for backwards compatibility
if (description === undefined) {
declareGenerator(obj, nameOrDescription);
}
else {
declareGenerator(obj, description, nameOrDescription);
}
};
}
exports.Generator = Generator;
function declareGenerator(obj, description, name) {
declareRug(obj, "generator", description, name);
return obj;
}
exports.declareGenerator = declareGenerator;
function Editor(nameOrDescription, description) {
return function (obj) {
// for backwards compatibility
if (description === undefined) {
declareEditor(obj, nameOrDescription);
}
else {
declareEditor(obj, description, nameOrDescription);
}
};
}
exports.Editor = Editor;
function declareEditor(obj, description, name) {
declareRug(obj, "editor", description, name);
return obj;
}
exports.declareEditor = declareEditor;
function declareRug(obj, kind, description, name) {
if (name === undefined) {
set_metadata(obj, "__description", description);
}
else {
set_metadata(obj, "__description", description);
set_metadata(obj, "__name", name);
}
set_metadata(obj, "__kind", kind);
}
function EventHandler(nameOrDescription, descriptionOrExpression, expression) {
return function (obj) {
if (expression !== undefined) {
if (typeof descriptionOrExpression === "string") {
declareEventHandler(obj, descriptionOrExpression, expression, nameOrDescription);
}
else {
throw Error("When three parameters are passed, the second must be a description");
}
}
else {
declareEventHandler(obj, nameOrDescription, descriptionOrExpression);
}
};
}
exports.EventHandler = EventHandler;
function declareEventHandler(obj, description, expression, name) {
declareRug(obj, "event-handler", description, name);
if (typeof expression === "string") {
set_metadata(obj, "__expression", expression);
}
else {
set_metadata(obj, "__expression", expression.expression);
}
return obj;
}
exports.declareEventHandler = declareEventHandler;
/**
* Decorator for tags. Sets tags on the class
*/
function Tags() {
var tags = [];
for (var _i = 0; _i < arguments.length; _i++) {
tags[_i] = arguments[_i];
}
return function (target) {
declareTags(target, tags);
};
}
exports.Tags = Tags;
function declareTags(target, tags) {
set_metadata(target, "__tags", tags);
return target;
}
exports.declareTags = declareTags;
function Intent() {
var intent = [];
for (var _i = 0; _i < arguments.length; _i++) {
intent[_i] = arguments[_i];
}
return function (target) {
declareIntent(target, intent);
};
}
exports.Intent = Intent;
function declareIntent(target, intent) {
set_metadata(target, "__intent", intent);
return target;
}
exports.declareIntent = declareIntent;
function Secrets() {
var secrets = [];
for (var _i = 0; _i < arguments.length; _i++) {
secrets[_i] = arguments[_i];
}
return function (target) {
declareSecrets(target, secrets);
};
}
exports.Secrets = Secrets;
function declareSecrets(target, secrets) {
set_metadata(target, "__secrets", secrets);
return target;
}
exports.declareSecrets = declareSecrets;
// for parameters to ResponseHandlers to do response body coercion
function ParseJson(target, propertyKey, parameterIndex) {
set_metadata(target, "__coercion", "JSON");
}
exports.ParseJson = ParseJson;