153 lines
5.6 KiB
JavaScript
153 lines
5.6 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
||
|
|
||
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
||
|
|
||
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||
|
|
||
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
||
|
|
||
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
||
|
|
||
|
const prompts = require('./prompts');
|
||
|
|
||
|
const passOn = ['suggest', 'format', 'onState', 'validate', 'onRender'];
|
||
|
|
||
|
const noop = () => {};
|
||
|
/**
|
||
|
* Prompt for a series of questions
|
||
|
* @param {Array|Object} questions Single question object or Array of question objects
|
||
|
* @param {Function} [onSubmit] Callback function called on prompt submit
|
||
|
* @param {Function} [onCancel] Callback function called on cancel/abort
|
||
|
* @returns {Object} Object with values from user input
|
||
|
*/
|
||
|
|
||
|
|
||
|
function prompt() {
|
||
|
return _prompt.apply(this, arguments);
|
||
|
}
|
||
|
|
||
|
function _prompt() {
|
||
|
_prompt = _asyncToGenerator(function* (questions = [], {
|
||
|
onSubmit = noop,
|
||
|
onCancel = noop
|
||
|
} = {}) {
|
||
|
const answers = {};
|
||
|
const override = prompt._override || {};
|
||
|
questions = [].concat(questions);
|
||
|
let answer, question, quit, name, type;
|
||
|
|
||
|
const getFormattedAnswer =
|
||
|
/*#__PURE__*/
|
||
|
function () {
|
||
|
var _ref = _asyncToGenerator(function* (question, answer, skipValidation = false) {
|
||
|
if (!skipValidation && question.validate && question.validate(answer) !== true) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
return question.format ? yield question.format(answer, answers) : answer;
|
||
|
});
|
||
|
|
||
|
return function getFormattedAnswer(_x, _x2) {
|
||
|
return _ref.apply(this, arguments);
|
||
|
};
|
||
|
}();
|
||
|
|
||
|
var _iteratorNormalCompletion = true;
|
||
|
var _didIteratorError = false;
|
||
|
var _iteratorError = undefined;
|
||
|
|
||
|
try {
|
||
|
for (var _iterator = questions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||
|
question = _step.value;
|
||
|
var _question = question;
|
||
|
name = _question.name;
|
||
|
type = _question.type;
|
||
|
|
||
|
// if property is a function, invoke it unless it's a special function
|
||
|
for (let key in question) {
|
||
|
if (passOn.includes(key)) continue;
|
||
|
let value = question[key];
|
||
|
question[key] = typeof value === 'function' ? yield value(answer, _objectSpread({}, answers), question) : value;
|
||
|
}
|
||
|
|
||
|
if (typeof question.message !== 'string') {
|
||
|
throw new Error('prompt message is required');
|
||
|
} // update vars in case they changed
|
||
|
|
||
|
|
||
|
var _question2 = question;
|
||
|
name = _question2.name;
|
||
|
type = _question2.type;
|
||
|
// skip if type is a falsy value
|
||
|
if (!type) continue;
|
||
|
|
||
|
if (prompts[type] === void 0) {
|
||
|
throw new Error(`prompt type (${type}) is not defined`);
|
||
|
}
|
||
|
|
||
|
if (override[question.name] !== undefined) {
|
||
|
answer = yield getFormattedAnswer(question, override[question.name]);
|
||
|
|
||
|
if (answer !== undefined) {
|
||
|
answers[name] = answer;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// Get the injected answer if there is one or prompt the user
|
||
|
answer = prompt._injected ? getInjectedAnswer(prompt._injected) : yield prompts[type](question);
|
||
|
answers[name] = answer = yield getFormattedAnswer(question, answer, true);
|
||
|
quit = yield onSubmit(question, answer, answers);
|
||
|
} catch (err) {
|
||
|
quit = !(yield onCancel(question, answers));
|
||
|
}
|
||
|
|
||
|
if (quit) return answers;
|
||
|
}
|
||
|
} catch (err) {
|
||
|
_didIteratorError = true;
|
||
|
_iteratorError = err;
|
||
|
} finally {
|
||
|
try {
|
||
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
||
|
_iterator.return();
|
||
|
}
|
||
|
} finally {
|
||
|
if (_didIteratorError) {
|
||
|
throw _iteratorError;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return answers;
|
||
|
});
|
||
|
return _prompt.apply(this, arguments);
|
||
|
}
|
||
|
|
||
|
function getInjectedAnswer(injected) {
|
||
|
const answer = injected.shift();
|
||
|
|
||
|
if (answer instanceof Error) {
|
||
|
throw answer;
|
||
|
}
|
||
|
|
||
|
return answer;
|
||
|
}
|
||
|
|
||
|
function inject(answers) {
|
||
|
prompt._injected = (prompt._injected || []).concat(answers);
|
||
|
}
|
||
|
|
||
|
function override(answers) {
|
||
|
prompt._override = Object.assign({}, answers);
|
||
|
}
|
||
|
|
||
|
module.exports = Object.assign(prompt, {
|
||
|
prompt,
|
||
|
prompts,
|
||
|
inject,
|
||
|
override
|
||
|
});
|