@sap-ux/inquirer-common 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- import type { PromptSeverityMessage, YUIQuestion } from '../types';
1
+ import type { Answers, Question, Validator } from 'inquirer';
2
+ import type { CommonPromptOptions, PromptDefaultValue, PromptSeverityMessage, YUIQuestion } from '../types';
2
3
  /**
3
4
  * Extends an additionalMessages function.
4
5
  *
@@ -7,4 +8,37 @@ import type { PromptSeverityMessage, YUIQuestion } from '../types';
7
8
  * @returns the extended additional messages function
8
9
  */
9
10
  export declare function extendAdditionalMessages(question: YUIQuestion, addMsgFunc: PromptSeverityMessage): PromptSeverityMessage;
11
+ /**
12
+ * Extends a validate function. The extended function will be called first.
13
+ *
14
+ * @param question - the question to which the validate function will be applied
15
+ * @param validateFunc - the validate function which will be applied to the question
16
+ * @returns the extended validate function
17
+ */
18
+ export declare function extendValidate<T extends Answers = Answers>(question: Question, validateFunc: NonNullable<Validator<T>>): NonNullable<Validator<T>>;
19
+ /**
20
+ * Extend the existing prompt property function with the one specified in prompt options or add as new.
21
+ *
22
+ * @param question - the question to which the extending function will be applied
23
+ * @param promptOption - prompt options, containing extending functions
24
+ * @param funcName - the question property (function) name to extend
25
+ * @returns the extended question
26
+ */
27
+ export declare function applyExtensionFunction<T extends Answers = Answers>(question: YUIQuestion, promptOption: CommonPromptOptions<T>, funcName: 'validate' | 'additionalMessages'): YUIQuestion;
28
+ /**
29
+ * Adds additional conditions to the provided questions.
30
+ *
31
+ * @param questions the questions to which the condition will be added
32
+ * @param condition function which returns true or false
33
+ * @returns the passed questions reference
34
+ */
35
+ export declare function withCondition(questions: Question[], condition: (answers: Answers) => boolean): Question[];
36
+ /**
37
+ * Updates questions with extensions for specific properties. Only `validate`, `default` and `additionalMessages` are currently supported.
38
+ *
39
+ * @param questions - array of prompts to be extended
40
+ * @param promptOptions - the prompt options possibly containing function extensions
41
+ * @returns - the extended questions
42
+ */
43
+ export declare function extendWithOptions<T extends YUIQuestion = YUIQuestion>(questions: T[], promptOptions: Record<string, CommonPromptOptions & PromptDefaultValue<string | boolean>>): YUIQuestion[];
10
44
  //# sourceMappingURL=helpers.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extendAdditionalMessages = void 0;
3
+ exports.extendWithOptions = exports.withCondition = exports.applyExtensionFunction = exports.extendValidate = exports.extendAdditionalMessages = void 0;
4
4
  /**
5
5
  * Extends an additionalMessages function.
6
6
  *
@@ -20,4 +20,106 @@ function extendAdditionalMessages(question, addMsgFunc) {
20
20
  };
21
21
  }
22
22
  exports.extendAdditionalMessages = extendAdditionalMessages;
23
+ /**
24
+ * Extends a validate function. The extended function will be called first.
25
+ *
26
+ * @param question - the question to which the validate function will be applied
27
+ * @param validateFunc - the validate function which will be applied to the question
28
+ * @returns the extended validate function
29
+ */
30
+ function extendValidate(question, validateFunc) {
31
+ const validate = question.validate;
32
+ return (value, previousAnswers) => {
33
+ // Cant use ReturnType<Validator<T>>
34
+ const extVal = validateFunc === null || validateFunc === void 0 ? void 0 : validateFunc(value, previousAnswers);
35
+ if (extVal !== true) {
36
+ return extVal;
37
+ }
38
+ return typeof validate === 'function' ? validate(value, previousAnswers) : true;
39
+ };
40
+ }
41
+ exports.extendValidate = extendValidate;
42
+ /**
43
+ * Extend the existing prompt property function with the one specified in prompt options or add as new.
44
+ *
45
+ * @param question - the question to which the extending function will be applied
46
+ * @param promptOption - prompt options, containing extending functions
47
+ * @param funcName - the question property (function) name to extend
48
+ * @returns the extended question
49
+ */
50
+ function applyExtensionFunction(question, promptOption, funcName) {
51
+ let extendedFunc;
52
+ if (funcName === 'validate' && promptOption.validate) {
53
+ extendedFunc = extendValidate(question, promptOption.validate);
54
+ }
55
+ if (funcName === 'additionalMessages' && promptOption.additionalMessages) {
56
+ extendedFunc = extendAdditionalMessages(question, promptOption.additionalMessages);
57
+ }
58
+ question = Object.assign(question, { [funcName]: extendedFunc });
59
+ return question;
60
+ }
61
+ exports.applyExtensionFunction = applyExtensionFunction;
62
+ /**
63
+ * Adds additional conditions to the provided questions.
64
+ *
65
+ * @param questions the questions to which the condition will be added
66
+ * @param condition function which returns true or false
67
+ * @returns the passed questions reference
68
+ */
69
+ function withCondition(questions, condition) {
70
+ questions.forEach((question) => {
71
+ if (question.when !== undefined) {
72
+ if (typeof question.when === 'function') {
73
+ const when = question.when;
74
+ question.when = (answers) => {
75
+ if (condition(answers)) {
76
+ return when(answers);
77
+ }
78
+ else {
79
+ return false;
80
+ }
81
+ };
82
+ }
83
+ else {
84
+ const whenValue = question.when;
85
+ question.when = (answers) => {
86
+ return condition(answers) && whenValue;
87
+ };
88
+ }
89
+ }
90
+ else {
91
+ question.when = condition;
92
+ }
93
+ });
94
+ return questions;
95
+ }
96
+ exports.withCondition = withCondition;
97
+ /**
98
+ * Updates questions with extensions for specific properties. Only `validate`, `default` and `additionalMessages` are currently supported.
99
+ *
100
+ * @param questions - array of prompts to be extended
101
+ * @param promptOptions - the prompt options possibly containing function extensions
102
+ * @returns - the extended questions
103
+ */
104
+ function extendWithOptions(questions, promptOptions) {
105
+ questions.forEach((question) => {
106
+ const promptOptKey = question.name;
107
+ const promptOpt = promptOptions[question.name];
108
+ if (promptOpt) {
109
+ const propsToExtend = Object.keys(promptOpt);
110
+ for (const extProp of propsToExtend) {
111
+ if (extProp === 'validate' || extProp === 'additionalMessages') {
112
+ question = applyExtensionFunction(question, promptOpt, extProp);
113
+ }
114
+ // Provided defaults will override built in defaults
115
+ const defaultOverride = promptOptions[promptOptKey].default;
116
+ if (defaultOverride) {
117
+ question.default = defaultOverride;
118
+ }
119
+ }
120
+ }
121
+ });
122
+ return questions;
123
+ }
124
+ exports.extendWithOptions = extendWithOptions;
23
125
  //# sourceMappingURL=helpers.js.map
