@rnv/engine-core
Version:
ReNative Engine Core
128 lines • 6.63 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var core_1 = require("@rnv/core");
var set_1 = tslib_1.__importDefault(require("lodash/set"));
var getContext_1 = require("../../../getContext");
var Question = function (data) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var c, inputs, files, bootstrapQuestions, results, providedAnswers;
var _a, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
c = (0, getContext_1.getContext)();
inputs = data.inputs, files = data.files;
inputs.bootstrapQuestions = {};
bootstrapQuestions = ((_b = (_a = files.template.renativeTemplateConfig) === null || _a === void 0 ? void 0 : _a.bootstrapConfig) === null || _b === void 0 ? void 0 : _b.bootstrapQuestions) || [];
results = {};
providedAnswers = {};
if (c.program.opts().answer) {
c.program.opts().answer.forEach(function (a) {
var key = a.split('=')[0];
var value;
try {
value = JSON.parse(a.split('=')[1]);
}
catch (e) {
value = a.split('=')[1];
}
providedAnswers[key] = value;
});
}
return [4 /*yield*/, interactiveQuestion(results, bootstrapQuestions, providedAnswers)];
case 1:
_c.sent();
(0, core_1.logDebug)('asnwer', JSON.stringify(results, null, 2));
Object.keys(results).forEach(function (targetKey) {
var objValue = results[targetKey].value;
(0, core_1.logDebug)('setting', targetKey, objValue);
if (targetKey) {
(0, set_1.default)(inputs.bootstrapQuestions, targetKey, objValue);
}
});
return [2 /*return*/];
}
});
}); };
var interactiveQuestion = function (results, bootstrapQuestions, providedAnswers) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var _loop_1, i;
var _a, _b, _c, _d;
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
if (!(bootstrapQuestions === null || bootstrapQuestions === void 0 ? void 0 : bootstrapQuestions.length)) return [3 /*break*/, 4];
_loop_1 = function (i) {
var q, qKey, qKeyClean, choicesObj, answer, value, inqQuestion, result, val;
return tslib_1.__generator(this, function (_f) {
switch (_f.label) {
case 0:
q = bootstrapQuestions[i];
qKey = ((_a = q === null || q === void 0 ? void 0 : q.configProp) === null || _a === void 0 ? void 0 : _a.key) || '';
qKeyClean = qKey.replace('.', '__');
choicesObj = {};
if (q.options) {
q.options.forEach(function (opt) {
choicesObj[opt.title] = opt;
});
}
answer = providedAnswers[qKey.replace('__', '.')];
if (!answer) return [3 /*break*/, 1];
value = void 0;
if (typeof answer === 'string' && q.type === 'list') {
value = (_b = choicesObj[answer]) === null || _b === void 0 ? void 0 : _b.value;
}
else {
value = answer;
}
results[qKey] = {
answer: answer,
configProp: q.configProp,
value: value,
};
return [3 /*break*/, 3];
case 1:
inqQuestion = {
name: qKeyClean,
type: q.type,
message: q.title,
choices: Object.keys(choicesObj),
};
return [4 /*yield*/, (0, core_1.inquirerPrompt)(inqQuestion)];
case 2:
result = _f.sent();
val = q.type === 'list' ? (_c = choicesObj[result[qKeyClean]]) === null || _c === void 0 ? void 0 : _c.value : result[qKeyClean];
results[qKey] = {
answer: result[qKeyClean],
configProp: q.configProp,
value: val,
};
_f.label = 3;
case 3:
if (!((_d = choicesObj[results[qKey].answer]) === null || _d === void 0 ? void 0 : _d.bootstrapQuestions)) return [3 /*break*/, 5];
// eslint-disable-next-line no-await-in-loop
return [4 /*yield*/, interactiveQuestion(results, choicesObj[results[qKey].answer].bootstrapQuestions, providedAnswers)];
case 4:
// eslint-disable-next-line no-await-in-loop
_f.sent();
_f.label = 5;
case 5: return [2 /*return*/];
}
});
};
i = 0;
_e.label = 1;
case 1:
if (!(i < bootstrapQuestions.length)) return [3 /*break*/, 4];
return [5 /*yield**/, _loop_1(i)];
case 2:
_e.sent();
_e.label = 3;
case 3:
i++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
}); };
exports.default = Question;
//# sourceMappingURL=bootstrapQuestions.js.map
;