@sap-ux/ui5-application-inquirer 0.3.26 → 0.4.0

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.
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ const isNil_1 = __importDefault(require("lodash/isNil"));
19
19
  const prompts_1 = require("./prompts");
20
20
  const types_1 = require("./types");
21
21
  Object.defineProperty(exports, "promptNames", { enumerable: true, get: function () { return types_1.promptNames; } });
22
+ const i18n_1 = require("./i18n");
22
23
  /**
23
24
  * Get the inquirer prompts for ui5 library inquirer.
24
25
  *
@@ -30,6 +31,8 @@ Object.defineProperty(exports, "promptNames", { enumerable: true, get: function
30
31
  function getPrompts(promptOptions, capCdsInfo, isYUI = false) {
31
32
  var _a, _b;
32
33
  return __awaiter(this, void 0, void 0, function* () {
34
+ // prompt texts must be loaded before the prompts are created, wait for the i18n bundle to be initialized
35
+ yield (0, i18n_1.initI18nUi5AppInquirer)();
33
36
  const filterOptions = {
34
37
  useCache: true,
35
38
  includeMaintained: true,
@@ -61,36 +64,55 @@ function prompt(adapter, promptOptions, capCdsInfo, isYUI = false) {
61
64
  const answers = yield adapter.prompt(ui5AppPrompts);
62
65
  // Apply default values to prompts in case they have not been executed
63
66
  if (promptOptions) {
64
- Object.assign(answers, yield getDefaults(answers, promptOptions));
67
+ const defaultAnswers = applyPromptOptionDefaults(answers, ui5AppPrompts, promptOptions, capCdsInfo);
68
+ Object.assign(answers, defaultAnswers);
65
69
  }
66
70
  return answers;
67
71
  });
68
72
  }
69
73
  exports.prompt = prompt;
70
74
  /**
71
- * Return the default values for prompts that did not provide an answer.
72
- * This can be derived from user input, or a fallback default in case an answer was not provided due to the prompt not having been executed.
75
+ * Apply prompt option default values to prompt answers in case they have not been executed by the user selections.
76
+ * This can occur where some prompts are hidden by advanced option selections.
73
77
  *
74
- * @param answers - the answers from previous prompting, which if present will be used instead of defaults
75
- * @param promptOptions - the prompt options
76
- * @returns answer values
78
+ * @param answers the answers from previous prompting, only answers without a value will be considered for defaulting
79
+ * @param ui5AppPrompts the prompts that were used to prompt the user, will be used to apply default values if not answered or no default provided
80
+ * @param promptOptions the prompt options which may provide default values or functions
81
+ * @param capCdsInfo will be passed as additional answer to default functions that depend on it to determine the default value
82
+ * @returns the default values for the unanswered prompts, based on the prompt options
77
83
  */
