UNPKG

rush-init-project-plugin

Version:

Rush plugin for initialize project in monorepo

364 lines 15.3 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseFieldComponent = void 0; var blessed_1 = require("blessed"); /** * This is a basic Field component based on inquirer * Any new component based on inquirer should extend this class */ var BaseFieldComponent = /** @class */ (function () { function BaseFieldComponent(form, prompt, option, hookForPrompt) { /** * control the active of the this field, if this field is active, its value is active and it's visible */ this.isActived = true; /** * validation of the field */ this.isValidate = true; /** * all elements involved in field */ this.elements = []; this.form = form; this.prompt = prompt; this.option = option; this._hookForPrompt = hookForPrompt; this.label = (0, blessed_1.box)({ tags: true, parent: this.form, height: 1, content: this.prompt.name, alwaysScroll: true, shrink: true }); } /** * validate result based on blessed widget implemented by every component */ BaseFieldComponent.prototype.validateResult = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; /** * set default value on the blessed widget implemented by every component */ BaseFieldComponent.prototype.setDefaultValue = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; /** * set content on the blessed widget implemented by every component */ BaseFieldComponent.prototype.setMessage = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; /** * used for blessed to focus on field implemented by every component */ BaseFieldComponent.prototype.focus = function () { }; /** * used for blessed to focus on next field */ BaseFieldComponent.prototype.focusNext = function () { this.form.focusNext(); }; /** * get really field object */ BaseFieldComponent.prototype.getFieldComponent = function () { return; }; BaseFieldComponent.prototype.reset = function () { }; BaseFieldComponent.prototype.render = function () { this.form.screen.render(); }; BaseFieldComponent.prototype.invokeHooks = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this._hookForPrompt) return [3 /*break*/, 2]; return [4 /*yield*/, this._hookForPrompt.call(this.prompt, this.form.submission)]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/]; } }); }); }; /** * get current total height of all elements and it's top */ BaseFieldComponent.prototype.getLayout = function () { var _this = this; return this.elements.reduce(function (prev, cur) { var _a, _b, _c; var height = 0; if (_this.isActived) { if (cur instanceof Array) { height = Number((_b = (_a = cur[0]) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0); } else { height = Number((_c = cur === null || cur === void 0 ? void 0 : cur.height) !== null && _c !== void 0 ? _c : 0); } } return { height: Number(prev.height) + height, top: prev.top }; }, { height: 0, top: this.option.top }); }; /** * update and top of all elements */ BaseFieldComponent.prototype.updateLayout = function (updateOption) { var _this = this; var _a; this.option = __assign(__assign({}, this.option), updateOption); var top = (_a = Number(this.option.top)) !== null && _a !== void 0 ? _a : 0; var totalHeight = 0; this.elements.forEach(function (ele, index) { var _a, _b, _c; if (ele instanceof Array) { ele.forEach(function (item) { item.top = top + totalHeight; }); if (_this.isActived) { totalHeight += Number((_b = (_a = ele[0]) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0); } } else { ele.top = top + totalHeight; if (_this.isActived) { totalHeight += Number((_c = ele === null || ele === void 0 ? void 0 : ele.height) !== null && _c !== void 0 ? _c : 0); } } }); return { top: top, height: totalHeight }; }; /** * The default value of the question. * implement of default param in inquirer */ BaseFieldComponent.prototype.default = function () { var _a, _b; return __awaiter(this, void 0, void 0, function () { var defaultValue, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: defaultValue = ''; _c = typeof this.prompt.default; switch (_c) { case 'function': return [3 /*break*/, 1]; case 'boolean': return [3 /*break*/, 3]; case 'number': return [3 /*break*/, 4]; } return [3 /*break*/, 5]; case 1: return [4 /*yield*/, this.prompt.default(this.form.submission)]; case 2: defaultValue = (_a = (_d.sent())) !== null && _a !== void 0 ? _a : ''; return [3 /*break*/, 6]; case 3: defaultValue = this.prompt.default; return [3 /*break*/, 6]; case 4: defaultValue = this.prompt.default; return [3 /*break*/, 6]; case 5: defaultValue = (_b = this.prompt.default) !== null && _b !== void 0 ? _b : ''; return [3 /*break*/, 6]; case 6: return [2 /*return*/, defaultValue]; } }); }); }; /** * The message value of the question. * implement of message param in inquirer */ BaseFieldComponent.prototype.message = function () { var _a, _b; return __awaiter(this, void 0, void 0, function () { var message; return __generator(this, function (_c) { switch (_c.label) { case 0: message = ''; if (!(typeof this.prompt.message === 'function')) return [3 /*break*/, 2]; return [4 /*yield*/, this.prompt.message(this.form.submission)]; case 1: message = (_a = (_c.sent())) !== null && _a !== void 0 ? _a : ''; return [3 /*break*/, 3]; case 2: if (typeof this.prompt.message === 'string') { message = (_b = this.prompt.message) !== null && _b !== void 0 ? _b : ''; } _c.label = 3; case 3: return [2 /*return*/, message]; } }); }); }; /** * Validates the integrity of the answer. * implement of validate param in inquirer * * @param input * The answer provided by the user. * * @returns * If this field is inactive, it will always return true * Either a value indicating whether the answer is valid or a `string` which describes the error. */ BaseFieldComponent.prototype.validate = function (value) { return __awaiter(this, void 0, void 0, function () { var validate; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isActived) { return [2 /*return*/, true]; } validate = this.prompt.validate; if (!(typeof validate === 'function')) return [3 /*break*/, 2]; this.form.submit(); return [4 /*yield*/, validate(value, this.form.submission)]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [2 /*return*/, true]; } }); }); }; /** * A value indicating whether the question should be prompted. * implement of when param in inquirer */ BaseFieldComponent.prototype.when = function () { return __awaiter(this, void 0, void 0, function () { var currentActive, _a; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: currentActive = true; _a = typeof this.prompt.when; switch (_a) { case 'function': return [3 /*break*/, 1]; } return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.prompt.when(this.form.submission)]; case 2: currentActive = _b.sent(); return [3 /*break*/, 4]; case 3: currentActive = true; return [3 /*break*/, 4]; case 4: if (this.isActived === currentActive) { return [2 /*return*/]; } this.isActived = currentActive; this.elements.forEach(function (ele, index) { if (ele instanceof Array) { ele.forEach(function (item) { if (_this.isActived) { item.show(); } else { item.hide(); } }); } else { if (_this.isActived) { ele.show(); } else { ele.hide(); } } }); return [2 /*return*/]; } }); }); }; /** * init state include default and message config */ BaseFieldComponent.prototype.initState = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.setMessage()]; case 1: _a.sent(); return [4 /*yield*/, this.setDefaultValue()]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; return BaseFieldComponent; }()); exports.BaseFieldComponent = BaseFieldComponent; //# sourceMappingURL=BaseFieldComponent.js.map