zcatalyst-cli 1.10.1 → 1.11.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 (113) hide show
  1. package/docs/client-utils.toml +5 -0
  2. package/docs/command_needs/auth.toml +1 -1
  3. package/docs/plugin-loader.toml +4 -0
  4. package/docs/serve/server/index.toml +4 -0
  5. package/docs/serve/server/lib/web_client/server.toml +4 -0
  6. package/lib/apig-utils.js +2 -1
  7. package/lib/client-utils.js +35 -6
  8. package/lib/command_needs/auth.js +2 -2
  9. package/lib/commands/client/delete.js +1 -0
  10. package/lib/commands/event/generate/index.js +4 -13
  11. package/lib/commands/iac/import.js +3 -1
  12. package/lib/commands/init.js +1 -1
  13. package/lib/commands/pull.js +1 -1
  14. package/lib/commands/serve.js +2 -1
  15. package/lib/error.js +6 -5
  16. package/lib/errorOut.js +2 -2
  17. package/lib/fn-utils/lib/common.js +4 -4
  18. package/lib/fn-utils/lib/java.js +1 -1
  19. package/lib/init/dependencies/npm-install.js +2 -9
  20. package/lib/init/features/client/index.js +47 -0
  21. package/lib/init/features/client/initializers/angular.js +88 -0
  22. package/lib/init/features/client/initializers/basic.js +27 -0
  23. package/lib/init/features/client/initializers/lyte.js +44 -0
  24. package/lib/init/features/client/initializers/react.js +53 -0
  25. package/lib/init/features/functions/languages/node.js +3 -1
  26. package/lib/init/features/index.js +10 -3
  27. package/lib/init/features/project.js +1 -1
  28. package/lib/internal/command.js +4 -1
  29. package/lib/internal/config.js +2 -1
  30. package/lib/optional-import.js +11 -25
  31. package/lib/plugin-loader.js +8 -2
  32. package/lib/port-resolver.js +4 -2
  33. package/lib/serve/index.js +6 -1
  34. package/lib/serve/server/index.js +52 -8
  35. package/lib/serve/server/lib/master.js +28 -12
  36. package/lib/serve/server/lib/web_client/index.js +30 -0
  37. package/lib/serve/server/lib/web_client/server.js +171 -0
  38. package/lib/serve/server/lib/web_client/utils.js +10 -0
  39. package/lib/util_modules/char.js +1 -1
  40. package/lib/util_modules/config/lib/apig.js +2 -1
  41. package/lib/util_modules/constants/index.js +3 -1
  42. package/lib/util_modules/constants/lib/placeholders.js +3 -1
  43. package/lib/util_modules/constants/lib/plugin.js +28 -0
  44. package/lib/util_modules/constants/lib/template.js +11 -1
  45. package/lib/util_modules/constants/lib/urls.js +8 -0
  46. package/lib/util_modules/{contextHelp.js → context-help.js} +0 -0
  47. package/lib/util_modules/env.js +13 -12
  48. package/lib/util_modules/global-space.js +99 -0
  49. package/lib/util_modules/option.js +5 -1
  50. package/lib/util_modules/shell.js +10 -6
  51. package/lib/winston.js +1 -1
  52. package/package.json +6 -3
  53. package/templates/init/.DS_Store +0 -0
  54. package/templates/init/client/.DS_Store +0 -0
  55. package/templates/init/client/{client-package.json → basic/client-package.json} +0 -0
  56. package/templates/init/client/{index.html → basic/index.html} +0 -0
  57. package/templates/init/client/{main.css → basic/main.css} +0 -0
  58. package/templates/init/client/{main.js → basic/main.js} +0 -0
  59. package/templates/init/client/lyte/build/build.js +301 -0
  60. package/templates/init/client/lyte/build/scripts/cliDownloadScript.js +54 -0
  61. package/templates/init/client/lyte/client-package.json +5 -0
  62. package/templates/init/client/lyte/components/javascript/welcome-comp.js +13 -0
  63. package/templates/init/client/lyte/components/styles/welcome-comp.css +0 -0
  64. package/templates/init/client/lyte/components/templates/welcome-comp.html +8 -0
  65. package/templates/init/client/lyte/data-store/adapters/.gitkeep +0 -0
  66. package/templates/init/client/lyte/data-store/models/.gitkeep +0 -0
  67. package/templates/init/client/lyte/data-store/serializers/.gitkeep +0 -0
  68. package/templates/init/client/lyte/index.html +17 -0
  69. package/templates/init/client/lyte/package.json +12 -0
  70. package/templates/init/client/lyte/router.js +14 -0
  71. package/templates/init/client/lyte/routes/index.js +54 -0
  72. package/templates/init/client/react/.DS_Store +0 -0
  73. package/templates/init/client/react/react_js/package.json +11 -0
  74. package/templates/init/client/react/react_js/template/README.md +70 -0
  75. package/templates/init/client/react/react_js/template/client-package.json +5 -0
  76. package/templates/init/client/react/react_js/template/gitignore +23 -0
  77. package/templates/init/client/react/react_js/template/public/favicon.ico +0 -0
  78. package/templates/init/client/react/react_js/template/public/index.html +43 -0
  79. package/templates/init/client/react/react_js/template/public/logo192.png +0 -0
  80. package/templates/init/client/react/react_js/template/public/logo512.png +0 -0
  81. package/templates/init/client/react/react_js/template/public/manifest.json +25 -0
  82. package/templates/init/client/react/react_js/template/public/robots.txt +3 -0
  83. package/templates/init/client/react/react_js/template/src/App.css +38 -0
  84. package/templates/init/client/react/react_js/template/src/App.js +25 -0
  85. package/templates/init/client/react/react_js/template/src/App.test.js +8 -0
  86. package/templates/init/client/react/react_js/template/src/index.css +13 -0
  87. package/templates/init/client/react/react_js/template/src/index.js +17 -0
  88. package/templates/init/client/react/react_js/template/src/logo.svg +1 -0
  89. package/templates/init/client/react/react_js/template/src/reportWebVitals.js +13 -0
  90. package/templates/init/client/react/react_js/template/src/setupTests.js +5 -0
  91. package/templates/init/client/react/react_js/template.json +13 -0
  92. package/templates/init/client/react/react_ts/package.json +11 -0
  93. package/templates/init/client/react/react_ts/template/README.md +46 -0
  94. package/templates/init/client/react/react_ts/template/client-package.json +5 -0
  95. package/templates/init/client/react/react_ts/template/gitignore +23 -0
  96. package/templates/init/client/react/react_ts/template/public/favicon.ico +0 -0
  97. package/templates/init/client/react/react_ts/template/public/index.html +43 -0
  98. package/templates/init/client/react/react_ts/template/public/logo192.png +0 -0
  99. package/templates/init/client/react/react_ts/template/public/logo512.png +0 -0
  100. package/templates/init/client/react/react_ts/template/public/manifest.json +25 -0
  101. package/templates/init/client/react/react_ts/template/public/robots.txt +3 -0
  102. package/templates/init/client/react/react_ts/template/src/App.css +38 -0
  103. package/templates/init/client/react/react_ts/template/src/App.test.tsx +9 -0
  104. package/templates/init/client/react/react_ts/template/src/App.tsx +26 -0
  105. package/templates/init/client/react/react_ts/template/src/index.css +13 -0
  106. package/templates/init/client/react/react_ts/template/src/index.tsx +17 -0
  107. package/templates/init/client/react/react_ts/template/src/logo.svg +1 -0
  108. package/templates/init/client/react/react_ts/template/src/reportWebVitals.ts +15 -0
  109. package/templates/init/client/react/react_ts/template/src/setupTests.ts +5 -0
  110. package/templates/init/client/react/react_ts/template.json +18 -0
  111. package/templates/web-socket.txt +21 -0
  112. package/lib/init/features/client.js +0 -50
  113. package/lib/serve/server/lib/client.js +0 -30
