@ivoryio/ivory-cli
Version:
51 lines (50 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.answerChildProcessQuestions = exports.amplifyAddAuth = void 0;
/* eslint-disable @typescript-eslint/restrict-template-expressions */
var child_process_1 = require("child_process");
exports.amplifyAddAuth = function () {
return new Promise(function (resolve, reject) {
var addProcess = child_process_1.spawn('amplify', ['add', 'auth']);
var ENTER = '\n';
var DOWN_ARROW = '\u001b\u005B\u0042';
var qas = [
{ question: 'Do you want to use the default authentication', answer: ENTER },
{ question: 'How do you want users to be able to sign in?', answer: DOWN_ARROW + ENTER },
{ question: 'Do you want to configure advanced settings?', answer: ENTER },
];
answerChildProcessQuestions({ childProcess: addProcess, qas: qas });
addProcess.stderr.on('data', function (data) {
reject("Auth integration failed: " + data);
});
addProcess.on('exit', function () {
resolve();
});
});
};
function answerChildProcessQuestions(_a) {
var _b;
var childProcess = _a.childProcess, qas = _a.qas;
var alreadyAnswered = qas.map(function () { return false; });
(_b = childProcess.stdout) === null || _b === void 0 ? void 0 : _b.on('data', function (data) {
qas.forEach(function (_a, index) {
var question = _a.question;
if (("" + data).indexOf(question) > 0) {
answer(index, "" + data);
}
});
});
function answer(index, message) {
if (!alreadyAnswered[index]) {
setTimeout(function () {
var _a;
(_a = childProcess.stdin) === null || _a === void 0 ? void 0 : _a.write(qas[index].answer);
alreadyAnswered[index] = true;
}, 100);
}
else {
console.info(message);
}
}
}
exports.answerChildProcessQuestions = answerChildProcessQuestions;