@eljs/utils
Version:
Collection of nodejs utility.
106 lines (104 loc) • 3.38 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/cli/prompt.ts
var prompt_exports = {};
__export(prompt_exports, {
confirm: () => confirm,
loopPrompt: () => loopPrompt,
prompt: () => prompt,
select: () => select
});
module.exports = __toCommonJS(prompt_exports);
var import_type = require("../type");
var import_prompts = __toESM(require("prompts"));
function confirm(message, preferNo, onCancel) {
onCancel = onCancel || (() => process.exit(1));
return (0, import_prompts.default)(
{
type: "confirm",
message,
name: "confirm",
initial: !preferNo
},
{
onCancel
}
).then((answers) => {
return answers.confirm;
});
}
function select(message, choices, initial) {
const questions = [
{
name: "name",
message,
type: "select",
choices,
initial
}
];
return (0, import_prompts.default)(questions).then((answers) => {
return answers.name;
});
}
function prompt(questions, initials) {
questions = questions.map((question) => {
const copied = Object.assign({}, question);
const name = copied.name || "";
copied.type = copied.type || (0, import_type.isNull)(copied.type) ? copied.type : "text";
if (initials == null ? void 0 : initials[name]) {
copied.initial = initials[name];
}
return copied;
});
return (0, import_prompts.default)(questions);
}
function loopPrompt(questions, initials) {
return prompt(questions, initials).then((answers) => {
console.log();
console.log("The information you entered is as follows:");
console.log(JSON.stringify(answers, null, 2));
console.log();
return confirm(
"If the information is correct, press Y to confirm; if you need to re-enter, press N"
).then((isOK) => {
if (isOK) {
return answers;
} else {
return loopPrompt(questions, answers);
}
});
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
confirm,
loopPrompt,
prompt,
select
});