zcatalyst-cli 1.15.0-beta.2 → 1.15.0-beta.4

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 (122) hide show
  1. package/lib/appsail-utils.js +84 -0
  2. package/lib/archiver.js +18 -1
  3. package/lib/code-deck.js +114 -0
  4. package/lib/command_needs/rc.js +7 -6
  5. package/lib/commands/appsail/add.js +55 -0
  6. package/lib/commands/codelib/install.js +143 -0
  7. package/lib/commands/config/set.js +1 -1
  8. package/lib/commands/deploy/appsail.js +62 -0
  9. package/lib/commands/{deploy.js → deploy/index.js} +20 -14
  10. package/lib/commands/iac/pack.js +3 -3
  11. package/lib/commands/index.js +2 -0
  12. package/lib/commands/init.js +5 -1
  13. package/lib/commands/pull.js +1 -1
  14. package/lib/commands/serve.js +1 -1
  15. package/lib/commands/whoami.js +2 -1
  16. package/lib/deploy/features/appsail/index.js +246 -0
  17. package/lib/deploy/features/appsail/utils.js +91 -0
  18. package/lib/deploy/features/functions/index.js +12 -10
  19. package/lib/deploy/features/index.js +3 -0
  20. package/lib/deploy/index.js +3 -3
  21. package/lib/endpoints/index.js +22 -1
  22. package/lib/endpoints/lib/appsail.js +98 -0
  23. package/lib/endpoints/lib/code-deck.js +71 -0
  24. package/lib/endpoints/lib/common.js +8 -5
  25. package/lib/endpoints/lib/git-hub.js +148 -0
  26. package/lib/endpoints/lib/iac.js +15 -0
  27. package/lib/endpoints/lib/sdk.js +4 -1
  28. package/lib/execute-script.js +35 -27
  29. package/lib/fn-utils/lib/browserLogic.js +17 -20
  30. package/lib/fn-utils/lib/common.js +3 -4
  31. package/lib/fn-utils/lib/integ.js +6 -4
  32. package/lib/fn-utils/lib/java.js +7 -1
  33. package/lib/fn-watcher.js +3 -0
  34. package/lib/iac/status/deploy.js +26 -4
  35. package/lib/iac/status/util/index.js +3 -0
  36. package/lib/init/dependencies/python/ensure-python.js +2 -2
  37. package/lib/init/dependencies/python/pip-install.js +4 -4
  38. package/lib/init/features/appsail/index.js +213 -0
  39. package/lib/init/features/functions/index.js +5 -72
  40. package/lib/init/features/functions/languages/java.js +7 -3
  41. package/lib/init/features/functions/languages/node.js +9 -7
  42. package/lib/init/features/functions/languages/python.js +1 -1
  43. package/lib/init/features/index.js +28 -12
  44. package/lib/init/features/project.js +3 -3
  45. package/lib/init/index.js +1 -1
  46. package/lib/init/util/common.js +103 -0
  47. package/lib/internal/api.js +5 -5
  48. package/lib/internal/command.js +26 -3
  49. package/lib/option-filter.js +40 -21
  50. package/lib/prompt/index.js +5 -0
  51. package/lib/prompt/types/file-path.js +4 -1
  52. package/lib/prompt/types/tree.js +297 -0
  53. package/lib/pull/features/functions/index.js +70 -47
  54. package/lib/serve/features/appsail.js +37 -0
  55. package/lib/serve/features/index.js +8 -1
  56. package/lib/serve/index.js +23 -6
  57. package/lib/serve/server/index.js +111 -233
  58. package/lib/serve/server/lib/appsail/index.js +134 -0
  59. package/lib/serve/server/lib/appsail/start.js +75 -0
  60. package/lib/serve/server/lib/java/index.js +54 -0
  61. package/lib/serve/server/lib/master.js +84 -60
  62. package/lib/serve/server/lib/node/index.js +85 -74
  63. package/lib/serve/server/lib/node/server/index.js +77 -0
  64. package/lib/serve/server/lib/python/index.js +46 -0
  65. package/lib/serve/server/lib/web_client/index.js +33 -0
  66. package/lib/serve/server/lib/web_client/server.js +3 -0
  67. package/lib/shell/dependencies/http-functions.js +7 -7
  68. package/lib/shell/dependencies/local-function.js +6 -6
  69. package/lib/shell/prepare/index.js +1 -1
  70. package/lib/shell/prepare/languages/index.js +3 -3
  71. package/lib/shell/prepare/languages/java.js +2 -2
  72. package/lib/util_modules/config/index.js +3 -1
  73. package/lib/util_modules/config/lib/appSail.js +89 -0
  74. package/lib/util_modules/constants/index.js +3 -1
  75. package/lib/util_modules/constants/lib/default.js +4 -2
  76. package/lib/util_modules/constants/lib/file-names.js +4 -1
  77. package/lib/util_modules/constants/lib/runtime.js +12 -3
  78. package/lib/util_modules/constants/lib/scopes.js +4 -0
  79. package/lib/util_modules/constants/lib/template.js +23 -20
  80. package/lib/util_modules/constants/lib/urls.js +5 -0
  81. package/lib/util_modules/context-help.js +3 -0
  82. package/lib/util_modules/fs/lib/async.js +10 -3
  83. package/lib/util_modules/fs/lib/sync.js +2 -2
  84. package/lib/util_modules/js.js +4 -0
  85. package/lib/util_modules/project.js +2 -2
  86. package/lib/util_modules/server.js +2 -1
  87. package/package.json +1 -1
  88. package/scripts/postInstall.js +27 -18
  89. package/templates/init/functions/java/browserLogic/playwright/sample.java +1 -1
  90. package/templates/init/functions/java/browserLogic/selenium/sample.java +1 -1
  91. package/templates/init/functions/node/browserlogic/playwright/sample.js +1 -1
  92. package/templates/init/functions/node/browserlogic/puppeteer/sample.js +1 -1
  93. package/templates/init/functions/node/browserlogic/selenium/sample.js +1 -1
  94. package/templates/init/functions/node/integ/cliq/package.json +1 -0
  95. package/templates/init/functions/python/aio/requirements.txt +1 -1
  96. package/templates/init/functions/python/bio/requirements.txt +1 -1
  97. package/templates/init/functions/python/cron/requirements.txt +1 -1
  98. package/templates/init/functions/python/event/requirements.txt +1 -1
  99. package/templates/init/functions/python/integ/cliq/requirements.txt +1 -1
  100. /package/lib/serve/server/lib/java/{JavaaioServer.java → aio_server/JavaaioServer.java} +0 -0
  101. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/catalyst-cli-java-runtime-1.0.0.jar +0 -0
  102. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/catalyst-function-sdk-1.0.0.jar +0 -0
  103. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/catalyst-java-runtime-1.0.0.jar +0 -0
  104. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/gradle-wrapper.jar +0 -0
  105. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/jetty-http-9.3.24.v20180605.jar +0 -0
  106. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/jetty-io-9.3.24.v20180605.jar +0 -0
  107. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/jetty-security-9.3.24.v20180605.jar +0 -0
  108. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/jetty-server-9.3.24.v20180605.jar +0 -0
  109. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/jetty-servlet-9.3.24.v20180605.jar +0 -0
  110. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/jetty-util-9.3.24.v20180605.jar +0 -0
  111. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/org.json.jar +0 -0
  112. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/servlet-api-3.1.jar +0 -0
  113. /package/lib/serve/server/lib/java/{lib → aio_server/lib}/zip4j-2.5.0.jar +0 -0
  114. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/browserlogic-playwright.js +0 -0
  115. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/browserlogic-puppeteer.js +0 -0
  116. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/browserlogic-selenium.js +0 -0
  117. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/index.js +0 -0
  118. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/utils/deferred_promise.js +0 -0
  119. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/utils/playwright-handler.js +0 -0
  120. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/utils/puppeteer-handler.js +0 -0
  121. /package/lib/serve/server/lib/node/{browserlogic → server/browserlogic}/utils/selenium-handler.js +0 -0
  122. /package/lib/serve/server/lib/node/{package.json → server/package.json} +0 -0
