zcatalyst-cli 1.26.1 → 1.27.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.
Files changed (155) hide show
  1. package/README.md +1 -0
  2. package/lib/appsail-utils.d.ts +1 -1
  3. package/lib/appsail-utils.js +170 -129
  4. package/lib/authentication/credential.d.ts +2 -1
  5. package/lib/authentication/credential.js +20 -3
  6. package/lib/authentication/crypt.d.ts +7 -3
  7. package/lib/authentication/crypt.js +87 -16
  8. package/lib/authentication/index.js +1 -0
  9. package/lib/authentication/key-manager.d.ts +7 -0
  10. package/lib/authentication/key-manager.js +62 -0
  11. package/lib/authentication/login.js +20 -20
  12. package/lib/bin/catalyst.js +1 -15
  13. package/lib/client.d.ts +3 -1
  14. package/lib/client.js +16 -5
  15. package/lib/command_needs/rc.js +13 -3
  16. package/lib/commands/appsail/add.js +10 -0
  17. package/lib/commands/client/delete.js +18 -6
  18. package/lib/commands/client/setup.js +7 -0
  19. package/lib/commands/deploy/index.js +3 -3
  20. package/lib/commands/deploy/slate.js +65 -4
  21. package/lib/commands/functions/add.js +7 -0
  22. package/lib/commands/functions/delete.js +22 -4
  23. package/lib/commands/functions/execute.js +39 -71
  24. package/lib/commands/functions/setup.js +13 -0
  25. package/lib/commands/functions/shell.js +15 -1
  26. package/lib/commands/iac/import.js +2 -2
  27. package/lib/commands/init.js +34 -20
  28. package/lib/commands/login.js +1 -0
  29. package/lib/commands/pull.js +22 -1
  30. package/lib/commands/serve.js +2 -1
  31. package/lib/commands/slate/create.js +7 -4
  32. package/lib/commands/slate/link.js +11 -5
  33. package/lib/commands/slate/unlink.js +7 -0
  34. package/lib/commands/zest/generate/index.js +28 -35
  35. package/lib/deploy/features/appsail/index.js +2 -1
  36. package/lib/deploy/features/slate.d.ts +3 -1
  37. package/lib/deploy/features/slate.js +204 -5
  38. package/lib/docs/client-utils.toml +0 -5
  39. package/lib/docs/command_needs/rc.toml +9 -1
  40. package/lib/docs/commands/client/delete.toml +4 -0
  41. package/lib/docs/commands/deploy/slate.toml +3 -0
  42. package/lib/docs/commands/functions/delete.toml +10 -0
  43. package/lib/docs/commands/functions/execute.toml +1 -16
  44. package/lib/docs/commands/functions/setup.toml +3 -0
  45. package/lib/docs/commands/functions/shell.toml +8 -0
  46. package/lib/docs/commands/init.toml +3 -0
  47. package/lib/docs/commands/pull.toml +6 -0
  48. package/lib/docs/commands/slate/unlink.toml +6 -0
  49. package/lib/docs/execute/command-utils.toml +14 -0
  50. package/lib/docs/execute/execute-input-resolver.toml +37 -0
  51. package/lib/docs/init/features/project.toml +20 -0
  52. package/lib/docs/init/util/client.toml +4 -0
  53. package/lib/docs/prompt/index.toml +15 -0
  54. package/lib/docs/pull/features/apig.toml +6 -0
  55. package/lib/docs/pull/features/client.toml +20 -0
  56. package/lib/docs/pull/features/functions/index.toml +24 -0
  57. package/lib/endpoints/lib/slate.d.ts +14 -1
  58. package/lib/endpoints/lib/slate.js +35 -0
  59. package/lib/error/context-help.js +1 -1
  60. package/lib/error/index.js +9 -13
  61. package/lib/execute/command-utils.d.ts +5 -0
  62. package/lib/execute/command-utils.js +86 -0
  63. package/lib/execute/execute-input-resolver.d.ts +29 -0
  64. package/lib/execute/execute-input-resolver.js +207 -0
  65. package/lib/execute/index.d.ts +4 -1
  66. package/lib/execute/index.js +3 -2
  67. package/lib/fn-utils/lib/integ.js +22 -17
  68. package/lib/index.js +1 -0
  69. package/lib/init/dependencies/package-json.js +11 -7
  70. package/lib/init/features/appsail/index.js +75 -26
  71. package/lib/init/features/appsail/prompt-fields.d.ts +43 -0
  72. package/lib/init/features/appsail/prompt-fields.js +55 -0
  73. package/lib/init/features/appsail/utils/custom-runtime.d.ts +3 -0
  74. package/lib/init/features/appsail/utils/custom-runtime.js +161 -0
  75. package/lib/init/features/appsail/{utils.d.ts → utils/index.d.ts} +2 -2
  76. package/lib/init/features/appsail/utils/index.js +170 -0
  77. package/lib/init/features/client/index.js +4 -3
  78. package/lib/init/features/client/initializers/angular.js +3 -2
  79. package/lib/init/features/client/initializers/react.js +4 -3
  80. package/lib/init/features/client/prompt-fields.d.ts +37 -0
  81. package/lib/init/features/client/prompt-fields.js +48 -0
  82. package/lib/init/features/functions/index.js +37 -8
  83. package/lib/init/features/functions/languages/java.js +42 -7
  84. package/lib/init/features/functions/languages/node.js +42 -10
  85. package/lib/init/features/functions/languages/python.js +42 -7
  86. package/lib/init/features/functions/prompt-fields.d.ts +25 -0
  87. package/lib/init/features/functions/prompt-fields.js +34 -0
  88. package/lib/init/features/project.js +41 -6
  89. package/lib/init/features/slate/index.js +2 -0
  90. package/lib/init/features/slate/prompt-fields.d.ts +31 -0
  91. package/lib/init/features/slate/prompt-fields.js +41 -0
  92. package/lib/init/index.js +2 -1
  93. package/lib/init/util/client.js +11 -8
  94. package/lib/init/util/common.d.ts +2 -0
  95. package/lib/init/util/common.js +33 -3
  96. package/lib/init/util/prompt-fields.d.ts +7 -0
  97. package/lib/init/util/prompt-fields.js +13 -0
  98. package/lib/internal/command.d.ts +3 -0
  99. package/lib/internal/command.js +26 -3
  100. package/lib/migration/global/1.27.0.d.ts +2 -0
  101. package/lib/migration/global/1.27.0.js +39 -0
  102. package/lib/prompt/choice.d.ts +2 -0
  103. package/lib/prompt/choice.js +24 -0
  104. package/lib/prompt/cli-option-key.d.ts +1 -0
  105. package/lib/prompt/cli-option-key.js +10 -0
  106. package/lib/prompt/index.d.ts +6 -22
  107. package/lib/prompt/index.js +264 -67
  108. package/lib/prompt/overwrite-prompt.d.ts +11 -0
  109. package/lib/prompt/overwrite-prompt.js +16 -0
  110. package/lib/prompt/question-factory.d.ts +34 -0
  111. package/lib/prompt/question-factory.js +41 -0
  112. package/lib/prompt/register-fields.d.ts +24 -0
  113. package/lib/prompt/register-fields.js +37 -0
  114. package/lib/pull/features/apig.js +57 -6
  115. package/lib/pull/features/client.js +107 -24
  116. package/lib/pull/features/functions/index.js +100 -4
  117. package/lib/pull/index.js +1 -1
  118. package/lib/pull/utils.d.ts +13 -0
  119. package/lib/pull/utils.js +20 -0
  120. package/lib/repl-server.d.ts +4 -0
  121. package/lib/repl-server.js +9 -0
  122. package/lib/serve/features/appsail.js +2 -45
  123. package/lib/serve/server/lib/master/index.js +3 -3
  124. package/lib/serve/server/lib/slate/static-server.js +2 -2
  125. package/lib/serve/server/lib/web_client/server.js +2 -2
  126. package/lib/shell/format-functions.d.ts +4 -0
  127. package/lib/shell/format-functions.js +59 -0
  128. package/lib/shell/index.js +18 -0
  129. package/lib/util_modules/config/lib/appsail.d.ts +3 -2
  130. package/lib/util_modules/config/lib/appsail.js +21 -5
  131. package/lib/util_modules/config/lib/slate.d.ts +19 -0
  132. package/lib/util_modules/config-store.d.ts +1 -0
  133. package/lib/util_modules/config-store.js +29 -10
  134. package/lib/util_modules/constants/index.d.ts +0 -1
  135. package/lib/util_modules/constants/index.js +1 -3
  136. package/lib/util_modules/constants/lib/urls.d.ts +2 -0
  137. package/lib/util_modules/constants/lib/urls.js +4 -0
  138. package/lib/util_modules/dc.d.ts +6 -1
  139. package/lib/util_modules/dc.js +21 -8
  140. package/lib/util_modules/env.d.ts +3 -0
  141. package/lib/util_modules/env.js +41 -0
  142. package/lib/util_modules/fs/lib/sync.d.ts +1 -0
  143. package/lib/util_modules/fs/lib/sync.js +19 -0
  144. package/lib/util_modules/logger/utils.d.ts +1 -0
  145. package/lib/util_modules/logger/utils.js +5 -1
  146. package/lib/util_modules/option.d.ts +1 -0
  147. package/lib/util_modules/option.js +8 -0
  148. package/lib/util_modules/{server.js → server/index.js} +3 -3
  149. package/lib/util_modules/server/needed-scopes.d.ts +1 -0
  150. package/lib/util_modules/{constants/lib → server}/needed-scopes.js +7 -7
  151. package/package.json +3 -2
  152. package/lib/init/features/appsail/utils.js +0 -273
  153. package/lib/util_modules/constants/lib/needed-scopes.d.ts +0 -4
  154. /package/lib/docs/{dc.toml → util_modules/dc.toml} +0 -0
  155. /package/lib/util_modules/{server.d.ts → server/index.d.ts} +0 -0
