@starship-ci/cli
Version:
59 lines (58 loc) • 1.93 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Inquirerer = void 0;
const readline_1 = __importDefault(require("readline"));
class Inquirerer {
rl;
noTty;
constructor(noTty = false) {
this.noTty = noTty;
if (!noTty) {
this.rl = readline_1.default.createInterface({
input: process.stdin,
output: process.stdout
});
}
else {
this.rl = null;
}
}
// Method to prompt for missing parameters
async prompt(params, questions, usageText) {
const obj = { ...params };
if (usageText &&
Object.values(params).some((value) => value === undefined) &&
!this.noTty) {
console.log(usageText);
}
for (const question of questions) {
if (obj[question.name] === undefined) {
if (!this.noTty) {
if (this.rl) {
obj[question.name] = await new Promise((resolve) => {
this.rl.question(`Enter ${question.name}: `, resolve);
});
}
else {
throw new Error('No TTY available and a readline interface is missing.');
}
}
else {
// Optionally handle noTty cases, e.g., set defaults or throw errors
throw new Error(`Missing required parameter: ${question.name}`);
}
}
}
return obj;
}
// Method to cleanly close the readline interface
close() {
if (this.rl) {
this.rl.close();
}
}
}
exports.Inquirerer = Inquirerer;
;