@@ -53,10 +53,11 @@ class API {
53
53
  headers
54
54
  };
55
55
  if (!isExternal) {
56
- this.requestOpts.headers = Object.assign(Object.assign({}, this.requestOpts.headers), { Accept: 'application/vnd.catalyst.v2+json', 'User-Agent': runtime_store_1.default.get('context.cli.package.name') +
57
- '/' +
58
- runtime_store_1.default.get('context.cli.package.version'), 'X-CATALYST-Environment': env, 'CATALYST-ORG': envId });
56
+ this.requestOpts.headers = Object.assign(Object.assign({}, this.requestOpts.headers), { Accept: 'application/vnd.catalyst.v2+json', 'X-CATALYST-Environment': env, 'CATALYST-ORG': envId });
59
57
  }
58
+ this.requestOpts.headers = Object.assign(Object.assign({}, this.requestOpts.headers), { 'User-Agent': runtime_store_1.default.get('context.cli.package.name') +
59
+ '/' +
60
+ runtime_store_1.default.get('context.cli.package.version') });
60
61
  this.authNeeded = authNeeded;
61
62
  this.resolveOnHTTPError = resolveOnError;
62
63
  this.logOpts = log;
@@ -216,8 +217,7 @@ class API {
216
217
  this.authNeeded = js_1.JS.get(options, 'authNeeded', this.authNeeded);
217
218
  this.logOpts = js_1.JS.get(options, 'log', this.logOpts);
218
219
  this.requestOpts = js_1.JS.defaultsDeep(js_1.JS.omit(options, ['origin', 'auth', 'maxRetry', 'resolveOnError', 'log']), this.requestOpts);
219
- this.requestOpts.url =
220
- js_1.JS.get(options, 'origin', this.requestOpts.url || constants_1.ORIGIN.admin) + path;
220
+ this.requestOpts.url = ((options === null || options === void 0 ? void 0 : options.origin) || this.requestOpts.url || constants_1.ORIGIN.admin) + path;
221
221
  this.requestOpts.method = method;
222
222
  if (this.authNeeded) {
223
223
  yield this._addAuthHeader();
@@ -48,6 +48,7 @@ const option_1 = require("../util_modules/option");
48
48
  const project_1 = require("../util_modules/project");
49
49
  class Command {
50
50
  constructor(cmd) {
51
+ this.isSubCommand = false;
51
52
  this.command = cmd;
52
53
  this.cmdName = cmd.split(' ')[0];
53
54
  this.aliasName = null;
@@ -87,6 +88,19 @@ class Command {
87
88
  this.helpText = text;
88
89
  return this;
89
90
  }
91
+ addSubCommand(command) {
92
+ this.subCommand = command;
93
+ command.isSubCommand = true;
94
+ return this;
95
+ }
96
+ helpConfig(config) {
97
+ this._helpConfig = config;
98
+ return this;
99
+ }
100
+ usage(cmdUsage) {
101
+ this.cmdUsage = cmdUsage;
102
+ return this;
103
+ }
90
104
  action(fn) {
91
105
  this.cmdAction = fn;
92
106
  return this;
@@ -95,9 +109,9 @@ class Command {
95
109
  this.optIgnores = opts;
96
110
  return this;
97
111
  }
98
- register(client) {
112
+ register(client, cli) {
99
113
  this.client = client;
100
- const program = client.cli;
114
+ const program = cli || client.cli;
101
115
  const cmd = program.command(this.command);
102
116
  if (this.aliasName !== null) {
103
117
  cmd.alias(this.aliasName);
@@ -105,11 +119,17 @@ class Command {
105
119
  if (this.cmdDescription !== null) {
106
120
  cmd.description(this.cmdDescription);
107
121
  }
122
+ if (this.cmdUsage) {
123
+ cmd.usage(this.cmdUsage);
124
+ }
108
125
  if (this.helpText !== null) {
109
126
  cmd.on('--help', () => {
110
127
  (0, logger_1.info)(this.helpText);
111
128
  });
112
129
  }
130
+ if (this._helpConfig) {
131
+ cmd.configureHelp(this._helpConfig);
132
+ }
113
133
  cmd.allowUnknownOption(this.allowUnknownOptions);
114
134
  this.cmdOptions.forEach((args) => {
115
135
  if (args.length > 0) {
@@ -142,6 +162,9 @@ class Command {
142
162
  yield (0, track_1.default)(errorEvent, preppedMessage, duration);
143
163
  }
144
164
  }));
165
+ if (this.subCommand) {
166
+ this.subCommand.register(client, cmd);
167
+ }
145
168
  return cmd;
146
169
  }
147
170
  _prepare(cmd) {
@@ -175,7 +198,7 @@ class Command {
175
198
  ? command.parseOptions(process.argv).unknown
176
199
  : undefined;
177
200
  runtime_store_1.default.set('opts', Object.assign({ _name: command.name(), unknownOpts }, opts));
178
- this._prepare(command);
201
+ this._prepare(this.isSubCommand && command.parent ? command.parent : command);
179
202
  try {
180
203
  yield (0, migration_1.default)(runtime_store_1.default.get('context.cli.package.version'));
181
204
  }
@@ -36,7 +36,18 @@ const fs_1 = require("./util_modules/fs");
36
36
  const js_1 = require("./util_modules/js");
37
37
  const option_1 = require("./util_modules/option");
38
38
  const project_1 = require("./util_modules/project");
39
- const validGlobalTargets = ['functions', 'client', 'apig'];
39
+ const onlyExceptTargets = [
40
+ { filterName: ['functions'], target: 'functions' },
41
+ { filterName: ['client'], target: 'client' },
42
+ { filterName: ['apig'], target: 'apig' },
43
+ { filterName: ['appsail'], target: 'appsail' }
44
+ ];
45
+ const portTargets = [
46
+ { filterName: ['appsail'], target: 'appsail' },
47
+ { filterName: ['advancedio', 'aio'], target: 'advancedio' },
48
+ { filterName: ['basicio', 'bio'], target: 'basicio' },
49
+ { filterName: ['client'], target: 'client' }
50
+ ];
40
51
  function enrichOptsData(opt, optValue, validTargets, multiTargetSupport = true) {
41
52
  if (optValue === undefined || typeof optValue === 'boolean') {
42
53
  return [];
@@ -56,28 +67,32 @@ function enrichOptsData(opt, optValue, validTargets, multiTargetSupport = true)
56
67
  });
57
68
  }
58
69
  const entrySet = entry.split(':');
59
- if (!validTargets.includes(entrySet[0])) {
70
+ const resultantTarget = validTargets.find((targs) => targs.filterName.includes(entrySet[0]));
71
+ if (!resultantTarget) {
60
72
  throw new error_1.default(`Invalid target "${entrySet[0]}" specified for option "${opt}".`, {
61
73
  exit: 1,
62
74
  errorId: 'OPT-FILTER-2',
63
75
  arg: [
64
76
  ansi_colors_1.bold.red(entrySet[0]),
65
77
  (0, ansi_colors_1.bold)('--' + opt),
66
- (0, ansi_colors_1.bold)(validTargets.map((target) => '* ' + target).join('\n'))
78
+ (0, ansi_colors_1.bold)(validTargets
79
+ .map((target) => '* ' + target.filterName.join(' | '))
80
+ .join('\n'))
67
81
  ]
68
82
  });
69
83
  }
70
- if (!multiTargetSupport && accumulator.some((res) => res.target === entrySet[0])) {
84
+ if (!multiTargetSupport &&
85
+ accumulator.some((res) => res.target.filterName.includes(entrySet[0]))) {
71
86
  throw new error_1.default(`Target "${entrySet[0]}" is specified more than once for option "${opt}". Please specify only one.`, {
72
87
  exit: 1
73
88
  });
74
89
  }
75
90
  const resultant = {
76
91
  hasFilter: false,
77
- target: entrySet[0]
92
+ target: resultantTarget
78
93
  };
79
94
  if (accumulator.some((res, idx) => {
80
- const truthy = res.target === entrySet[0] &&
95
+ const truthy = res.target.filterName.includes(entrySet[0]) &&
81
96
  ((res.hasFilter && entrySet.length === 1) ||
82
97
  (!res.hasFilter && entrySet.length === 2));
83
98
  if (truthy) {
@@ -115,22 +130,22 @@ function validateTargets() {
115
130
  arg: [(0, ansi_colors_1.bold)('--only'), (0, ansi_colors_1.bold)('--except')]
116
131
  });
117
132
  }
118
- const enrichedOnlyOpts = enrichOptsData('only', (0, option_1.getOptionValue)('only'), validGlobalTargets);
133
+ const enrichedOnlyOpts = enrichOptsData('only', (0, option_1.getOptionValue)('only'), onlyExceptTargets);
119
134
  if (enrichedOnlyOpts.length > 0) {
120
135
  return enrichedOnlyOpts;
121
136
  }
122
- const enrichedExceptOpts = enrichOptsData('except', (0, option_1.getOptionValue)('except'), validGlobalTargets);
137
+ const enrichedExceptOpts = enrichOptsData('except', (0, option_1.getOptionValue)('except'), onlyExceptTargets);
123
138
  if (enrichedExceptOpts.length > 0) {
124
139
  return enrichedExceptOpts;
125
140
  }
126
141
  return [];
127
142
  }
128
143
  function validatePorts() {
129
- const customHttpPort = (0, option_1.getOptionValue)('http', false);
130
- const enrichedDebugOpts = enrichOptsData('debug', (0, option_1.getOptionValue)('debug'), Object.keys(constants_1.DEFAULT.serve_port.debug));
144
+ const customHttpPort = enrichOptsData('debug', (0, option_1.getOptionValue)('debug'), portTargets);
145
+ const enrichedDebugOpts = enrichOptsData('debug', (0, option_1.getOptionValue)('debug'), portTargets);
131
146
  if (enrichedDebugOpts.some((opt) => {
132
147
  const noFilter = !opt.hasFilter;
133
- if (opt.target === 'client') {
148
+ if (opt.target.target === 'client') {
134
149
  throw new error_1.default('Client cannot be debugged', {
135
150
  exit: 1,
136
151
  errorId: 'OPT-FILTER-5'
@@ -144,8 +159,8 @@ function validatePorts() {
144
159
  arg: [
145
160
  (0, ansi_colors_1.bold)('--debug'),
146
161
  ansi_colors_1.italic.red('--debug <target>'),
147
- (0, ansi_colors_1.bold)('* basicio: ') + ansi_colors_1.italic.green('--debug basicio:<port>'),
148
- (0, ansi_colors_1.bold)('* advancedio: ') + ansi_colors_1.italic.green('--debug advancedio:<name>:<port>')
162
+ (0, ansi_colors_1.bold)('* basicio: ') + ansi_colors_1.italic.green('--debug <basicio|bio>:<port>'),
163
+ (0, ansi_colors_1.bold)('* advancedio: ') + ansi_colors_1.italic.green('--debug <advancedio|aio>:<name>:<port>')
149
164
  ]
150
165
  });
151
166
  }
@@ -158,19 +173,19 @@ function filterPorts() {
158
173
  const validOptResultants = validatePorts();
159
174
  runtime_store_1.default.set(`context.port.http.master`, validOptResultants.port);
160
175
  validOptResultants.debug.forEach((res) => {
161
- if (res.target === constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.advanced] && !res.specific) {
176
+ if (res.target.target === constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.advanced] && !res.specific) {
162
177
  throw new error_1.default('Invalid input format for advancedio target', {
163
178
  exit: 1,
164
179
  errorId: 'OPT-FILTER-7',
165
180
  arg: [
166
181
  (0, ansi_colors_1.bold)('--debug'),
167
182
  (0, ansi_colors_1.bold)('advancedio'),
168
- ansi_colors_1.italic.red('--debug advancedio:<name|port>'),
169
- ansi_colors_1.italic.green('--debug advancedio:<name>:<port>')
183
+ ansi_colors_1.italic.red('--debug <advancedio|aio>:<name|port>'),
184
+ ansi_colors_1.italic.green('--debug <advancedio|aio>:<name>:<port>')
170
185
  ]
171
186
  });
172
187
  }
173
- else if (res.target === constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]) {
188
+ else if (res.target.target === constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]) {
174
189
  runtime_store_1.default.set(`context.port.debug.${res.target}`, res.filter);
175
190
  }
176
191
  else {
@@ -182,10 +197,13 @@ exports.filterPorts = filterPorts;
182
197
  function filterTargets(pathSense = true) {
183
198
  const validTargetResultants = validateTargets();
184
199
  const config = runtime_store_1.default.get('config');
185
- let targets = validGlobalTargets.filter((t) => config.has(t));
200
+ let targets = onlyExceptTargets.filter((t) => config.has(t.target)).map((t) => t.target);
186
201
  if (validTargetResultants.length === 0) {
187
202
  if (runtime_store_1.default.get('project.root') !== runtime_store_1.default.get('cwd')) {
188
203
  targets = targets.filter((target) => {
204
+ if (target === 'appsail') {
205
+ return true;
206
+ }
189
207
  if (target === 'apig' && (0, option_1.getCurrentCommand)() === 'serve') {
190
208
  return true;
191
209
  }
@@ -200,18 +218,19 @@ function filterTargets(pathSense = true) {
200
218
  }
201
219
  else if ((0, option_1.getOptionValue)('only', false)) {
202
220
  targets = validTargetResultants.map((resultant) => {
203
- return resultant.target;
221
+ return resultant.target.target;
204
222
  });
205
223
  }
206
224
  else if ((0, option_1.getOptionValue)('except', false)) {
207
225
  const unwantedTargets = validTargetResultants
208
226
  .filter((val) => {
209
- if (val.target === 'functions' && val.hasFilter) {
227
+ if ((val.target.target === 'functions' || val.target.target === 'appsail') &&
228
+ val.hasFilter) {
210
229
  return false;
211
230
  }
212
231
  return true;
213
232
  })
214
- .map((resultant) => resultant.target);
233
+ .map((resultant) => resultant.target.target);
215
234
  targets = targets.filter((target) => !unwantedTargets.includes(target));
216
235
  }
217
236
  targets = js_1.JS.uniq(targets);
@@ -63,6 +63,11 @@ exports.default = {
63
63
  const filePathExt = yield Promise.resolve().then(() => __importStar(require('./types/file-path')));
64
64
  inquirer_1.default.registerPrompt('file-path', filePathExt.default);
65
65
  break;
66
+ case 'tree': {
67
+ const treeExt = yield Promise.resolve().then(() => __importStar(require('./types/tree')));
68
+ inquirer_1.default.registerPrompt('tree', treeExt.default);
69
+ break;
70
+ }
66
71
  default:
67
72
  throw new error_1.default('No such type registered', { exit: 2 });
68
73
  }
@@ -19,7 +19,10 @@ const fs_1 = require("../../util_modules/fs");
19
19
  const ansi_colors_1 = require("ansi-colors");
20
20
  function getPaths(rootPath, pattern, exclude, defaultItem) {
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
- const nodeList = yield fs_1.ASYNC.walk(rootPath, { filter: { exclude, excludeDir: false } });
22
+ const nodeList = yield fs_1.ASYNC.walk(rootPath, {
23
+ filter: { exclude, excludeDir: false },
24
+ includeDirPaths: true
25
+ });
23
26
  const filteredNodes = fuzzy_1.default.filter(pattern || '', nodeList).map((e) => e.string);
24
27
  if (!pattern && defaultItem) {
25
28
  filteredNodes.unshift(defaultItem);
@@ -0,0 +1,297 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ var _TreeNode_instances, _TreeNode_constructNodes;
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TreeNode = void 0;
13
+ const ansi_colors_1 = require("ansi-colors");
14
+ const cli_cursor_1 = __importDefault(require("cli-cursor"));
15
+ const base_js_1 = __importDefault(require("inquirer/lib/prompts/base.js"));
16
+ const events_js_1 = __importDefault(require("inquirer/lib/utils/events.js"));
17
+ const paginator_js_1 = __importDefault(require("inquirer/lib/utils/paginator.js"));
18
+ const figures = {
19
+ arrowRight: '→',
20
+ arrowDown: '↓'
21
+ };
22
+ var ETreeState;
23
+ (function (ETreeState) {
24
+ ETreeState[ETreeState["OPEN"] = 1] = "OPEN";
25
+ ETreeState[ETreeState["CLOSED"] = 2] = "CLOSED";
26
+ ETreeState[ETreeState["EXPANDED"] = 3] = "EXPANDED";
27
+ })(ETreeState || (ETreeState = {}));
28
+ class TreeNode {
29
+ constructor(root) {
30
+ _TreeNode_instances.add(this);
31
+ this.open = false;
32
+ this.leaves = [];
33
+ this.activeIdx = 0;
34
+ this.prevActiveIdx = -1;
35
+ this.nodes = [];
36
+ this.level = 1;
37
+ this.root = root;
38
+ }
39
+ get indent() {
40
+ return this.level * 2;
41
+ }
42
+ addNode(node) {
43
+ if (node instanceof TreeNode) {
44
+ this.leaves.push(node);
45
+ }
46
+ else {
47
+ this.leaves.push(Object.assign({ isExpanded: false }, node));
48
+ }
49
+ return this;
50
+ }
51
+ buildTree({ paginator, force = false } = {}) {
52
+ if (this.nodes.length === 0 || force) {
53
+ __classPrivateFieldGet(this, _TreeNode_instances, "m", _TreeNode_constructNodes).call(this);
54
+ }
55
+ if (this.prevActiveIdx >= 0) {
56
+ this.nodes[this.prevActiveIdx] = (0, ansi_colors_1.stripColor)(this.nodes[this.prevActiveIdx]);
57
+ }
58
+ const activeNodeArr = this.nodes[this.activeIdx].split('\n');
59
+ this.nodes[this.activeIdx] =
60
+ (0, ansi_colors_1.cyan)((0, ansi_colors_1.stripColor)(activeNodeArr.shift() || '')) +
61
+ (activeNodeArr.length > 0 ? '\n' + activeNodeArr.join('\n') : '');
62
+ return paginator
63
+ ? paginator.paginate(this.nodes.join('\n'), this.activeIdx, 3)
64
+ : this.nodes.join('\n');
65
+ }
66
+ expandNode(display, expand) {
67
+ const lineLength = process.stdout.columns -
68
+ (display.length % (process.stdout.columns - this.indent)) -
69
+ this.indent;
70
+ return (this.wrapLine('- ' + display, this.indent - 2) +
71
+ ' '.repeat(lineLength) +
72
+ this.wrapLine(expand, this.indent + 2));
73
+ }
74
+ openTree(strict = false) {
75
+ const activeLeaf = this.leaves[this.activeIdx];
76
+ if (!(activeLeaf instanceof TreeNode)) {
77
+ if (!activeLeaf.expand || strict) {
78
+ return ETreeState.CLOSED;
79
+ }
80
+ this.nodes[this.activeIdx] = this.expandNode(activeLeaf.display, activeLeaf.expand);
81
+ activeLeaf.isExpanded = true;
82
+ return ETreeState.EXPANDED;
83
+ }
84
+ if (activeLeaf.leaves.length === 0) {
85
+ throw new Error('No leaves');
86
+ }
87
+ if (activeLeaf.open) {
88
+ const state = activeLeaf.openTree();
89
+ if (state === ETreeState.EXPANDED) {
90
+ const activeNode = this.nodes[this.activeIdx].split('\n')[0];
91
+ const subTree = activeLeaf.buildTree();
92
+ this.nodes[this.activeIdx] = activeNode + '\n' + subTree;
93
+ return state;
94
+ }
95
+ else if (state === ETreeState.CLOSED) {
96
+ return state;
97
+ }
98
+ }
99
+ const activeNode = this.nodes[this.activeIdx]
100
+ .split('\n')[0]
101
+ .replace(figures.arrowRight, figures.arrowDown);
102
+ activeLeaf.level = this.level + 1;
103
+ const subTree = activeLeaf.buildTree();
104
+ this.nodes[this.activeIdx] = activeNode + '\n' + subTree;
105
+ activeLeaf.open = true;
106
+ return ETreeState.OPEN;
107
+ }
108
+ closeTree() {
109
+ const activeLeaf = this.leaves[this.activeIdx];
110
+ if (!(activeLeaf instanceof TreeNode) || !activeLeaf.open) {
111
+ return;
112
+ }
113
+ const subActive = activeLeaf.leaves[activeLeaf.activeIdx];
114
+ if (subActive instanceof TreeNode && subActive.open) {
115
+ activeLeaf.closeTree();
116
+ this.nodes[this.activeIdx] = this.nodes[this.activeIdx].replace(/\n{1}[\s\S]*/m, '\n' + activeLeaf.buildTree({ force: true }));
117
+ return;
118
+ }
119
+ else if (!(subActive instanceof TreeNode) && subActive.expand && subActive.isExpanded) {
120
+ activeLeaf.nodes[activeLeaf.activeIdx] = this.wrapLine('+ ' + subActive.display, activeLeaf.indent - 2);
121
+ this.nodes[this.activeIdx] =
122
+ this.nodes[this.activeIdx].split('\n')[0] + '\n' + activeLeaf.buildTree();
123
+ subActive.isExpanded = false;
124
+ return;
125
+ }
126
+ this.nodes[this.activeIdx] = this.nodes[this.activeIdx]
127
+ .replace(figures.arrowDown, figures.arrowRight)
128
+ .replace(/\n{1}[\s\S]*/m, '');
129
+ activeLeaf.open = false;
130
+ }
131
+ moveActive(distance = 0) {
132
+ const activeLeaf = this.leaves[this.activeIdx];
133
+ if (activeLeaf instanceof TreeNode && activeLeaf.open) {
134
+ activeLeaf.moveActive(distance);
135
+ const subTree = activeLeaf.buildTree();
136
+ this.nodes[this.activeIdx] =
137
+ this.wrapLine(figures.arrowDown + ' ' + activeLeaf.root, this.indent - 2) +
138
+ '\n' +
139
+ subTree;
140
+ return;
141
+ }
142
+ const effectiveIndex = this.activeIdx + distance;
143
+ if (effectiveIndex === -1 && this.leaves.length > 1) {
144
+ this.prevActiveIdx = 0;
145
+ this.activeIdx = this.leaves.length - 1;
146
+ return;
147
+ }
148
+ else if (effectiveIndex === this.leaves.length && this.leaves.length > 1) {
149
+ this.prevActiveIdx = this.leaves.length - 1;
150
+ this.activeIdx = 0;
151
+ return;
152
+ }
153
+ else if (effectiveIndex <= -1 || effectiveIndex >= this.leaves.length) {
154
+ return;
155
+ }
156
+ this.prevActiveIdx = this.activeIdx;
157
+ this.activeIdx = effectiveIndex;
158
+ }
159
+ getValue() {
160
+ const activeLeaf = this.leaves[this.activeIdx];
161
+ if (activeLeaf instanceof TreeNode) {
162
+ return activeLeaf.getValue();
163
+ }
164
+ return activeLeaf;
165
+ }
166
+ wrapLine(line, indent) {
167
+ if (line.length <= process.stdout.columns - indent) {
168
+ return ' '.repeat(indent) + line;
169
+ }
170
+ const splitLine = line.slice(0, process.stdout.columns - indent);
171
+ return (' '.repeat(indent) +
172
+ splitLine +
173
+ this.wrapLine(line.slice(process.stdout.columns - (indent + 1)).trimStart(), indent));
174
+ }
175
+ }
176
+ exports.TreeNode = TreeNode;
177
+ _TreeNode_instances = new WeakSet(), _TreeNode_constructNodes = function _TreeNode_constructNodes() {
178
+ const nodes = this.leaves.map((leaf) => {
179
+ if (leaf instanceof TreeNode) {
180
+ const rootNode = this.wrapLine((leaf.open ? figures.arrowDown : figures.arrowRight) + ' ' + leaf.root, this.indent - 2);
181
+ const subNodes = leaf.open ? '\n' + leaf.nodes.join('\n') : '';
182
+ return rootNode + subNodes;
183
+ }
184
+ if (leaf.expand) {
185
+ return this.wrapLine('+ ' + leaf.display, this.indent - 2);
186
+ }
187
+ return this.wrapLine(leaf.display, this.indent);
188
+ });
189
+ this.nodes = nodes;
190
+ };
191
+ class InquirerTree extends base_js_1.default {
192
+ constructor(question, rl, answers) {
193
+ const questionBase = {
194
+ name: question.name,
195
+ message: question.message
196
+ };
197
+ super(questionBase, rl, answers);
198
+ this.treeOpts = {
199
+ pageSize: 10,
200
+ multiple: false,
201
+ loop: true
202
+ };
203
+ this.message = question.message;
204
+ this.choices = new TreeNode();
205
+ question.treeChoices.forEach((choice) => this.choices.addNode(choice));
206
+ this.done = () => undefined;
207
+ this.opt.default = null;
208
+ this.paginator = new paginator_js_1.default(this.screen, { isInfinite: this.treeOpts.loop !== false });
209
+ }
210
+ _run(callback) {
211
+ this.done = callback;
212
+ cli_cursor_1.default.hide();
213
+ this.bindKeyEvents();
214
+ this.render();
215
+ return this;
216
+ }
217
+ bindKeyEvents() {
218
+ const events = (0, events_js_1.default)(this.rl);
219
+ events.normalizedDownKey.subscribe(() => {
220
+ try {
221
+ this.choices.moveActive(1);
222
+ this.render();
223
+ }
224
+ catch (er) {
225
+ this.render({ error: er });
226
+ }
227
+ });
228
+ events.normalizedUpKey.subscribe(() => {
229
+ try {
230
+ this.choices.moveActive(-1);
231
+ this.render();
232
+ }
233
+ catch (er) {
234
+ this.render({ error: er });
235
+ }
236
+ });
237
+ events.line.subscribe(() => {
238
+ try {
239
+ const treeState = this.choices.openTree(true);
240
+ if (treeState === ETreeState.OPEN) {
241
+ this.render();
242
+ return;
243
+ }
244
+ const answer = this.choices.getValue();
245
+ this.render({ answer });
246
+ this.screen.done();
247
+ cli_cursor_1.default.show();
248
+ if (this.done) {
249
+ this.done(answer.value);
250
+ }
251
+ }
252
+ catch (er) {
253
+ this.render({ error: er });
254
+ }
255
+ });
256
+ events.keypress.subscribe((event) => {
257
+ try {
258
+ switch (event.key.name) {
259
+ case 'right': {
260
+ this.choices.openTree();
261
+ break;
262
+ }
263
+ case 'left': {
264
+ this.choices.closeTree();
265
+ break;
266
+ }
267
+ default: {
268
+ return;
269
+ }
270
+ }
271
+ return this.render();
272
+ }
273
+ catch (er) {
274
+ this.render({ error: er });
275
+ }
276
+ });
277
+ }
278
+ constructTree() {
279
+ this.choices.buildTree();
280
+ }
281
+ render({ answer, error } = {}) {
282
+ let message = (0, ansi_colors_1.bold)(this.message);
283
+ if (answer) {
284
+ message += ` ${(0, ansi_colors_1.cyan)(answer.short)}`;
285
+ }
286
+ else {
287
+ message += (0, ansi_colors_1.dim)(` (Use arrow keys to navigate and enter to select)`);
288
+ message += '\n' + this.choices.buildTree();
289
+ }
290
+ let bottomContent = '';
291
+ if (error) {
292
+ bottomContent = '\n' + (0, ansi_colors_1.red)('>> ') + (error instanceof Error ? error.message : error);
293
+ }
294
+ this.screen.render(message, bottomContent);
295
+ }
296
+ }
297
+ exports.default = InquirerTree;