@@ -15,12 +15,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getRuntimeDetails = getRuntimeDetails;
16
16
  exports.getRuntimeChoices = getRuntimeChoices;
17
17
  exports.getRuntimePromptAnswer = getRuntimePromptAnswer;
18
+ exports.ignorableOptsTest = ignorableOptsTest;
19
+ exports.niIgnorableOptsTest = niIgnorableOptsTest;
20
+ const ansi_colors_1 = require("ansi-colors");
18
21
  const catalyst_details_js_1 = __importDefault(require("../../endpoints/lib/catalyst-details.js"));
19
22
  const index_js_1 = __importDefault(require("../../error/index.js"));
20
23
  const index_js_2 = __importDefault(require("../../prompt/index.js"));
21
24
  const index_js_3 = require("../../util_modules/constants/index.js");
22
25
  const logger_1 = require("../../util_modules/logger");
23
26
  const index_1 = require("../../util_modules/logger/index");
27
+ const option_js_1 = require("../../util_modules/option.js");
28
+ const prompt_fields_js_1 = require("./prompt-fields.js");
29
+ const env_js_1 = require("../../util_modules/env.js");
24
30
  function getRuntimeDetails(fnType) {
25
31
  const catalystDetailsAPI = new catalyst_details_js_1.default();
26
32
  return catalystDetailsAPI.getDetails('runtime', fnType ? index_js_3.REMOTE_REF.functions.type[fnType] : undefined);
@@ -88,16 +94,40 @@ function getRuntimeChoices(_a) {
88
94
  function getRuntimePromptAnswer(question, fnType) {
89
95
  return __awaiter(this, void 0, void 0, function* () {
90
96
  const runtimeChoices = yield getRuntimeChoices({ fnType });
91
- const runtimeAns = yield index_js_2.default.ask(index_js_2.default.question('runtime', question, {
97
+ const runtimeAns = yield index_js_2.default.ask(index_js_2.default.question(prompt_fields_js_1.initCommonPromptFields.get('stack'), question, {
92
98
  type: 'list',
93
99
  choices: runtimeChoices,
94
100
  when: () => {
95
101
  return runtimeChoices.length > 1;
96
102
  }
97
103
  }));
98
- if (runtimeAns === undefined || runtimeAns.runtime === undefined) {
104
+ if (runtimeAns === undefined || runtimeAns.stack === undefined) {
99
105
  throw new index_js_1.default('Selected runtime is invalid.', { exit: 2 });
100
106
  }
101
- return runtimeAns.runtime;
107
+ return runtimeAns.stack;
102
108
  });
103
109
  }
110
+ function ignorableOptsTest(optNames, warnMsg, removeFromOpts = true) {
111
+ if (typeof optNames === 'string')
112
+ optNames = [optNames];
113
+ const ignorableOpts = optNames.filter((optName) => {
114
+ const optValue = (0, option_js_1.getOptionValue)(optName, undefined);
115
+ return optValue !== undefined;
116
+ });
117
+ if (ignorableOpts.length > 0) {
118
+ (0, logger_1.warning)(`${warnMsg}\nIgnored options: ${ignorableOpts
119
+ .map((_opt) => (0, ansi_colors_1.italic)(`--${_opt}`))
120
+ .join(', ')}.`);
121
+ if (removeFromOpts) {
122
+ ignorableOpts.forEach((optName) => {
123
+ (0, option_js_1.unsetOption)(optName);
124
+ });
125
+ }
126
+ }
127
+ }
128
+ function niIgnorableOptsTest(optNames, warnMsg, removeFromOpts = true) {
129
+ if (!(0, env_js_1.isNonInteractiveMode)()) {
130
+ return;
131
+ }
132
+ return ignorableOptsTest(optNames, warnMsg, removeFromOpts);
133
+ }
@@ -0,0 +1,7 @@
1
+ export declare const initCommonPromptFields: import("../../prompt/register-fields").PromptFieldsRegistry<readonly [{
2
+ readonly name: "stack";
3
+ readonly cliOption: {
4
+ readonly option: "--stack <stack>";
5
+ readonly description: "catalyst runtime stack to scaffold (for example node18, java17, python_3_9)";
6
+ };
7
+ }]>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initCommonPromptFields = void 0;
4
+ const register_fields_1 = require("../../prompt/register-fields");
5
+ exports.initCommonPromptFields = (0, register_fields_1.definePromptFields)([
6
+ {
7
+ name: 'stack',
8
+ cliOption: {
9
+ option: '--stack <stack>',
10
+ description: 'catalyst runtime stack to scaffold (for example node18, java17, python_3_9)'
11
+ }
12
+ }
13
+ ]);
@@ -11,6 +11,7 @@ declare class Command {
11
11
  private beforeRunners;
12
12
  private optIgnores;
13
13
  private helpText;
14
+ private niHelpText;
14
15
  private allowUnknownOptions;
15
16
  client: Client | null;
16
17
  private subCommand;
@@ -25,8 +26,10 @@ declare class Command {
25
26
  argument(name: string, description: string, defaultValue?: string): Command;
26
27
  allowUnknownOpts(): Command;
27
28
  option(...args: Array<string>): Command;
29
+ option(optionTuple: ReadonlyArray<string>): Command;
28
30
  needs(fn: 'auth' | 'config' | 'rc', args?: unknown): Command;
29
31
  help(text: string): Command;
32
+ niHelp(text: string): Command;
30
33
  addSubCommand(...commands: Array<Command>): Command;
31
34
  ci(value: boolean): Command;
32
35
  helpConfig(config: HelpConfiguration): Command;
@@ -72,6 +72,7 @@ class Command {
72
72
  this.beforeRunners = [];
73
73
  this.optIgnores = [];
74
74
  this.helpText = null;
75
+ this.niHelpText = null;
75
76
  this.client = null;
76
77
  this.allowUnknownOptions = false;
77
78
  }
@@ -91,8 +92,16 @@ class Command {
91
92
  this.allowUnknownOptions = true;
92
93
  return this;
93
94
  }
94
- option(...args) {
95
- this.cmdOptions.push(args);
95
+ option(firstOrTuple, ...rest) {
96
+ if (firstOrTuple === undefined) {
97
+ this.cmdOptions.push(rest.length > 0 ? rest : []);
98
+ return this;
99
+ }
100
+ if (typeof firstOrTuple === 'string') {
101
+ this.cmdOptions.push([firstOrTuple, ...rest]);
102
+ return this;
103
+ }
104
+ this.cmdOptions.push([...firstOrTuple, ...rest]);
96
105
  return this;
97
106
  }
98
107
  needs(fn, args) {
@@ -106,6 +115,13 @@ class Command {
106
115
  this.helpText = text;
107
116
  return this;
108
117
  }
118
+ niHelp(text) {
119
+ this.niHelpText = `\nNon-Interactive Mode help: \n${text
120
+ .split('\n')
121
+ .map((line) => ' ' + line)
122
+ .join('\n')}`;
123
+ return this;
124
+ }
109
125
  addSubCommand(...commands) {
110
126
  commands.forEach((command) => {
111
127
  this.subCommand.push(command);
@@ -159,6 +175,13 @@ class Command {
159
175
  (0, index_1.info)(this.helpText);
160
176
  });
161
177
  }
178
+ if (this.niHelpText !== null) {
179
+ cmd.on('--help', () => {
180
+ if (!(0, env_1.isNonInteractiveMode)())
181
+ return;
182
+ (0, index_1.info)(this.niHelpText);
183
+ });
184
+ }
162
185
  if (this._helpConfig) {
163
186
  cmd.configureHelp(this._helpConfig);
164
187
  }
@@ -266,5 +289,5 @@ class Command {
266
289
  });
267
290
  }
268
291
  }
269
- Command.globalOpts = ['project', 'token', 'dc', 'org', 'verbose'];
292
+ Command.globalOpts = ['project', 'token', 'dc', 'org', 'verbose', 'nonInteractive'];
270
293
  exports.default = Command;
@@ -0,0 +1,2 @@
1
+ export declare const isRequire: () => boolean;
2
+ export declare const migrate: () => void;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.migrate = exports.isRequire = void 0;
7
+ const fs_1 = require("fs");
8
+ const path_1 = require("path");
9
+ const config_store_js_1 = __importDefault(require("../../util_modules/config-store.js"));
10
+ const index_1 = require("../../util_modules/logger/index");
11
+ const LEGACY_CLI_CONFIG_NAME = 'zcatalyst-cli';
12
+ const getLegacyConfigPath = () => {
13
+ const configDir = config_store_js_1.default.path;
14
+ return configDir ? (0, path_1.join)(configDir, `${LEGACY_CLI_CONFIG_NAME}.json`) : undefined;
15
+ };
16
+ const isRequire = () => {
17
+ const legacyPath = getLegacyConfigPath();
18
+ if (!legacyPath || !(0, fs_1.existsSync)(legacyPath)) {
19
+ return false;
20
+ }
21
+ return Object.keys(config_store_js_1.default.store).every((key) => key === 'migration');
22
+ };
23
+ exports.isRequire = isRequire;
24
+ const migrate = () => {
25
+ const legacyPath = getLegacyConfigPath();
26
+ if (!legacyPath) {
27
+ return;
28
+ }
29
+ try {
30
+ const legacyContent = JSON.parse((0, fs_1.readFileSync)(legacyPath, 'utf8'));
31
+ config_store_js_1.default.store = Object.assign(Object.assign({}, legacyContent), { migration: config_store_js_1.default.store.migration });
32
+ (0, index_1.debug)('legacy config migrated to zcatalyst-cli-v1.json');
33
+ }
34
+ catch (e) {
35
+ (0, index_1.debug)('legacy config migration failed');
36
+ throw e;
37
+ }
38
+ };
39
+ exports.migrate = migrate;
@@ -0,0 +1,2 @@
1
+ import type { IChoice } from './index';
2
+ export declare const choice: (name: string, { id, value, short, disabled, checked, ...otherOpts }?: IChoice) => IChoice;
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.choice = void 0;
15
+ const choice = (name, _a = {}) => {
16
+ var { id = name, value = name, short = name, disabled = false, checked = false } = _a, otherOpts = __rest(_a, ["id", "value", "short", "disabled", "checked"]);
17
+ return Object.assign({ id,
18
+ name,
19
+ value,
20
+ short,
21
+ disabled,
22
+ checked }, otherOpts);
23
+ };
24
+ exports.choice = choice;
@@ -0,0 +1 @@
1
+ export declare function commanderOptionKey(option: string): string;
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.commanderOptionKey = commanderOptionKey;
4
+ function commanderOptionKey(option) {
5
+ const parts = option.split(',');
6
+ const tail = parts[parts.length - 1].trim();
7
+ const long = tail.match(/^--([\w-]+)/);
8
+ const kebab = long ? long[1] : tail.replace(/^-+/, '');
9
+ return kebab.replace(/-([a-z])/gi, (_, c) => c.toUpperCase());
10
+ }
@@ -1,26 +1,6 @@
1
1
  import inquirer from 'inquirer';
2
- import Separator from 'inquirer/lib/objects/separator';
3
2
  import PromptUI from 'inquirer/lib/ui/prompt';
4
- import { TreeData, TreeNode } from './types/tree';
5
- export interface IChoice extends inquirer.ChoiceOptions, inquirer.ListChoiceOptions, inquirer.CheckboxChoiceOptions {
6
- id?: string;
7
- }
8
- export interface IQuestion extends inquirer.Question, inquirer.ListQuestionOptions {
9
- name: string;
10
- type?: inquirer.QuestionTypeName | 'file-path' | 'tree' | 'search-box' | 'with-desc';
11
- defaultAns?: unknown;
12
- choices?: Array<IChoice | TSeparator> | Array<string | TSeparator>;
13
- treeChoices?: Array<TreeNode | TreeData>;
14
- rootPath?: string;
15
- exclude?: Array<string>;
16
- excludeDir?: boolean;
17
- ignoreFiles?: boolean;
18
- depth?: number;
19
- empTxt?: string;
20
- validatingText?: string;
21
- description?: string;
22
- }
23
- export type TSeparator = Separator;
3
+ import { buildQuestion, IChoice, IQuestion, TSeparator } from './question-factory';
24
4
  declare const _default: {
25
5
  register: (type: "file-path" | "tree" | "search-box" | "with-desc") => Promise<void>;
26
6
  deregister: (type: "file-path" | "tree" | "search-box") => void;
@@ -29,6 +9,10 @@ declare const _default: {
29
9
  ask: (...questions: Array<IQuestion>) => (Promise<inquirer.Answers> & {
30
10
  ui: PromptUI;
31
11
  }) | Promise<inquirer.Answers>;
32
- question: (name: string, question: string, { type, defaultAns, choices, validate, filter, when, prefix, suffix, ...otheropts }?: Omit<IQuestion, "name">) => IQuestion;
12
+ question: typeof buildQuestion;
33
13
  };
34
14
  export default _default;
15
+ export { overwritePromptQuestion } from './overwrite-prompt';
16
+ export { definePromptFields, PromptFieldHandle, PromptFieldsRegistry } from './register-fields';
17
+ export type { DefinedPromptFields, PromptFieldDef, PromptFieldNames } from './register-fields';
18
+ export type { IQuestion, IChoice, TSeparator } from './question-factory';
@@ -56,18 +56,184 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
56
56
  return (mod && mod.__esModule) ? mod : { "default": mod };
57
57
  };
58
58
  Object.defineProperty(exports, "__esModule", { value: true });
59
+ exports.PromptFieldsRegistry = exports.PromptFieldHandle = exports.definePromptFields = exports.overwritePromptQuestion = void 0;
59
60
  const inquirer_1 = __importDefault(require("inquirer"));
61
+ const separator_1 = __importDefault(require("inquirer/lib/objects/separator"));
60
62
  const error_1 = __importDefault(require("../error"));
63
+ const env_1 = require("../util_modules/env");
64
+ const utils_1 = require("../util_modules/logger/utils");
65
+ const option_1 = require("../util_modules/option");
66
+ const choice_1 = require("./choice");
67
+ const question_factory_1 = require("./question-factory");
61
68
  const runtime_store_1 = __importDefault(require("../runtime-store"));
62
- const choice = (name, _a = {}) => {
63
- var { id = name, value = name, short = name, disabled = false, checked = false } = _a, otherOpts = __rest(_a, ["id", "value", "short", "disabled", "checked"]);
64
- return Object.assign({ id,
65
- name,
66
- value,
67
- short,
68
- disabled,
69
- checked }, otherOpts);
70
- };
69
+ const register_fields_1 = require("./register-fields");
70
+ const ansi_colors_1 = require("ansi-colors");
71
+ function isChoiceSeparator(c) {
72
+ return c instanceof separator_1.default;
73
+ }
74
+ function omitCliOptionKeyForInquirer(q) {
75
+ const { fieldOrName } = q, rest = __rest(q, ["fieldOrName"]);
76
+ void fieldOrName;
77
+ rest.name = (0, question_factory_1.getQuestionPromptName)(q);
78
+ return rest;
79
+ }
80
+ function questionWillBeAsked(question, answers) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ let shouldAsk = true;
83
+ const whenFn = question.when;
84
+ if (whenFn !== undefined) {
85
+ if (typeof whenFn === 'function') {
86
+ const result = whenFn(answers);
87
+ shouldAsk = !!(result instanceof Promise ? yield result : result);
88
+ }
89
+ else {
90
+ shouldAsk = !!whenFn;
91
+ }
92
+ }
93
+ return shouldAsk;
94
+ });
95
+ }
96
+ function ensureCustomPromptTypeRegistered(question) {
97
+ if (question.type && ['file-path', 'tree', 'search-box'].includes(question.type)) {
98
+ if (!registeredTypes.has(question.type)) {
99
+ throw new error_1.default(`Prompt type "${question.type}" is not registered.`, {
100
+ exit: 2
101
+ });
102
+ }
103
+ }
104
+ }
105
+ function applyRawToAnswer(question, answers, raw) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ let value = mapRuntimeRawToAnswer(question, raw);
108
+ const filterFn = question.filter;
109
+ if (typeof filterFn === 'function') {
110
+ const filtered = yield Promise.resolve(filterFn(value, answers));
111
+ value = filtered;
112
+ }
113
+ const validateFn = question.validate;
114
+ if (typeof validateFn === 'function') {
115
+ const v = yield Promise.resolve(validateFn(value, answers));
116
+ if (v !== true) {
117
+ const qName = (0, question_factory_1.getQuestionPromptName)(question);
118
+ throw new error_1.default(typeof v === 'string' ? v : `Validation failed for prompt "${qName}".`, { exit: 2 });
119
+ }
120
+ }
121
+ const qName = (0, question_factory_1.getQuestionPromptName)(question);
122
+ answers[qName] = value;
123
+ });
124
+ }
125
+ function resolveRawInputForQuestion(question, answers, runtimePrompt) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const qName = (0, question_factory_1.getQuestionPromptName)(question);
128
+ let raw = runtimePrompt ? runtimePrompt[qName] : undefined;
129
+ if (raw === undefined && question.default !== undefined) {
130
+ const d = question.default;
131
+ if (typeof d === 'function') {
132
+ const r = d(answers);
133
+ raw = r instanceof Promise ? yield r : r;
134
+ }
135
+ else {
136
+ raw = d;
137
+ }
138
+ }
139
+ if (raw === undefined && question.defaultAns !== undefined) {
140
+ raw = question.defaultAns;
141
+ }
142
+ return raw;
143
+ });
144
+ }
145
+ function resolveRuntimePromptAnswers(questions, runtimePrompt) {
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ const answers = {};
148
+ for (const question of questions) {
149
+ const shouldAsk = yield questionWillBeAsked(question, answers);
150
+ if (shouldAsk) {
151
+ const raw = yield resolveRawInputForQuestion(question, answers, runtimePrompt);
152
+ if (raw === undefined) {
153
+ if ((0, env_1.isNonInteractiveMode)()) {
154
+ const qName = (0, question_factory_1.getQuestionPromptName)(question);
155
+ throw new error_1.default(`Missing runtime input for prompt "${qName}" (after defaults).`, { exit: 2 });
156
+ }
157
+ ensureCustomPromptTypeRegistered(question);
158
+ const qForPrompt = Object.assign(Object.assign({}, omitCliOptionKeyForInquirer(question)), { when: () => true });
159
+ const interactiveSlice = yield inquirer_1.default.prompt([qForPrompt], answers);
160
+ Object.assign(answers, interactiveSlice);
161
+ }
162
+ else {
163
+ yield applyRawToAnswer(question, answers, raw);
164
+ }
165
+ }
166
+ else {
167
+ const raw = yield resolveRawInputForQuestion(question, answers, runtimePrompt);
168
+ if (raw !== undefined) {
169
+ yield applyRawToAnswer(question, answers, raw);
170
+ }
171
+ }
172
+ }
173
+ return answers;
174
+ });
175
+ }
176
+ function mapRuntimeRawToAnswer(question, raw) {
177
+ const qType = question.type;
178
+ const qName = (0, question_factory_1.getQuestionPromptName)(question);
179
+ switch (qType) {
180
+ case 'checkbox': {
181
+ let ids;
182
+ if (typeof raw === 'string') {
183
+ ids = raw
184
+ .split(',')
185
+ .map((s) => s.trim())
186
+ .filter(Boolean);
187
+ }
188
+ else if (Array.isArray(raw)) {
189
+ ids = raw;
190
+ }
191
+ else {
192
+ throw new error_1.default(`Runtime input for "${qName}" must be a comma-separated list or an array of choice ids for checkbox prompts.`, { exit: 2 });
193
+ }
194
+ const choices = (question.choices || []);
195
+ return choices
196
+ .filter((c) => !isChoiceSeparator(c))
197
+ .map((c) => c)
198
+ .filter((c) => !!c.id && ids.includes(c.id))
199
+ .map((c) => c.value);
200
+ }
201
+ case 'list': {
202
+ const choices = question.choices;
203
+ if (!(choices === null || choices === void 0 ? void 0 : choices.length)) {
204
+ return raw;
205
+ }
206
+ if (typeof choices[0] === 'string') {
207
+ return raw;
208
+ }
209
+ const id = String(raw);
210
+ const flat = choices.filter((c) => !isChoiceSeparator(c) && typeof c !== 'string');
211
+ let found = flat.find((c) => c.id === id);
212
+ if (found === undefined) {
213
+ found = flat.find((c) => String(c.value) === id || String(c.name) === id);
214
+ }
215
+ if (found === undefined) {
216
+ const idx = Number(id);
217
+ if (!isNaN(idx) && idx >= 0 && idx <= flat.length) {
218
+ found = flat[idx];
219
+ }
220
+ }
221
+ if (found === undefined) {
222
+ const promptChoices = flat
223
+ .map((c) => '* ' + (typeof c.value === 'string' ? c.value : c.id))
224
+ .join('\n');
225
+ throw new error_1.default(`No list choice matches "${id}" for prompt "${qName}".`, {
226
+ exit: 1,
227
+ errorId: 'E_RUNTIME_PROMPT_NO_MATCH',
228
+ arg: [(0, ansi_colors_1.bold)(id), (0, ansi_colors_1.bold)(qName), promptChoices]
229
+ });
230
+ }
231
+ return found.value;
232
+ }
233
+ default:
234
+ return raw;
235
+ }
236
+ }
71
237
  const registeredTypes = new Map();