@@ -1,5 +1,5 @@
1
- import type { ListChoiceOptions } from 'inquirer';
2
1
  import { type UI5Version } from '@sap-ux/ui5-info';
2
+ import type { ListChoiceOptions } from 'inquirer';
3
3
  import type { UI5VersionChoice } from '../types';
4
4
  import { Separator } from './separator';
5
5
  /**
@@ -24,11 +24,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.getDefaultUI5VersionChoice = exports.ui5VersionsGrouped = exports.searchChoices = exports.getUI5ThemesChoices = void 0;
27
+ const ui5_info_1 = require("@sap-ux/ui5-info");
27
28
  const fuzzy = __importStar(require("fuzzy"));
29
+ const semver_1 = require("semver");
28
30
  const i18n_1 = require("../i18n");
29
- const ui5_info_1 = require("@sap-ux/ui5-info");
30
31
  const separator_1 = require("./separator");
31
- const semver_1 = require("semver");
32
32
  /**
33
33
  * Get the UI5 themes as prompt choices applicable for the specified UI5 version.
34
34
  *
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type IMessageSeverity } from '@sap-devx/yeoman-ui-types';
2
- import type { Answers, ConfirmQuestion as BaseConfirmQuestion, InputQuestion as BaseInputQuestion, ListQuestion as BaseListQuestion, CheckboxQuestion as BaseCheckBoxQuestion, ListChoiceOptions, PromptFunction, PromptModule, Question } from 'inquirer';
2
+ import type { Answers, ConfirmQuestion as BaseConfirmQuestion, InputQuestion as BaseInputQuestion, ListQuestion as BaseListQuestion, CheckboxQuestion as BaseCheckBoxQuestion, ListChoiceOptions, PromptFunction, PromptModule, Question, Validator, AsyncDynamicQuestionProperty } from 'inquirer';
3
3
  export interface UI5VersionChoice extends ListChoiceOptions {
4
4
  /**
5
5
  * UI5 semantic version
@@ -39,7 +39,6 @@ export interface GuiOptions {
39
39
  breadcrumb?: boolean | string;
40
40
  }
41
41
  export type PromptSeverityMessage = (input?: unknown, previousAnswers?: Answers) => IMessageSeverity | undefined;
42
- export type validate<T> = (input: any, answers?: T) => boolean | string | Promise<boolean | string>;
43
42
  export type YUIQuestion<A extends Answers = Answers> = Question<A> & {
44
43
  name: string;
45
44
  guiOptions?: GuiOptions;
@@ -67,4 +66,15 @@ export interface CheckBoxQuestion<A extends Answers = Answers> extends BaseCheck
67
66
  guiOptions?: YUIQuestion['guiOptions'];
68
67
  additionalMessages?: YUIQuestion['additionalMessages'];
69
68
  }
69
+ /**
70
+ * Defines prompt/question default values and/or whether or not they should be shown.
71
+ */
72
+ export type CommonPromptOptions<T extends Answers = Answers> = {
73
+ hide?: boolean;
74
+ validate?: Validator<T>;
75
+ additionalMessages?: PromptSeverityMessage;
76
+ };
77
+ export type PromptDefaultValue<T> = {
78
+ default?: AsyncDynamicQuestionProperty<T>;
79
+ };
70
80
  //# sourceMappingURL=types.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sap-ux/inquirer-common",
3
3
  "description": "Commonly used shared functionality and types to support inquirer modules.",
4
- "version": "0.2.6",
4
+ "version": "0.2.8",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/SAP/open-ux-tools.git",
@@ -24,7 +24,7 @@
24
24
  "chalk": "4.1.2",
25
25
  "figures": "3.2.0",
26
26
  "semver": "7.5.4",
27
- "@sap-ux/ui5-info": "0.4.0"
27
+ "@sap-ux/ui5-info": "0.5.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@sap-devx/yeoman-ui-types": "1.14.4",