@@ -98,6 +98,13 @@ function setFnConfig(featureName, add) {
98
98
  break;
99
99
  case 'client':
100
100
  config.set('client.source', targetSource);
101
+ const plugin = runtime_store_1.default.get('payload.client.plugin', false);
102
+ if (plugin) {
103
+ config.set('client.plugin', plugin);
104
+ }
105
+ else {
106
+ config.unset('client.plugin');
107
+ }
101
108
  break;
102
109
  case 'apig':
103
110
  config.set('apig.rules', targetSource);
@@ -109,21 +116,21 @@ function setFnConfig(featureName, add) {
109
116
  exports.setFnConfig = setFnConfig;
110
117
  function functions(add = false) {
111
118
  return __awaiter(this, void 0, void 0, function* () {
112
- yield (yield Promise.resolve().then(() => __importStar(require('./functions')))).default();
119
+ yield (yield Promise.resolve().then(() => __importStar(require('./functions/index.js')))).default();
113
120
  setFnConfig('functions', add);
114
121
  });
115
122
  }
116
123
  exports.functions = functions;
117
124
  function client() {
118
125
  return __awaiter(this, void 0, void 0, function* () {
119
- yield (yield Promise.resolve().then(() => __importStar(require('./client')))).default();
126
+ yield (yield Promise.resolve().then(() => __importStar(require('./client/index.js')))).default();
120
127
  setFnConfig('client', false);
121
128
  });
122
129
  }
123
130
  exports.client = client;
124
131
  function project() {
125
132
  return __awaiter(this, void 0, void 0, function* () {
126
- yield (yield Promise.resolve().then(() => __importStar(require('./project')))).default();
133
+ yield (yield Promise.resolve().then(() => __importStar(require('./project.js')))).default();
127
134
  const projectPayload = runtime_store_1.default.get('payload.project', null);
128
135
  if (!projectPayload) {
129
136
  return;
@@ -58,7 +58,7 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
58
58
  const allProjects = (yield projectAPI.getAllProjects());
59
59
  if (allProjects.length === 0) {
60
60
  throw new error_1.default('Visit ' +
61
- ansi_colors_1.underline(constants_1.ORIGIN.admin) +
61
+ ansi_colors_1.underline(constants_1.ORIGIN.console) +
62
62
  ' for creation of your first project before you continue.', { exit: 1 });
63
63
  }
64
64
  const projectChoices = [
@@ -167,7 +167,10 @@ class Command {
167
167
  args.push({});
168
168
  }
169
169
  const [opts, command] = args.slice(-2);
170
- runtime_store_1.default.set('opts', Object.assign({ _name: command.name() }, opts));
170
+ const unknownOpts = this.allowUnknownOptions
171
+ ? command.parseOptions(process.argv).unknown
172
+ : undefined;
173
+ runtime_store_1.default.set('opts', Object.assign({ _name: command.name(), unknownOpts }, opts));
171
174
  this._prepare(command);
172
175
  try {
173
176
  yield migration_1.default(runtime_store_1.default.get('context.cli.package.version'));
@@ -70,7 +70,8 @@ class Config {
70
70
  catch (e) {
71
71
  const err = error_1.default.getErrorInstance(e, {
72
72
  message: 'There was an error loading a file',
73
- skipHelp: false
73
+ skipHelp: false,
74
+ fileName: __filename
74
75
  });
75
76
  err.errorId = 'CONFIG-1';
76
77
  err.arg = [ansi_colors_1.bold(constants_1.FILENAME.config), err.message];
@@ -31,34 +31,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- const path_1 = __importDefault(require("path"));
35
34
  const app_module_path_1 = __importDefault(require("app-module-path"));
36
- const logger_1 = require("./util_modules/logger");
37
35
  const project_1 = require("./util_modules/project");
38
- const global_dirs_1 = __importDefault(require("global-dirs"));
36
+ const global_space_1 = __importDefault(require("./util_modules/global-space"));
37
+ const path_1 = require("path");
39
38
  exports.default = (name) => __awaiter(void 0, void 0, void 0, function* () {
40
- try {
41
- if (name.startsWith('./')) {
42
- const projectModulePath = project_1.resolveProjectPath(name);
43
- app_module_path_1.default.addPath(projectModulePath);
44
- return Promise.resolve().then(() => __importStar(require(projectModulePath)));
45
- }
46
- else {
47
- for (const manager of ['npm', 'yarn']) {
48
- try {
49
- const packageName = path_1.default.join(global_dirs_1.default[manager].packages, name);
50
- app_module_path_1.default.addPath(packageName);
51
- return Promise.resolve().then(() => __importStar(require(packageName)));
52
- }
53
- catch (e) {
54
- logger_1.debug(e);
55
- }
56
- }
57
- return;
58
- }
39
+ if (name !== path_1.basename(name)) {
40
+ const projectModulePath = project_1.resolveProjectPath(name);
41
+ app_module_path_1.default.addPath(projectModulePath);
42
+ return Promise.resolve().then(() => __importStar(require(projectModulePath)));
59
43
  }
60
- catch (err) {
61
- logger_1.debug(err);
62
- return;
44
+ const packageName = yield global_space_1.default(name);
45
+ if (typeof packageName === 'string') {
46
+ app_module_path_1.default.addPath(packageName);
47
+ return Promise.resolve().then(() => __importStar(require(packageName)));
63
48
  }
49
+ return;
64
50
  });
@@ -31,6 +31,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
+ const ansi_colors_1 = require("ansi-colors");
35
+ const error_1 = __importDefault(require("./error"));
34
36
  const optional_import_1 = __importDefault(require("./optional-import"));
35
37
  const runtime_store_1 = __importDefault(require("./runtime-store"));
36
38
  const config = __importStar(require("./util_modules/config"));
@@ -57,10 +59,14 @@ function pluginLoader(feature, name, source) {
57
59
  pluginMod = loadedPlugins[specificPlugin];
58
60
  }
59
61
  else {
60
- pluginMod = (yield optional_import_1.default(specificPlugin));
62
+ pluginMod = yield optional_import_1.default(specificPlugin);
61
63
  }
62
64
  if (pluginMod === undefined) {
63
- return;
65
+ throw new error_1.default(`Configured plugin could not be found for [${feature}:${name}] - ${specificPlugin}`, {
66
+ exit: 1,
67
+ errorId: 'PLG-LDR-1',
68
+ arg: [ansi_colors_1.italic.red(specificPlugin), ansi_colors_1.bold(name), ansi_colors_1.bold(feature)]
69
+ });
64
70
  }
65
71
  loadedPlugins[specificPlugin] = pluginMod;
66
72
  if (typeof pluginMod === 'function') {
@@ -70,7 +70,8 @@ class PortResolver {
70
70
  if (searchSpan === 0) {
71
71
  const err = error_1.default.getErrorInstance(e, {
72
72
  message: `Provided port '${port}' is not available`,
73
- skipHelp: false
73
+ skipHelp: false,
74
+ fileName: __filename
74
75
  });
75
76
  err.errorId = 'PORT-RESOLVER-1';
76
77
  err.arg = [
@@ -84,7 +85,8 @@ class PortResolver {
84
85
  }
85
86
  const cerr = error_1.default.getErrorInstance(e, {
86
87
  message: `No ports from '${port}' to ${port + searchSpan} is free to use as a ${type} port.`,
87
- skipHelp: false
88
+ skipHelp: false,
89
+ fileName: __filename
88
90
  });
89
91
  cerr.errorId = 'PORT-RESOLVER-2';
90
92
  cerr.arg = [ansi_colors_1.bold(port + ''), ansi_colors_1.bold(port + searchSpan + ''), ansi_colors_1.bold('--' + type)];
@@ -54,7 +54,8 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
54
54
  runtime_store_1.default.set('context.port.http.master', masterPort);
55
55
  yield Promise.all(serveTargets.map((target) => features[target]()));
56
56
  const rawTargets = runtime_store_1.default.get('context.functions.targets', []);
57
- if (option_1.getOptionValue('watch', false)) {
57
+ const watchOpt = option_1.getOptionValue('watch', false);
58
+ if (watchOpt) {
58
59
  yield Promise.all(rawTargets.map((target) => __awaiter(void 0, void 0, void 0, function* () { return (target.valid ? fn_watcher_1.default(target) : undefined); })));
59
60
  }
60
61
  const funtionTargets = rawTargets.filter((target) => {
@@ -73,6 +74,10 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
73
74
  if (serveTargets.includes('client')) {
74
75
  const client = runtime_store_1.default.get('context.client', false);
75
76
  if (client && client.valid) {
77
+ client.opts = {
78
+ open: option_1.getOptionValue('open', false),
79
+ watch: watchOpt
80
+ };
76
81
  advancedServer.add('client', client);
77
82
  }
78
83
  else {
@@ -1,4 +1,23 @@
1
1
  'use strict';
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
2
21
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
22
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
23
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,6 +54,7 @@ const js_1 = require("../../util_modules/js");
35
54
  const logger_1 = require("../../util_modules/logger");
36
55
  const shell_1 = require("../../util_modules/shell");
37
56
  const master_1 = __importDefault(require("./lib/master"));
57
+ const ansi_colors_1 = require("ansi-colors");
38
58
  class Server {
39
59
  constructor() {
40
60
  this.targetDetailsArr = [];
@@ -85,8 +105,8 @@ class Server {
85
105
  opts.push(details.httpPort + '');
86
106
  const targetSource = (_c = details.target) === null || _c === void 0 ? void 0 : _c.source;
87
107
  const configType = details.type === 'server' ? 'functions' : details.type;
88
- const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource);
89
- if (servePlugin !== undefined) {
108
+ const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource).catch((err) => logger_1.debug(err));
109
+ if (typeof servePlugin === 'function') {
90
110
  const event = servePlugin(details, masterPort);
91
111
  logger_1.labeled(label, `Plugin : "${runtime_store_1.default.get(`context.${configType}.plugins.start${targetSource ? '.' + targetSource : ''}`)}" is used for ${details.type} serve`).MESSAGE();
92
112
  return event;
@@ -111,7 +131,7 @@ class Server {
111
131
  const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
112
132
  const _d = details.target, { ['watcher']: _ } = _d, serialisableTarget = __rest(_d, ['watcher']);
113
133
  const opts = [
114
- path_1.join(__dirname, 'lib', details.type === 'client' ? 'client.js' : 'node.js'),
134
+ path_1.join(__dirname, 'lib', 'node.js'),
115
135
  details.httpPort + '',
116
136
  JSON.stringify(serialisableTarget)
117
137
  ];
@@ -121,12 +141,28 @@ class Server {
121
141
  }
122
142
  const targetSource = (_b = details.target) === null || _b === void 0 ? void 0 : _b.source;
123
143
  const configType = details.type === 'server' ? 'functions' : details.type;
124
- const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource);
125
- if (servePlugin !== undefined) {
126
- const event = servePlugin(details, masterPort);
144
+ const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource).catch((err) => logger_1.debug(err));
145
+ if (typeof servePlugin === 'function') {
146
+ const event = servePlugin(details, masterPort).catch((e) => {
147
+ var _a, _b;
148
+ const err = error_1.default.getErrorInstance(e, {
149
+ fileName: __filename,
150
+ skipHelp: false
151
+ });
152
+ err.errorId = 'SERVE-IDX-1';
153
+ err.arg = [
154
+ configType,
155
+ ansi_colors_1.bold(((_b = (_a = details.target) === null || _a === void 0 ? void 0 : _a.plugins) === null || _b === void 0 ? void 0 : _b.start)[targetSource] || 'start'),
156
+ ansi_colors_1.bold('Error: ') + ansi_colors_1.italic.red(err.message)
157
+ ];
158
+ throw err;
159
+ });
127
160
  logger_1.labeled(label, `Plugin : "${runtime_store_1.default.get(`context.${configType}.plugins.start${targetSource ? '.' + targetSource : ''}`)}" is used for ${details.type} serve`).MESSAGE();
128
161
  return event;
129
162
  }
163
+ if (details.type === 'client') {
164
+ return yield (yield Promise.resolve().then(() => __importStar(require('./lib/web_client/index.js')))).default(details, masterPort);
165
+ }
130
166
  const child = shell_1.spawn('node', opts, {
131
167
  cwd: targetSource,
132
168
  stdio: 'pipe',
@@ -136,7 +172,7 @@ class Server {
136
172
  X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin
137
173
  }
138
174
  }).RAW();
139
- logger_1.labeled(label, `URL : http://localhost:${masterPort}/${details.type === 'client' ? 'app/' : 'server/' + ((_c = details.target) === null || _c === void 0 ? void 0 : _c.name) + '/'}`).MESSAGE();
175
+ logger_1.labeled(label, `URL : http://localhost:${masterPort}/server/${(_c = details.target) === null || _c === void 0 ? void 0 : _c.name}/`).MESSAGE();
140
176
  logger_1.info();
141
177
  return child;
142
178
  });
@@ -196,7 +232,15 @@ class Server {
196
232
  this.masterServer = server;
197
233
  return new Promise((res) => {
198
234
  server.on('listening', () => {
199
- this.targetDetailsArr.forEach((targetDetails) => { var _a; return (_a = targetDetails.process) === null || _a === void 0 ? void 0 : _a.emit('start'); });
235
+ this.targetDetailsArr.forEach((targetDetails) => {
236
+ var _a;
237
+ if (targetDetails.process &&
238
+ 'send' in targetDetails.process) {
239
+ targetDetails.process.send('start');
240
+ return;
241
+ }
242
+ (_a = targetDetails.process) === null || _a === void 0 ? void 0 : _a.emit('start');
243
+ });
200
244
  res();
201
245
  });
202
246
  });
@@ -82,6 +82,10 @@ const proxyResponseHandler = (systemRoutes) => (proxyRes, req, res) => {
82
82
  }
83
83
  }
84
84
  };
85
+ const generateConnectionId = () => {
86
+ const offset = Math.floor(Math.random() * 10);
87
+ return Date.now() + offset + '';
88
+ };
85
89
  function spinUpMaster(listenPort, otherServerDetails) {
86
90
  return __awaiter(this, void 0, void 0, function* () {
87
91
  const projectDetails = {
@@ -122,26 +126,36 @@ function spinUpMaster(listenPort, otherServerDetails) {
122
126
  app.use('/', apig_matcher_1.default, auth_checker_1.default, url_rewriter_1.default);
123
127
  }
124
128
  const proxy = http_proxy_1.createProxyServer()
125
- .on('error', (err, req) => {
129
+ .on('error', (err, req, res) => {
130
+ res.writeHead(503, { 'Content-Type': 'json' });
131
+ res.write(JSON.stringify({ error: 'unable to proxy request' }));
132
+ res.end();
126
133
  console.log('unable to proxy request ', req.url);
127
134
  console.error(err);
128
135
  })
129
- .on('proxyRes', proxyResponseHandler(systemRoutes));
136
+ .on('proxyRes', proxyResponseHandler(systemRoutes))
137
+ .on('proxyReq', (proxyReq) => {
138
+ const id = generateConnectionId();
139
+ connections[id] = proxyReq;
140
+ proxyReq.on('finish', () => {
141
+ delete connections[id];
142
+ });
143
+ })
144
+ .on('close', () => logger_2.debug('proxy closed'));
130
145
  otherServerDetails.forEach((serverDetail) => {
131
146
  var _a, _b, _c, _d, _e, _f;
132
147
  switch (serverDetail.type) {
133
148
  case 'client':
134
149
  const appTarget = `http://127.0.0.1:${serverDetail.httpPort}/app/`;
135
150
  app.use('/app', (req, res) => {
136
- var _a;
137
- if (req.url.startsWith('/local-redirect') &&
138
- ((_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.login_redirect)) {
139
- const isUrl = serverDetail.target.login_redirect.startsWith('/');
151
+ const target = serverDetail.target;
152
+ if (req.url.startsWith('/local-redirect') && target.login_redirect) {
153
+ const isUrl = target.login_redirect.startsWith('/');
140
154
  if (isUrl) {
141
- res.redirect(serverDetail.target.login_redirect);
155
+ res.redirect(target.login_redirect);
142
156
  }
143
157
  else {
144
- res.redirect('/app/' + serverDetail.target.login_redirect);
158
+ res.redirect('/app/' + target.login_redirect);
145
159
  }
146
160
  }
147
161
  else {
@@ -192,7 +206,8 @@ function spinUpMaster(listenPort, otherServerDetails) {
192
206
  ws: true
193
207
  });
194
208
  });
195
- app.use('/baas/v1/project/:projectId/function/' + ((_f = serverDetail.target) === null || _f === void 0 ? void 0 : _f.id), (req, res) => {
209
+ app.use('/baas/v1/project/:projectId/function/' +
210
+ ((_f = serverDetail.target) === null || _f === void 0 ? void 0 : _f.id), (req, res) => {
196
211
  var _a;
197
212
  proxy.web(req, res, {
198
213
  target: `http://127.0.0.1:${serverDetail.httpPort}/server/${(_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.id}`,
@@ -216,10 +231,11 @@ function spinUpMaster(listenPort, otherServerDetails) {
216
231
  console.error(err);
217
232
  });
218
233
  expressServer.maxConnections = 100;
219
- process.once('SIGINT', () => Object.keys(connections).forEach((sokcetId) => { var _a; return (_a = connections[sokcetId]) === null || _a === void 0 ? void 0 : _a.destroy(); }));
234
+ process.on('SIGINT', () => Object.keys(connections).forEach((connectionId) => {
235
+ connections[connectionId].destroy();
236
+ }));
220
237
  expressServer.on('connection', (socket) => {
221
- const offset = Math.floor(Math.random() * 10);
222
- const id = Date.now() + offset + '';
238
+ const id = generateConnectionId();
223
239
  connections[id] = socket;
224
240
  socket.on('close', () => {
225
241
  delete connections[id];
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const open_1 = __importDefault(require("open"));
16
+ const logger_1 = require("../../../../util_modules/logger");
17
+ const server_js_1 = __importDefault(require("./server.js"));
18
+ exports.default = (details, masterPort) => __awaiter(void 0, void 0, void 0, function* () {
19
+ var _a;
20
+ const target = details.target;
21
+ const clientListner = yield server_js_1.default(details.httpPort, target.source, target.homepage, ((_a = target.opts) === null || _a === void 0 ? void 0 : _a.watch) || false);
22
+ clientListner.addListener('start', () => {
23
+ var _a, _b;
24
+ const appUrl = 'http://localhost:' + masterPort;
25
+ const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
26
+ logger_1.labeled(label, `URL : ${appUrl}/app/`).MESSAGE();
27
+ ((_b = details.target.opts) === null || _b === void 0 ? void 0 : _b.open) && open_1.default(appUrl).catch();
28
+ });
29
+ return clientListner;
30
+ });
@@ -0,0 +1,171 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const express_1 = __importDefault(require("express"));
16
+ const utils_js_1 = require("./utils.js");
17
+ const chokidar_1 = require("chokidar");
18
+ const logger_js_1 = require("../../../../util_modules/logger.js");
19
+ const index_js_1 = require("../../../../util_modules/constants/index.js");
20
+ const error_js_1 = __importDefault(require("../../../../error.js"));
21
+ const index_js_2 = require("../../../../util_modules/fs/index.js");
22
+ const path_1 = require("path");
23
+ const ws_1 = require("ws");
24
+ const port_resolver_js_1 = __importDefault(require("../../../../port-resolver.js"));
25
+ const ansi_colors_1 = require("ansi-colors");
26
+ function prepareScriptFile(dir, port) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const scriptFile = yield index_js_2.ASYNC.readFile(index_js_1.TEMPLATE.client.basic.socket);
29
+ if (!scriptFile) {
30
+ throw new error_js_1.default('Unable to read the script file', { exit: 2 });
31
+ }
32
+ const scriptReady = scriptFile.replace(new RegExp(index_js_1.PLACEHOLDER.client.port, 'g'), port + '');
33
+ yield index_js_2.ASYNC.ensureDir(dir);
34
+ const scriptFilePath = path_1.join(dir, 'reload-script.js');
35
+ yield index_js_2.ASYNC.writeFile(scriptFilePath, scriptReady);
36
+ return scriptFilePath;
37
+ });
38
+ }
39
+ function stringInjector(source, anchorIdx, marker, inject) {
40
+ const headLastIdx = anchorIdx + marker.length;
41
+ const scriptPrefix = source.slice(0, headLastIdx);
42
+ const scriptSufix = source.slice(headLastIdx + 1);
43
+ return scriptPrefix + inject + scriptSufix;
44
+ }
45
+ function injectScript(scriptLocale, indexHtml) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const headIdx = indexHtml.search(/<head>/gi);
48
+ const scriptTag = `\n<!-- Catalyst web client reload script -->\n<script type="text/javascript" src="${scriptLocale}"></script>\n`;
49
+ if (headIdx !== -1) {
50
+ return stringInjector(indexHtml, headIdx, '<head>', scriptTag);
51
+ }
52
+ const htmlIdx = indexHtml.search(/<html>/gi);
53
+ const headTag = `\n<head>${scriptTag}</head>\n`;
54
+ return stringInjector(indexHtml, htmlIdx, '<html>', headTag);
55
+ });
56
+ }
57
+ function reloadApp(event, path, liveSockets) {
58
+ logger_js_1.info(ansi_colors_1.cyan(`${event} detected in ${path} of the Web-Client.`));
59
+ liveSockets.forEach((socket) => socket.send('reload'));
60
+ }
61
+ function webClientServer(httpPort, source, homepage, enableWatch) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const eventListner = new utils_js_1.WebClientEvent();
64
+ const catalystTempDir = path_1.join(source, '.catalyst');
65
+ const socketServerPort = yield port_resolver_js_1.default.getFreePort(httpPort, 20, true);
66
+ const reloadScriptSrc = enableWatch
67
+ ? path_1.relative(source, yield prepareScriptFile(catalystTempDir, socketServerPort))
68
+ : undefined;
69
+ if (reloadScriptSrc) {
70
+ const clientWatcher = chokidar_1.watch(source, {
71
+ ignoreInitial: true,
72
+ ignored: ['node_modules/**/*']
73
+ });
74
+ const liveSockets = [];
75
+ let watcherReady = false;
76
+ clientWatcher.on('error', (err) => {
77
+ watcherReady = false;
78
+ eventListner.emit('error', new error_js_1.default('client watcher error', {
79
+ original: err,
80
+ exit: 1,
81
+ skipHelp: true
82
+ }));
83
+ });
84
+ clientWatcher.on('ready', () => {
85
+ watcherReady = true;
86
+ const socketServer = new ws_1.WebSocketServer({
87
+ port: socketServerPort,
88
+ path: '/client-reload'
89
+ })
90
+ .on('connection', (clientSocket) => {
91
+ if (!watcherReady) {
92
+ clientSocket.send('watcher not ready');
93
+ clientSocket.close();
94
+ return;
95
+ }
96
+ clientSocket.on('close', () => {
97
+ const socketIdx = liveSockets.findIndex((socket) => socket === clientSocket);
98
+ liveSockets.splice(socketIdx, 1);
99
+ });
100
+ liveSockets.push(clientSocket);
101
+ clientSocket.send('connected');
102
+ })
103
+ .on('error', (err) => logger_js_1.debug(err));
104
+ setTimeout(() => {
105
+ clientWatcher.on('change', (path) => {
106
+ reloadApp('Change(s) is', path_1.relative(source, path), liveSockets);
107
+ });
108
+ clientWatcher.on('add', (path) => {
109
+ reloadApp('Addition of file(s) is', path_1.relative(source, path), liveSockets);
110
+ });
111
+ clientWatcher.on('unlink', (path) => {
112
+ reloadApp('Deletion of file(s) is', path_1.relative(source, path), liveSockets);
113
+ });
114
+ }, 1000);
115
+ eventListner.addListener('close', () => socketServer === null || socketServer === void 0 ? void 0 : socketServer.close());
116
+ });
117
+ eventListner.addListener('close', () => {
118
+ if (clientWatcher) {
119
+ clientWatcher.close();
120
+ }
121
+ liveSockets.forEach((socket) => socket.close());
122
+ index_js_2.SYNC.deleteDir(catalystTempDir);
123
+ });
124
+ }
125
+ const app = express_1.default();
126
+ app.use('/app', (req, res) => __awaiter(this, void 0, void 0, function* () {
127
+ const requestedFile = req.url === '/' ? homepage : decodeURIComponent(req.path);
128
+ if (enableWatch &&
129
+ requestedFile === homepage &&
130
+ (requestedFile.includes('.html') || requestedFile.includes('.htm'))) {
131
+ const homepageFile = yield index_js_2.ASYNC.readFile(path_1.join(source, homepage));
132
+ if (!homepageFile) {
133
+ eventListner.emit('error', new error_js_1.default('Unable to read the homepage file', {
134
+ exit: 1,
135
+ errorId: 'WEB-CLIENT-SERVER-1',
136
+ arg: [ansi_colors_1.red.italic(homepage)]
137
+ }));
138
+ return;
139
+ }
140
+ const injectedHomePage = reloadScriptSrc
141
+ ? yield injectScript(encodeURIComponent(reloadScriptSrc), homepageFile)
142
+ : undefined;
143
+ res.contentType('text/html');
144
+ res.status(200);
145
+ res.send(injectedHomePage || homepageFile);
146
+ return;
147
+ }
148
+ res.sendFile(path_1.join(source, requestedFile), (err) => {
149
+ if (err) {
150
+ logger_js_1.error('Unable to serve the requested file: ' + path_1.join(source, requestedFile));
151
+ logger_js_1.debug(err.stack || err.message);
152
+ res.status(404);
153
+ res.send('Resource not found');
154
+ }
155
+ });
156
+ }));
157
+ const server = app.listen(httpPort).on('error', (err) => {
158
+ eventListner.emit('error', err);
159
+ });
160
+ eventListner.on('error', (err) => {
161
+ logger_js_1.error('Error when serving the web-client: ' + err.message);
162
+ logger_js_1.info('Please exit the command to get more info');
163
+ eventListner.emit('close');
164
+ });
165
+ eventListner.on('close', () => {
166
+ server.close(() => logger_js_1.debug('client server closed'));
167
+ });
168
+ return new Promise((res) => server.on('listening', () => res(eventListner)));
169
+ });
170
+ }
171
+ exports.default = webClientServer;
@@ -0,0 +1,10 @@
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.WebClientEvent = void 0;
7
+ const events_1 = __importDefault(require("events"));
8
+ class WebClientEvent extends events_1.default {
9
+ }
10
+ exports.WebClientEvent = WebClientEvent;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CHAR = void 0;
4
4
  const ansi_colors_1 = require("ansi-colors");
5
5
  const env_1 = require("./env");
6
- const isSupported = !env_1.isWindows() || process.env.CI || process.env.TERM === 'xterm-256color';
6
+ const isSupported = !env_1.isWindows || process.env.CI || process.env.TERM === 'xterm-256color';
7
7
  exports.CHAR = {
8
8
  success: isSupported ? ansi_colors_1.bold.green('✔') : ansi_colors_1.bold.green('√'),
9
9
  warning: isSupported ? ansi_colors_1.bold.yellow('⚠') : ansi_colors_1.bold.yellow('‼'),
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.script = exports.enabled = exports.rules = exports.raw = void 0;
6
+ exports.source = exports.script = exports.enabled = exports.rules = exports.raw = void 0;
7
7
  const error_1 = __importDefault(require("../../../error"));
8
8
  const runtime_store_1 = __importDefault(require("../../../runtime-store"));
9
9
  const constants_1 = require("../../constants");
@@ -43,3 +43,4 @@ function script(name, fallback) {
43
43
  return (rawScripts && rawScripts[name]) || fallback;
44
44
  }
45
45
  exports.script = script;
46
+ exports.source = rules;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.IAC = exports.ORIGIN = exports.TEMPLATE = exports.SCOPE = exports.REMOTE_REF = exports.REGEX = exports.REFERENCE = exports.PLACEHOLDER = exports.INTEG = exports.FOLDERNAME = exports.FN_TYPE = exports.DC_TYPE = exports.FILENAME = exports.EVENT_SOURCE = exports.EVENT_REF = exports.DEFAULT = exports.CLIQ = exports.AUTH = exports.APIG_RULES = void 0;
6
+ exports.IAC = exports.PLUGIN = exports.ORIGIN = exports.TEMPLATE = exports.SCOPE = exports.REMOTE_REF = exports.REGEX = exports.REFERENCE = exports.PLACEHOLDER = exports.INTEG = exports.FOLDERNAME = exports.FN_TYPE = exports.DC_TYPE = exports.FILENAME = exports.EVENT_SOURCE = exports.EVENT_REF = exports.DEFAULT = exports.CLIQ = exports.AUTH = exports.APIG_RULES = void 0;
7
7
  var apig_rules_1 = require("./lib/apig-rules");
8
8
  Object.defineProperty(exports, "APIG_RULES", { enumerable: true, get: function () { return __importDefault(apig_rules_1).default; } });
9
9
  var auth_1 = require("./lib/auth");
@@ -40,5 +40,7 @@ var template_1 = require("./lib/template");
40
40
  Object.defineProperty(exports, "TEMPLATE", { enumerable: true, get: function () { return __importDefault(template_1).default; } });
41
41
  var urls_1 = require("./lib/urls");
42
42
  Object.defineProperty(exports, "ORIGIN", { enumerable: true, get: function () { return __importDefault(urls_1).default; } });
43
+ var plugin_1 = require("./lib/plugin");
44
+ Object.defineProperty(exports, "PLUGIN", { enumerable: true, get: function () { return __importDefault(plugin_1).default; } });
43
45
  var iac_1 = require("./lib/iac");
44
46
  Object.defineProperty(exports, "IAC", { enumerable: true, get: function () { return __importDefault(iac_1).default; } });