UNPKG

3.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const metadataReading_1 = require("./internal/metadata/metadataReading");
4const string_1 = require("./internal/util/string");
5const scan_1 = require("./scan");
6const constructionUtils_1 = require("./util/constructionUtils");
7var QuestionStyle;
8(function (QuestionStyle) {
9 QuestionStyle["Dialog"] = "dialog";
10 QuestionStyle["Threaded"] = "threaded";
11 QuestionStyle["Unthreaded"] = "unthreaded";
12 QuestionStyle["DialogAction"] = "dialog_action";
13})(QuestionStyle = exports.QuestionStyle || (exports.QuestionStyle = {}));
14/**
15 * Given a regex used for an intent, add anchors to it if missing. Additionally, wraps the new anchors in match groups along with the original
16 * intent for access later.
17 */
18function addRegExIntentAnchors(intent) {
19 if (!(intent instanceof RegExp)) {
20 throw new Error(`Intent must be an instance of RegExp!`);
21 }
22 let regex = intent.source;
23 if (!regex.startsWith("^")) {
24 regex = "^[\\s\\S]*" + regex;
25 }
26 if (!regex.endsWith("$")) {
27 regex = regex + "[\\s\\S]*$";
28 }
29 return regex;
30}
31exports.addRegExIntentAnchors = addRegExIntentAnchors;
32/**
33 * Parse command handler intent. If the incoming intent is a RegExp convert it to a string pattern. Otherwise, pass existing intent through.
34 */
35function parseRegExIntent(intent) {
36 if (typeof intent === "string" && intent.length > 0) {
37 return intent;
38 }
39 else if ((Array.isArray(intent) && (intent.length < 1 || intent.every(i => typeof i === "string")))) {
40 return intent;
41 }
42 else if (intent instanceof RegExp) {
43 return addRegExIntentAnchors(intent);
44 }
45 else if (intent === undefined || intent === null || (typeof intent === "string" && intent === "")) {
46 throw new Error(`Intent cannot be undefined, null, or empty!`);
47 }
48 else {
49 throw new Error(`Unknown Intent type of ${typeof intent} supplied!`);
50 }
51}
52exports.parseRegExIntent = parseRegExIntent;
53/**
54 * Create a HandleCommand instance with the appropriate metadata wrapping
55 * the given function
56 */
57function commandHandlerFrom(h, factory, name = h.name || `Command${string_1.generateHash(h.toString())}`, description = name, intent = [], tags = [], autoSubmit = false, question) {
58 const updatedIntent = parseRegExIntent(intent);
59 const handler = new FunctionWrappingCommandHandler(name, description, h, factory, tags, updatedIntent, autoSubmit, question);
60 scan_1.registerCommand(handler);
61 return handler;
62}
63exports.commandHandlerFrom = commandHandlerFrom;
64class FunctionWrappingCommandHandler {
65 constructor(name, description, h, parametersFactory, _tags = [], _intent = [], autoSubmit = false, _question) {
66 this.name = name;
67 this.description = description;
68 this.h = h;
69 this.parametersFactory = parametersFactory;
70 this._tags = _tags;
71 this._intent = _intent;
72 this.autoSubmit = autoSubmit;
73 this._question = _question;
74 const newParamInstance = this.freshParametersInstance();
75 const md = metadataReading_1.metadataFromInstance(newParamInstance);
76 this.parameters = md.parameters;
77 this.mapped_parameters = md.mapped_parameters;
78 this.values = md.values;
79 this.secrets = md.secrets;
80 this.intent = string_1.toStringArray(_intent);
81 this.tags = string_1.toStringArray(_tags).map(t => ({ name: t, description: t }));
82 this.auto_submit = autoSubmit;
83 this.question = (!!_question ? _question.toString() : undefined);
84 }
85 /* tslint:enAble:variable-name */
86 freshParametersInstance() {
87 return constructionUtils_1.toFactory(this.parametersFactory)();
88 }
89 handle(ctx, params) {
90 return this.h(ctx, params);
91 }
92}
93//# sourceMappingURL=onCommand.js.map
\No newline at end of file