72
238
  exports.default = {
73
239
  register: (type) => __awaiter(void 0, void 0, void 0, function* () {
@@ -101,72 +267,103 @@ exports.default = {
101
267
  separator: (line) => {
102
268
  return new inquirer_1.default.Separator(line);
103
269
  },
104
- choice,
270
+ choice: choice_1.choice,
105
271
  ask: (...questions) => {
106
- const runtimePrompt = runtime_store_1.default.get('prompt', false);
107
- if (!runtimePrompt) {
272
+ const runInteractiveAsk = () => __awaiter(void 0, void 0, void 0, function* () {
273
+ if (!(0, env_1.isNonInteractiveMode)()) {
274
+ return inquirer_1.default.prompt(questions.map(omitCliOptionKeyForInquirer));
275
+ }
276
+ const answers = {};
277
+ const askableQuestions = [];
108
278
  for (const question of questions) {
109
- if (question.when &&
110
- question.type &&
111
- ['file-path', 'tree', 'search-box'].includes(question.type)) {
112
- if (!registeredTypes.has(question.type)) {
113
- throw new error_1.default(`Prompt type "${question.type}" is not registered.`, {
114
- exit: 2
115
- });
279
+ if (yield questionWillBeAsked(question, answers)) {
280
+ askableQuestions.push(question);
281
+ const qName = (0, question_factory_1.getQuestionPromptName)(question);
282
+ if (!(qName in answers)) {
283
+ const raw = yield resolveRawInputForQuestion(question, answers);
284
+ if (raw !== undefined) {
285
+ yield applyRawToAnswer(question, answers, raw);
286
+ }
116
287
  }
117
288
  }
118
289
  }
119
- return inquirer_1.default.prompt(questions);
120
- }
121
- return new Promise((res, rej) => {
122
- try {
123
- const answers = {};
124
- questions.forEach((question) => {
125
- var _a;
126
- const runtimeAns = runtimePrompt[question.name];
127
- if (runtimeAns !== undefined) {
128
- switch (question.type) {
129
- case 'checkbox': {
130
- answers[question.name] = question.choices
131
- .filter((choice) => choice.id &&
132
- runtimeAns.includes(choice.id))
133
- .map((filteredChoice) => filteredChoice.value);
134
- break;
135
- }
136
- case 'list': {
137
- answers[question.name] = (_a = question.choices.find((choice) => choice.id === runtimeAns.toString())) === null || _a === void 0 ? void 0 : _a.value;
138
- break;
139
- }
140
- default: {
141
- answers[question.name] = runtimeAns;
142
- }
143
- }
290
+ const missingAskableQuestions = askableQuestions.filter((q) => {
291
+ const qName = (0, question_factory_1.getQuestionPromptName)(q);
292
+ return !(qName in answers);
293
+ });
294
+ if (missingAskableQuestions.length > 0) {
295
+ const names = missingAskableQuestions
296
+ .map((q) => (0, question_factory_1.getQuestionPromptName)(q))
297
+ .join(', ');
298
+ const niSource = (0, env_1.getNonInteractiveModeSource)();
299
+ let hint;
300
+ if ((0, env_1.isCLIBin)()) {
301
+ if (niSource === 'env') {
302
+ hint = `Use command options to supply values, or unset ${(0, ansi_colors_1.italic)('ZCATALYST_NON_INTERACTIVE')}.`;
144
303
  }
304
+ else if (niSource === 'option') {
305
+ hint = `Use command options to supply values, or run without ${(0, ansi_colors_1.italic)('--non-interactive, -ni')}.`;
306
+ }
307
+ else {
308
+ hint =
309
+ 'Use command options to supply values, or disable non-interactive mode.';
310
+ }
311
+ }
312
+ else {
313
+ hint = `Use Catalyst.exec({ inputs: { ... } }) or matching options, or disable non-interactive mode (e.g. omit nonInteractive / ZCATALYST_NON_INTERACTIVE).`;
314
+ }
315
+ const _firstMissingQ = missingAskableQuestions[0];
316
+ if (_firstMissingQ.niInputMissingError) {
317
+ if (typeof _firstMissingQ.niInputMissingError === 'string') {
318
+ throw new error_1.default(_firstMissingQ.niInputMissingError, {
319
+ exit: 1,
320
+ errorId: 'E_NON_INTERACTIVE_MISSING_INPUTS_ANY',
321
+ arg: [(0, ansi_colors_1.bold)(_firstMissingQ.niInputMissingError)]
322
+ });
323
+ }
324
+ _firstMissingQ.niInputMissingError.skipHelp = false;
325
+ throw _firstMissingQ.niInputMissingError;
326
+ }
327
+ throw new error_1.default(`Non-interactive mode requires prepared inputs for: ${(0, ansi_colors_1.bold)(names)}`, {
328
+ exit: 1,
329
+ errorId: 'E_NON_INTERACTIVE_MISSING_INPUTS',
330
+ arg: [
331
+ (0, ansi_colors_1.bold)(names),
332
+ hint,
333
+ (0, utils_1.shellCommand)(`catalyst help ${(0, option_1.getCurrentCommand)()}`)
334
+ ]
145
335
  });
146
- res(answers);
147
- }
148
- catch (err) {
149
- rej(err);
150
336
  }
337
+ return answers;
151
338
  });
152
- },
153
- question: (name, question, _a = {}) => {
154
- var { type = 'input', defaultAns, choices = [], validate = () => {
155
- return true;
156
- }, filter = (ans) => {
157
- return ans;
158
- }, when = () => {
159
- return true;
160
- }, prefix = '', suffix = '' } = _a, otheropts = __rest(_a, ["type", "defaultAns", "choices", "validate", "filter", "when", "prefix", "suffix"]);
161
- if (choices.length > 0 && typeof choices[0] === 'string') {
162
- choices = choices.map((strChoice) => choice(strChoice));
339
+ const stored = runtime_store_1.default.get('context.prompt', false);
340
+ const effective = typeof stored === 'object' && stored !== null ? Object.assign({}, stored) : {};
341
+ let filledFromCli = false;
342
+ for (const q of questions) {
343
+ const qName = (0, question_factory_1.getQuestionPromptName)(q);
344
+ if (effective[qName] === undefined) {
345
+ const optKey = q.fieldOrName instanceof register_fields_1.PromptFieldHandle
346
+ ? q.fieldOrName.optKey
347
+ : q.fieldOrName;
348
+ const fromOpts = (0, option_1.getOptionValue)(optKey, undefined);
349
+ if (fromOpts !== undefined) {
350
+ effective[qName] = fromOpts;
351
+ filledFromCli = true;
352
+ }
353
+ }
163
354
  }
164
- return Object.assign({ type,
165
- name, message: question, default: defaultAns, choices,
166
- validate,
167
- filter,
168
- when,
169
- prefix,
170
- suffix }, otheropts);
171
- }
355
+ const fromExec = typeof stored === 'object' && stored !== null;
356
+ const useRuntimePath = fromExec || filledFromCli;
357
+ if (!useRuntimePath) {
358
+ return runInteractiveAsk();
359
+ }
360
+ return resolveRuntimePromptAnswers(questions, effective);
361
+ },
362
+ question: question_factory_1.buildQuestion
172
363
  };
364
+ var overwrite_prompt_1 = require("./overwrite-prompt");
365
+ Object.defineProperty(exports, "overwritePromptQuestion", { enumerable: true, get: function () { return overwrite_prompt_1.overwritePromptQuestion; } });
366
+ var register_fields_2 = require("./register-fields");
367
+ Object.defineProperty(exports, "definePromptFields", { enumerable: true, get: function () { return register_fields_2.definePromptFields; } });
368
+ Object.defineProperty(exports, "PromptFieldHandle", { enumerable: true, get: function () { return register_fields_2.PromptFieldHandle; } });
369
+ Object.defineProperty(exports, "PromptFieldsRegistry", { enumerable: true, get: function () { return register_fields_2.PromptFieldsRegistry; } });