UNPKG

2.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const _ = require("lodash");
4const automationMetadata_1 = require("../metadata/automationMetadata");
5/**
6 * Populate the parameters of the command handler instance,
7 * performing type coercion if necessary
8 * @param instanceToPopulate parameters instance (may be handler instance itself)
9 * @param hm handler metadata
10 * @param args string args
11 */
12function populateParameters(instanceToPopulate, hm, args) {
13 args.forEach(arg => {
14 if (arg.value !== undefined) {
15 const parameter = hm.parameters.find(p => p.name === arg.name);
16 if (parameter) {
17 _.update(instanceToPopulate, parameter.name, () => computeValue(parameter, arg.value));
18 }
19 }
20 });
21}
22exports.populateParameters = populateParameters;
23function populateValues(instanceToPopulate, am, configuration) {
24 (am.values || []).forEach(v => {
25 const configValue = _.get(configuration, v.path);
26 if (!configValue && v.required) {
27 throw new Error(`Required @Value '${v.path}' in '${instanceToPopulate.constructor.name}' is not available in configuration`);
28 }
29 else {
30 _.update(instanceToPopulate, v.name, () => computeValue({ name: v.name, type: v.type }, configValue));
31 }
32 });
33}
34exports.populateValues = populateValues;
35function computeValue(parameter, value) {
36 // Convert type if necessary
37 switch (parameter.type) {
38 case "string":
39 case undefined:
40 // It's a string. Keep the value the same
41 break;
42 case automationMetadata_1.FreeChoices:
43 // It's a string array. Keep the value the same
44 break;
45 case "boolean":
46 if (typeof value !== "boolean") {
47 value = value === "true" || value === "yes" || value === "1";
48 }
49 break;
50 case "number":
51 if (typeof value === "string") {
52 value = parseInt(value, 10);
53 }
54 else {
55 throw new Error(`Parameter '${parameter.name}' has array value, but is numeric`);
56 }
57 break;
58 default:
59 // It's a Chooser
60 const chooser = parameter.type;
61 if (chooser.pickOne) {
62 if (typeof value !== "string") {
63 throw new Error(`Parameter '${parameter.name}' has array value, but should be string`);
64 }
65 }
66 else {
67 if (typeof value.value === "string") {
68 throw new Error(`Parameter '${parameter.name}' has string value, but should be array`);
69 }
70 }
71 break;
72 }
73 return value;
74}
75//# sourceMappingURL=parameterPopulation.js.map
\No newline at end of file