1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const metadataReading_1 = require("./internal/metadata/metadataReading");
|
4 | const string_1 = require("./internal/util/string");
|
5 | const scan_1 = require("./scan");
|
6 | const constructionUtils_1 = require("./util/constructionUtils");
|
7 | var 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 |
|
16 |
|
17 |
|
18 | function 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 | }
|
31 | exports.addRegExIntentAnchors = addRegExIntentAnchors;
|
32 |
|
33 |
|
34 |
|
35 | function 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 | }
|
52 | exports.parseRegExIntent = parseRegExIntent;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function 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 | }
|
63 | exports.commandHandlerFrom = commandHandlerFrom;
|
64 | class 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 |
|
86 | freshParametersInstance() {
|
87 | return constructionUtils_1.toFactory(this.parametersFactory)();
|
88 | }
|
89 | handle(ctx, params) {
|
90 | return this.h(ctx, params);
|
91 | }
|
92 | }
|
93 |
|
\ | No newline at end of file |