78
- function getDefaults(answers, promptOptions) {
84
+ function applyPromptOptionDefaults(answers, ui5AppPrompts, promptOptions, capCdsInfo) {
79
85
  const defaultAnswers = {};
80
86
  Object.entries(promptOptions).forEach(([key, promptOpt]) => {
87
+ var _a;
81
88
  const promptKey = key;
82
- // Do we have an answer, if not apply the default, either specified or fallback
83
- const defaultProperty = promptOpt.default;
84
- if ((0, isNil_1.default)(answers[promptKey]) && (defaultProperty !== null && defaultProperty !== void 0 ? defaultProperty : promptKey === types_1.promptNames.ui5Theme)) {
89
+ // Do we have an answer, if not apply the default
90
+ const defaultValueOrFunc = promptOpt.default;
91
+ // A prompt option for ui5Theme is not supported (as its dependant on the ui5Version) and is special cased
92
+ // `enableTypeScript` is dependent on the CdsUi5PluginInfo and is special cased
93
+ if ((0, isNil_1.default)(answers[promptKey]) &&
94
+ ((defaultValueOrFunc !== null && defaultValueOrFunc !== void 0 ? defaultValueOrFunc : promptKey === types_1.promptNames.ui5Theme) || promptOpt.advancedOption === true)) {
85
95
  let defaultValue;
86
- if (typeof defaultProperty === 'function') {
87
- defaultValue = defaultProperty(answers);
96
+ if (promptKey === types_1.promptNames.ui5Theme) {
97
+ defaultValue = (0, ui5_info_1.getDefaultUI5Theme)(answers.ui5Version);
88
98
  }
89
- else if (defaultProperty) {
90
- defaultValue = defaultProperty;
99
+ else if (promptKey === types_1.promptNames.enableTypeScript) {
100
+ // TypeScript default value is dependent on the CdsUi5PluginInfo
101
+ const enableTypeScriptOpts = promptOpt;
102
+ // If an enableTypeScript default function is provided, use it to determine the default value
103
+ // otherwise override with the provided default value
104
+ defaultValue =
105
+ typeof (enableTypeScriptOpts === null || enableTypeScriptOpts === void 0 ? void 0 : enableTypeScriptOpts.default) === 'function'
106
+ ? enableTypeScriptOpts.default(Object.assign(Object.assign({}, answers), { capCdsInfo }))
107
+ : defaultValueOrFunc;
91
108
  }
92
- else if (promptKey === types_1.promptNames.ui5Theme) {
93
- defaultValue = (0, ui5_info_1.getDefaultUI5Theme)(answers.ui5Version);
109
+ else if (defaultValueOrFunc !== undefined) {
110
+ defaultValue = getDefaultValue(answers, defaultValueOrFunc);
111
+ }
112
+ else if (promptOpt.advancedOption) {
113
+ // Apply the orginal default value if it was not answered
114
+ const originalDefault = (_a = ui5AppPrompts.find((prompt) => prompt.name === promptKey)) === null || _a === void 0 ? void 0 : _a.default;
115
+ defaultValue = getDefaultValue(answers, originalDefault);
94
116
  }
95
117
  Object.assign(defaultAnswers, {
96
118
  [promptKey]: defaultValue
@@ -99,4 +121,21 @@ function getDefaults(answers, promptOptions) {
99
121
  });
100
122
  return defaultAnswers;
101
123
  }
124
+ /**
125
+ * Get the default value for a prompt based on the provided default value or function.
126
+ * If a function is provided, it will be called with the current answers to determine the default value.
127
+ *
128
+ * @param answers the current answers provided to default functions
129
+ * @param promptDefault the default value or function to determine the default value
130
+ * @returns the default value for the prompt or undefined if no default value is provided
131
+ */
132
+ function getDefaultValue(answers, promptDefault) {
133
+ if (typeof promptDefault === 'function') {
134
+ return promptDefault(answers);
135
+ }
136
+ if (promptDefault !== undefined) {
137
+ return promptDefault;
138
+ }
139
+ return undefined;
140
+ }
102
141
  //# sourceMappingURL=index.js.map
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { CdsUi5PluginInfo } from '@sap-ux/cap-config-writer';
1
2
  import type { CommonPromptOptions, PromptDefaultValue, UI5VersionChoice, YUIQuestion } from '@sap-ux/inquirer-common';
2
3
  import type { AutocompleteQuestionOptions } from 'inquirer-autocomplete-prompt';
3
4
  export interface UI5ApplicationAnswers {
@@ -76,6 +77,19 @@ type UI5VersionPromptOptions = {
76
77
  */
77
78
  defaultChoice?: UI5VersionChoice;
78
79
  };
80
+ /**
81
+ * Options for the enable TypeScript prompt. This allows for a default value to be determined based on the answers provided
82
+ * and additonal runtime cds information if available. This effectively constains the prompt options for the enable TypeScript prompt
83
+ * to be a function that returns a boolean value since enable TypeScript prompt default is conditional.
84
+ */
85
+ type EnableTypeScriptPromptOptions = Omit<PromptDefaultValue<boolean>, 'default'> & {
86
+ /**
87
+ * Callback function to determine the default value for TypeScript
88
+ */
89
+ default?: (answers: UI5ApplicationAnswers & {
90
+ capCdsInfo?: CdsUi5PluginInfo;
91
+ }) => boolean;
92
+ };
79
93
  /**
80
94
  * These are boolean value prompt option keys
81
95
  */
@@ -85,7 +99,7 @@ type stringValuePrompts = stringValuePromptType[keyof stringValuePromptType];
85
99
  type booleanValuePromptType = Pick<typeof promptNames, booleanPromptKeys>;
86
100
  type booleanValuePrompts = booleanValuePromptType[keyof booleanValuePromptType];
87
101
  type DefaultValueInputPrompts = promptNames.name | promptNames.description | promptNames.namespace | promptNames.ui5Version | promptNames.targetFolder;
88
- type DefaultValueConfirmPrompts = promptNames.enableCodeAssist | promptNames.enableEslint | promptNames.skipAnnotations | promptNames.enableTypeScript;
102
+ type DefaultValueConfirmPrompts = promptNames.enableCodeAssist | promptNames.enableEslint | promptNames.skipAnnotations;
89
103
  /**
90
104
  * Defines prompt/question default values and/or whether or not they should be shown.
91
105
  */
@@ -111,7 +125,7 @@ type booleanValuePromptOtions = Record<booleanValuePrompts, {
111
125
  * @returns
112
126
  */
113
127
  validatorCallback?: (answer: boolean, promptName: string) => void;
114
- } & UI5ApplicationCommonPromptOptions> & Record<DefaultValueConfirmPrompts, PromptDefaultValue<boolean>>;
128
+ } & UI5ApplicationCommonPromptOptions> & Record<DefaultValueConfirmPrompts, PromptDefaultValue<boolean>> & Record<promptNames.enableTypeScript, EnableTypeScriptPromptOptions>;
115
129
  export type UI5ApplicationQuestion = YUIQuestion<UI5ApplicationAnswers> & Partial<Pick<AutocompleteQuestionOptions, 'source'>>;
116
130
  export type UI5ApplicationPromptOptions = Partial<stringValuePromptOptions & booleanValuePromptOtions>;
117
131
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sap-ux/ui5-application-inquirer",
3
3
  "description": "Prompts module that can prompt users for inputs required for UI5 application writing",
4
- "version": "0.3.26",
4
+ "version": "0.4.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/SAP/open-ux-tools.git",
@@ -35,7 +35,7 @@
35
35
  "@types/lodash": "4.14.202",
36
36
  "@types/semver": "7.5.4",
37
37
  "inquirer": "8.2.6",
38
- "@sap-ux/cap-config-writer": "0.3.3"
38
+ "@sap-ux/cap-config-writer": "0.3.4"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=18.x"