react-native-integrate
Version:
Automate integration of additional code into React Native projects
58 lines (57 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runPrompt = runPrompt;
exports.getValidate = getValidate;
const prompter_1 = require("../prompter");
const variables_1 = require("../variables");
const getPackageUpgradeInput_1 = require("./getPackageUpgradeInput");
const packageUpgradeConfig_1 = require("./packageUpgradeConfig");
async function runPrompt(prompt, packageName) {
if ((0, getPackageUpgradeInput_1.handlePackageUpgradeInput)(packageName, prompt.name))
return;
const lastInputValue = (0, getPackageUpgradeInput_1.getLastInputValue)(packageName, prompt.name);
prompt = (0, variables_1.transformTextInObject)(prompt);
let inputValue;
switch (prompt.type) {
case 'boolean':
if (lastInputValue != null)
prompt.initialValue = lastInputValue;
inputValue = await (0, prompter_1.confirm)(prompt.text, prompt);
break;
case 'multiselect':
if (lastInputValue != null)
prompt.initialValues = lastInputValue;
inputValue = await (0, prompter_1.multiselect)(prompt.text, prompt);
break;
case 'select':
if (lastInputValue != null)
prompt.initialValue = lastInputValue;
inputValue = await (0, prompter_1.select)(prompt.text, prompt);
break;
default:
if (lastInputValue != null)
prompt.initialValue = lastInputValue;
inputValue = await (0, prompter_1.text)(prompt.text, {
placeholder: prompt.placeholder,
defaultValue: prompt.defaultValue,
initialValue: prompt.initialValue,
validate: getValidate(prompt.validate),
});
break;
}
variables_1.variables.set(prompt.name, inputValue);
(0, packageUpgradeConfig_1.addPackageUpgradeInput)(packageName, prompt.name, inputValue);
}
function getValidate(validate) {
if (!validate)
return undefined;
if (!Array.isArray(validate))
validate = [validate];
const _validate = validate;
return (value) => {
for (const { flags, regex, message } of _validate) {
if (!new RegExp(regex, flags).test(value))
return message;
}
};
}