templates-mo
Version:
Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting
96 lines (95 loc) • 3.95 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const is = __importStar(require("is"));
const logger_1 = __importDefault(require("../utilities/logger"));
class Prompt {
constructor(prompt, prompter) {
var _a;
logger_1.default.prompt.info('Prompt %O', prompt);
this.aliases = prompt.aliases || [];
this.tpsType = prompt.tpsType || 'package';
if (!['package', 'data'].includes(this.tpsType)) {
throw new Error("Invalid prop type in prompts. tpsType must be either 'package' or 'data'");
}
// inquire props
this.name = prompt.name;
this.type = prompt.type;
this.message = prompt.message;
this.description = prompt.description;
// let defaultValue;
// const isPrompterDefaultIndex = ['list', 'rawlist', 'expand'].includes(
// prompt.type
// );
// if (isPrompterDefaultIndex) {
// let defaultFromChoices = this.choices[prompt.default];
// if (is.func(this.filter)) {
// defaultFromChoices = this.filter(defaultFromChoices);
// }
// defaultValue = defaultFromChoices;
// } else {
// defaultValue = prompt.default;
// }
this.hidden = (_a = prompt.hidden) !== null && _a !== void 0 ? _a : false;
this.choices = prompt.choices;
this.pageSize = prompt.pageSize;
this.prefix = prompt.prefix;
this.suffix = prompt.suffix;
this.filter = prompt.filter;
this.transformer = prompt.transformer;
this.default = this.wrapFunc(prompt.default, (fn, inquirerAnswers) => {
return fn(Object.assign(Object.assign({}, prompter.answers), inquirerAnswers));
});
this.when = this.wrapFunc(prompt.when, (fn, inquirerAnswers) => {
return fn(Object.assign(Object.assign({}, prompter.answers), inquirerAnswers));
});
this.validate = this.wrapFunc(prompt.validate, (fn, input, inquirerAnswers) => {
return fn(input, Object.assign(Object.assign({}, prompter.answers), inquirerAnswers));
});
}
wrapFunc(d, wrapper) {
return d instanceof Function ? (...args) => wrapper(d, ...args) : d;
}
isData() {
return this.tpsType === 'data';
}
isPkg() {
return !this.isData();
}
getDefaultValue(answers) {
return this.default instanceof Function
? this.default(answers)
: this.default;
}
answerWith(answers) {
const canAnswerBy = [this.name, ...this.aliases];
const didAnswerBy = canAnswerBy.find((by) => is.defined(answers[by]));
return !didAnswerBy ? undefined : answers[didAnswerBy];
}
}
exports.default = Prompt;