zcatalyst-cli 1.8.0 → 1.10.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/README.md +31 -10
  2. package/docs/command_needs/auth.toml +5 -0
  3. package/docs/commands/iac/export.toml +4 -0
  4. package/docs/commands/iac/import.toml +4 -0
  5. package/docs/commands/iac/status.toml +4 -0
  6. package/docs/commands/project/use.toml +6 -0
  7. package/docs/iac/status/bundle.toml +4 -0
  8. package/lib/api-timer.js +81 -0
  9. package/lib/apig-utils.js +16 -14
  10. package/lib/archiver.js +83 -45
  11. package/lib/authentication/login.js +4 -2
  12. package/lib/bin/catalyst.js +0 -1
  13. package/lib/client-utils.js +22 -22
  14. package/lib/client.js +0 -3
  15. package/lib/command_needs/auth.js +12 -3
  16. package/lib/command_needs/rc.js +12 -7
  17. package/lib/commands/apig/disable.js +1 -1
  18. package/lib/commands/apig/enable.js +1 -1
  19. package/lib/commands/apig/status.js +7 -5
  20. package/lib/commands/client/delete.js +3 -5
  21. package/lib/commands/client/setup.js +1 -1
  22. package/lib/commands/deploy.js +4 -2
  23. package/lib/commands/ds/export.js +4 -2
  24. package/lib/commands/ds/import.js +4 -2
  25. package/lib/commands/ds/status.js +146 -150
  26. package/lib/commands/event/generate/index.js +1 -1
  27. package/lib/commands/event/generate/integ.js +1 -1
  28. package/lib/commands/functions/add.js +1 -1
  29. package/lib/commands/functions/config.js +1 -1
  30. package/lib/commands/functions/delete.js +1 -1
  31. package/lib/commands/functions/setup.js +1 -1
  32. package/lib/commands/functions/shell.js +1 -1
  33. package/lib/commands/help.js +0 -22
  34. package/lib/commands/iac/export.js +85 -0
  35. package/lib/commands/iac/import.js +187 -0
  36. package/lib/commands/iac/pack.js +129 -0
  37. package/lib/commands/iac/status.js +63 -0
  38. package/lib/commands/index.js +4 -0
  39. package/lib/commands/init.js +34 -27
  40. package/lib/commands/login.js +1 -1
  41. package/lib/commands/logout.js +1 -1
  42. package/lib/commands/project/list.js +11 -7
  43. package/lib/commands/project/reset.js +1 -1
  44. package/lib/commands/project/use.js +42 -25
  45. package/lib/commands/pull.js +1 -1
  46. package/lib/commands/run.js +1 -1
  47. package/lib/commands/serve.js +6 -5
  48. package/lib/commands/token/generate.js +1 -1
  49. package/lib/commands/token/list.js +1 -1
  50. package/lib/commands/token/revoke.js +1 -1
  51. package/lib/commands/whoami.js +1 -1
  52. package/lib/deploy/features/functions/index.js +2 -1
  53. package/lib/deploy/index.js +3 -1
  54. package/lib/endpoints/lib/iac.js +134 -0
  55. package/lib/error.js +43 -9
  56. package/lib/errorOut.js +9 -2
  57. package/lib/express_middlewares/unknownReqProxy.js +18 -7
  58. package/lib/fn-utils/lib/common.js +32 -12
  59. package/lib/fn-utils/lib/java.js +8 -9
  60. package/lib/fn-utils/lib/node.js +7 -1
  61. package/lib/fn-watcher.js +67 -0
  62. package/lib/iac/status/bundle.js +82 -0
  63. package/lib/iac/status/deploy.js +74 -0
  64. package/lib/iac/status/util/index.js +26 -0
  65. package/lib/index.js +13 -19
  66. package/lib/init/features/client.js +2 -3
  67. package/lib/init/features/functions/languages/java.js +2 -5
  68. package/lib/init/features/functions/languages/node.js +2 -5
  69. package/lib/init/features/index.js +8 -3
  70. package/lib/init/features/project.js +70 -33
  71. package/lib/init/util/client.js +12 -0
  72. package/lib/init/util/functions.js +20 -0
  73. package/lib/init/util/project.js +35 -0
  74. package/lib/internal/api.js +6 -9
  75. package/lib/internal/command.js +3 -3
  76. package/lib/internal/config.js +6 -4
  77. package/lib/migration/index.js +10 -6
  78. package/lib/option-filter.js +4 -1
  79. package/lib/port-resolver.js +18 -16
  80. package/lib/prompt/index.js +92 -0
  81. package/lib/prompt/types/file-path.js +95 -0
  82. package/lib/pull/features/client.js +1 -1
  83. package/lib/pull/features/functions/index.js +5 -7
  84. package/lib/pull/index.js +4 -1
  85. package/lib/serve/features/index.js +3 -1
  86. package/lib/serve/index.js +8 -4
  87. package/lib/serve/server/index.js +134 -64
  88. package/lib/serve/server/lib/master.js +5 -4
  89. package/lib/shell/dependencies/http-functions.js +46 -21
  90. package/lib/shell/dependencies/local-function.js +17 -48
  91. package/lib/shell/index.js +9 -10
  92. package/lib/shell/prepare/index.js +1 -5
  93. package/lib/shell/prepare/languages/index.js +14 -3
  94. package/lib/track.js +3 -1
  95. package/lib/util_modules/constants/index.js +3 -1
  96. package/lib/util_modules/constants/lib/file-names.js +2 -1
  97. package/lib/util_modules/constants/lib/iac.js +8 -0
  98. package/lib/util_modules/constants/lib/regex.js +2 -1
  99. package/lib/util_modules/constants/lib/scopes.js +5 -0
  100. package/lib/util_modules/constants/project.js +6 -0
  101. package/lib/util_modules/contextHelp.js +25 -14
  102. package/lib/util_modules/env.js +2 -5
  103. package/lib/util_modules/fs/index.js +9 -1
  104. package/lib/util_modules/fs/lib/async.js +29 -22
  105. package/lib/util_modules/js.js +6 -0
  106. package/lib/util_modules/logger.js +11 -10
  107. package/lib/util_modules/parser/toml.js +5 -1
  108. package/lib/util_modules/project.js +34 -19
  109. package/lib/util_modules/shell.js +6 -5
  110. package/package.json +12 -7
  111. package/scripts/postInstall.js +31 -0
  112. package/templates/iacSuccess.html +391 -0
  113. package/lib/prompt.js +0 -49
@@ -8,6 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
11
22
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
24
  };
@@ -28,26 +39,26 @@ class Server {
28
39
  constructor() {
29
40
  this.targetDetailsArr = [];
30
41
  }
31
- _spinUpMaster(masterPort) {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
34
- const debugPort = parseInt(runtime_store_1.default.get('context.port.debug.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], '-1'), 10);
35
- if (!isNaN(httpPort)) {
36
- const fnTargets = runtime_store_1.default.get('context.functions.targets');
37
- fnTargets
38
- .filter((t) => t.url !== undefined && t.type === constants_1.FN_TYPE.basic)
39
- .map((t) => js_1.JS.omit(t, ['zip_stream', 'watcher', 'localFn']))
40
- .forEach((t) => {
41
- this.targetDetailsArr.push({
42
- type: 'functions',
43
- httpPort,
44
- debugPort,
45
- target: t
46
- });
42
+ _addBasicFnDetails() {
43
+ const fnTargets = runtime_store_1.default.get('context.functions.targets', []);
44
+ if (fnTargets.length === 0) {
45
+ logger_1.debug('No basic functions to add');
46
+ }
47
+ const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
48
+ const debugPort = parseInt(runtime_store_1.default.get('context.port.debug.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], '-1'), 10);
49
+ if (!isNaN(httpPort)) {
50
+ fnTargets
51
+ .filter((t) => t.url !== undefined && t.type === constants_1.FN_TYPE.basic)
52
+ .map((t) => js_1.JS.omit(t, ['zip_stream', 'localFn']))
53
+ .forEach((t) => {
54
+ this.targetDetailsArr.push({
55
+ type: 'functions',
56
+ httpPort,
57
+ debugPort,
58
+ target: t
47
59
  });
48
- }
49
- return master_1.default(masterPort, this.targetDetailsArr);
50
- });
60
+ });
61
+ }
51
62
  }
52
63
  _spinUpJavaServer(details, masterPort) {
53
64
  var _a, _b, _c, _d;
@@ -98,10 +109,11 @@ class Server {
98
109
  var _a, _b, _c;
99
110
  return __awaiter(this, void 0, void 0, function* () {
100
111
  const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
112
+ const _d = details.target, { ['watcher']: _ } = _d, serialisableTarget = __rest(_d, ['watcher']);
101
113
  const opts = [
102
114
  path_1.join(__dirname, 'lib', details.type === 'client' ? 'client.js' : 'node.js'),
103
115
  details.httpPort + '',
104
- JSON.stringify(details.target)
116
+ JSON.stringify(serialisableTarget)
105
117
  ];
106
118
  if (details.debugPort !== -1) {
107
119
  logger_1.labeled(label, 'you can attach your debugger at port : ' + details.debugPort).MESSAGE();
@@ -142,44 +154,49 @@ class Server {
142
154
  debugPort
143
155
  });
144
156
  }
157
+ startServer(details, masterPort) {
158
+ var _a;
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ if (details.target === undefined) {
161
+ return;
162
+ }
163
+ let currentProcess;
164
+ if ((_a = details.target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) {
165
+ currentProcess = yield this._spinUpJavaServer(details, masterPort);
166
+ }
167
+ else {
168
+ currentProcess = yield this._spinUpProcess(details, masterPort);
169
+ }
170
+ details.process = currentProcess;
171
+ const stdout = currentProcess.stdout;
172
+ if (stdout && typeof stdout.pipe === 'function') {
173
+ stdout.pipe(process.stdout);
174
+ }
175
+ const stderr = currentProcess.stderr;
176
+ if (stderr && typeof stderr.pipe === 'function') {
177
+ stderr.pipe(process.stderr);
178
+ }
179
+ return new Promise((res) => setTimeout(res, 100));
180
+ });
181
+ }
145
182
  start() {
146
183
  return __awaiter(this, void 0, void 0, function* () {
147
184
  const masterPort = runtime_store_1.default.get(`context.port.http.master`, -1);
148
185
  if (masterPort === -1) {
149
186
  throw new error_1.default('master port cannot be undefined', { exit: 2 });
150
187
  }
151
- yield Promise.all(this.targetDetailsArr.map((details) => __awaiter(this, void 0, void 0, function* () {
152
- var _a;
153
- if (details.target === undefined) {
154
- return;
155
- }
156
- let currentProcess;
157
- if ((_a = details.target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) {
158
- currentProcess = yield this._spinUpJavaServer(details, masterPort);
159
- }
160
- else {
161
- currentProcess = yield this._spinUpProcess(details, masterPort);
162
- }
163
- Server.process = [...Server.process, currentProcess];
164
- const stdout = currentProcess.stdout;
165
- if (stdout && typeof stdout.pipe === 'function') {
166
- stdout.pipe(process.stdout);
167
- }
168
- const stderr = currentProcess.stderr;
169
- if (stderr && typeof stderr.pipe === 'function') {
170
- stderr.pipe(process.stderr);
171
- }
172
- })));
173
- const masterServer = yield this._spinUpMaster(masterPort);
188
+ yield Promise.all(this.targetDetailsArr.map((details) => __awaiter(this, void 0, void 0, function* () { return this.startServer(details, masterPort); })));
189
+ this._addBasicFnDetails();
174
190
  if (this.targetDetailsArr.length === 0) {
175
- throw new error_1.default('master server started before other server', {
191
+ throw new error_1.default('Trying to start master server before other server', {
176
192
  exit: 2
177
193
  });
178
194
  }
179
- Server.process.push(masterServer);
195
+ const server = yield master_1.default(masterPort, this.targetDetailsArr);
196
+ this.masterServer = server;
180
197
  return new Promise((res) => {
181
- masterServer.on('listening', () => {
182
- Server.process.forEach((process) => process.emit('start'));
198
+ server.on('listening', () => {
199
+ this.targetDetailsArr.forEach((targetDetails) => { var _a; return (_a = targetDetails.process) === null || _a === void 0 ? void 0 : _a.emit('start'); });
183
200
  res();
184
201
  });
185
202
  });
@@ -187,35 +204,88 @@ class Server {
187
204
  }
188
205
  wait() {
189
206
  return __awaiter(this, void 0, void 0, function* () {
190
- if (Server.process.length === 0) {
191
- logger_1.debug('The server is not started yet.');
207
+ if (!this.masterServer) {
208
+ logger_1.debug('Master server is not started yet.');
192
209
  throw new error_1.default('Server cannot be put to wait state.', { exit: 2 });
193
210
  }
211
+ this.targetDetailsArr.forEach((targetdetails) => {
212
+ var _a, _b;
213
+ const target = targetdetails.target;
214
+ if (targetdetails.type === 'functions' || !target || !targetdetails.process) {
215
+ return;
216
+ }
217
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.on('preparing', () => __awaiter(this, void 0, void 0, function* () {
218
+ yield this.kill(targetdetails.process);
219
+ }));
220
+ (_b = target.watcher) === null || _b === void 0 ? void 0 : _b.on('compiled', () => __awaiter(this, void 0, void 0, function* () {
221
+ yield this.restart(target);
222
+ logger_1.labeled(`server[${target.name}]`, 'ready!').MESSAGE();
223
+ setTimeout(() => {
224
+ var _a;
225
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.emit('next');
226
+ }, 1000);
227
+ }));
228
+ }, this);
194
229
  return new Promise((res) => {
195
230
  ['SIGINT', 'SIGTERM'].forEach((sig) => process.on(sig, res));
196
231
  });
197
232
  });
198
233
  }
199
- stop() {
234
+ kill(serverProcess) {
200
235
  return __awaiter(this, void 0, void 0, function* () {
201
- yield Promise.all(Server.process.map((serverProcess) => {
202
- return new Promise((resolve) => {
203
- if (serverProcess.pid === undefined) {
204
- try {
205
- serverProcess.close(() => resolve());
206
- }
207
- catch (e) {
208
- serverProcess.emit('close');
209
- }
236
+ return new Promise((resolve) => {
237
+ if (serverProcess.pid === undefined) {
238
+ try {
239
+ serverProcess.close(() => resolve());
210
240
  }
211
- else {
212
- serverProcess.kill('SIGINT');
241
+ catch (e) {
242
+ serverProcess.emit('close');
213
243
  }
214
- resolve();
244
+ }
245
+ if (serverProcess.exitCode === null) {
246
+ serverProcess.kill('SIGINT');
247
+ }
248
+ resolve();
249
+ });
250
+ });
251
+ }
252
+ restart(target) {
253
+ return __awaiter(this, void 0, void 0, function* () {
254
+ const targetIdx = this.targetDetailsArr.findIndex((targetDetails) => { var _a; return target.source === ((_a = targetDetails.target) === null || _a === void 0 ? void 0 : _a.source); });
255
+ if (targetIdx === -1) {
256
+ throw new error_1.default('Unknown target given for restart.', { exit: 2 });
257
+ }
258
+ if (this.targetDetailsArr[targetIdx].type === 'functions') {
259
+ return;
260
+ }
261
+ const targetProcess = this.targetDetailsArr[targetIdx].process;
262
+ if (!targetProcess) {
263
+ throw new error_1.default('Process is not defined for this target.', { exit: 2 });
264
+ }
265
+ yield this.kill(targetProcess);
266
+ yield this.startServer(this.targetDetailsArr[targetIdx], runtime_store_1.default.get(`context.port.http.master`, -1));
267
+ });
268
+ }
269
+ stop() {
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ yield Promise.all(this.targetDetailsArr.map((targetdetails) => __awaiter(this, void 0, void 0, function* () {
272
+ var _a;
273
+ const target = targetdetails.target;
274
+ if (!target) {
275
+ return;
276
+ }
277
+ yield ((_a = target.watcher) === null || _a === void 0 ? void 0 : _a.close());
278
+ if (targetdetails.process) {
279
+ return this.kill(targetdetails.process);
280
+ }
281
+ })));
282
+ if (this.masterServer) {
283
+ const masterServer = this.masterServer;
284
+ return new Promise((res, rej) => {
285
+ masterServer.close((err) => (err ? rej(err) : res()));
215
286
  });
216
- }));
287
+ }
217
288
  });
218
289
  }
219
290
  }
220
- Server.process = [];
221
291
  exports.default = Server;
@@ -29,6 +29,7 @@ const constants_1 = require("../../../util_modules/constants");
29
29
  const js_1 = require("../../../util_modules/js");
30
30
  const logger_2 = require("../../../util_modules/logger");
31
31
  const project_2 = require("../../../util_modules/project");
32
+ const option_1 = require("../../../util_modules/option");
32
33
  const removeSecure = (str) => str.replace(/;\s?Secure/i, '').replace(/;\s?SameSite=None/i, '');
33
34
  const redirectByAuth = (req, res, redirectUrl) => {
34
35
  if (req.headers['x-zc-user-cred-token'] === undefined) {
@@ -90,11 +91,11 @@ function spinUpMaster(listenPort, otherServerDetails) {
90
91
  key: project_2.getDomainKey(),
91
92
  env_name: project_2.getEnvName()
92
93
  };
93
- const bindPort = parseInt(runtime_store_1.default.get('context.port.bind', '-1'), 10);
94
94
  const rules = runtime_store_1.default.get('context.apig.local.config', -1);
95
+ const customProxyUrl = option_1.getOptionValue('proxy');
95
96
  const app = express_1.default();
96
- let systemRoutes;
97
97
  const connections = {};
98
+ let systemRoutes;
98
99
  app.use(logger_1.default);
99
100
  app.use(cookie_parser_1.default);
100
101
  app.use(authenticator_1.default);
@@ -206,7 +207,7 @@ function spinUpMaster(listenPort, otherServerDetails) {
206
207
  });
207
208
  }
208
209
  });
209
- app.use('/', unknownReqProxy_1.default(proxy, { listenPort, bindPort, systemRules: systemRoutes }));
210
+ app.use('/', unknownReqProxy_1.default(proxy, customProxyUrl));
210
211
  app.use(error_handler_1.default);
211
212
  const expressServer = app.listen(listenPort).on('error', (err) => {
212
213
  if (proxy) {
@@ -215,7 +216,7 @@ function spinUpMaster(listenPort, otherServerDetails) {
215
216
  console.error(err);
216
217
  });
217
218
  expressServer.maxConnections = 100;
218
- process.once('SIGINT', () => Object.keys(connections).forEach((sokcetId) => connections[sokcetId].destroy()));
219
+ process.once('SIGINT', () => Object.keys(connections).forEach((sokcetId) => { var _a; return (_a = connections[sokcetId]) === null || _a === void 0 ? void 0 : _a.destroy(); }));
219
220
  expressServer.on('connection', (socket) => {
220
221
  const offset = Math.floor(Math.random() * 10);
221
222
  const id = Date.now() + offset + '';
@@ -30,6 +30,9 @@ const js_1 = require("../../util_modules/js");
30
30
  const logger_2 = require("../../util_modules/logger");
31
31
  const project_2 = require("../../util_modules/project");
32
32
  const shell_1 = require("../../util_modules/shell");
33
+ const logUrl = (name, pthName, httpPort, masterPort) => {
34
+ logger_2.labeled(`functions(${name})`, 'URL => http://localhost:' + (masterPort === -1 ? httpPort : masterPort) + pthName).MESSAGE();
35
+ };
33
36
  class HttpFunctions {
34
37
  constructor(repl) {
35
38
  this.repl = repl;
@@ -44,6 +47,7 @@ class HttpFunctions {
44
47
  this.responseFile = path_1.join(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_res_body');
45
48
  this.metaFile = path_1.join(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_meta.json');
46
49
  this.runningSlaves = [];
50
+ this.targets = [];
47
51
  this.q = new better_queue_1.default((opts, cb) => {
48
52
  var _a, _b;
49
53
  fs_1.SYNC.ensureFile(this.responseFile, true);
@@ -67,7 +71,7 @@ class HttpFunctions {
67
71
  }
68
72
  }).RAW();
69
73
  (_a = slave.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
70
- logger_2.info(data);
74
+ logger_2.info(Buffer.isBuffer(data) ? data.toString() : data);
71
75
  });
72
76
  (_b = slave.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (data) => {
73
77
  const errorStr = data.toString().trim();
@@ -106,6 +110,12 @@ class HttpFunctions {
106
110
  return __awaiter(this, void 0, void 0, function* () {
107
111
  const debugPort = parseInt(runtime_store_1.default.get('context.port.debug.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], '-1'), 10);
108
112
  const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
113
+ if (this.repl !== undefined) {
114
+ this.app.use((_req, _res, next) => {
115
+ shell_1.clearLine(process.stdout);
116
+ next();
117
+ });
118
+ }
109
119
  this.app.use(logger_1.default);
110
120
  this.app.use(cookie_parser_1.default);
111
121
  this.app.use(authenticator_1.default);
@@ -225,10 +235,13 @@ class HttpFunctions {
225
235
  };
226
236
  shell_1.clearLine(process.stdout);
227
237
  const masterPort = runtime_store_1.default.get(`context.port.http.master`, -1);
228
- const fnTargets = runtime_store_1.default.get('context.functions.targets');
238
+ const fnTargets = runtime_store_1.default.get('context.functions.targets', []);
229
239
  fnTargets
230
240
  .filter((t) => t.url !== undefined && t.type === constants_1.FN_TYPE.basic)
231
- .map((t) => js_1.JS.omit(t, ['zip_stream', 'watcher', 'localFn']))
241
+ .map((t) => {
242
+ this.targets.push(t);
243
+ return js_1.JS.omit(t, ['zip_stream', 'watcher', 'localFn']);
244
+ })
232
245
  .forEach((t) => {
233
246
  var _a;
234
247
  if ((_a = t.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) {
@@ -237,9 +250,7 @@ class HttpFunctions {
237
250
  const pthName = new url_1.URL(t.url).pathname;
238
251
  if (pthName !== null) {
239
252
  this.app.use(pthName, reqHandler(t));
240
- logger_2.labeled(`functions(${t.name})`, 'URL => http://localhost:' +
241
- (masterPort === -1 ? httpPort : masterPort) +
242
- pthName).MESSAGE();
253
+ logUrl(t.name, pthName, httpPort, masterPort);
243
254
  }
244
255
  if (t.url_with_id !== undefined) {
245
256
  const pathWithId = new url_1.URL(t.url_with_id).pathname;
@@ -250,6 +261,26 @@ class HttpFunctions {
250
261
  });
251
262
  return new Promise((res) => {
252
263
  const server = this.app.listen(httpPort, () => {
264
+ this.targets.forEach((target) => {
265
+ var _a, _b;
266
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.on('preparing', () => {
267
+ if (this.repl && !this.repl.paused) {
268
+ this.repl.pause();
269
+ }
270
+ });
271
+ (_b = target.watcher) === null || _b === void 0 ? void 0 : _b.on('compiled', () => {
272
+ logger_2.labeled(`functions(${target.name})`, 'ready!').MESSAGE();
273
+ logUrl(target.name, new url_1.URL(target.url).pathname, httpPort, masterPort);
274
+ if (this.repl && this.repl.paused) {
275
+ this.repl.resume();
276
+ this.repl.showPrompt();
277
+ }
278
+ setTimeout(() => {
279
+ var _a;
280
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.emit('next');
281
+ }, 1000);
282
+ });
283
+ });
253
284
  if (this.repl !== undefined) {
254
285
  this.repl.showPrompt();
255
286
  }
@@ -271,28 +302,22 @@ class HttpFunctions {
271
302
  }
272
303
  stop() {
273
304
  return __awaiter(this, void 0, void 0, function* () {
274
- return new Promise((resolve) => {
305
+ return new Promise((res) => {
275
306
  this.runningSlaves.forEach((slave) => {
276
307
  slave.kill('SIGINT');
277
308
  });
278
- this.q.destroy(() => {
309
+ this.q.destroy(() => __awaiter(this, void 0, void 0, function* () {
310
+ yield Promise.all(this.targets.map((target) => __awaiter(this, void 0, void 0, function* () { var _a; return (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.close(); })));
279
311
  if (HttpFunctions.server !== null) {
280
- if (HttpFunctions.server.pid === undefined) {
281
- try {
282
- HttpFunctions.server.close(() => resolve());
283
- }
284
- catch (e) {
285
- HttpFunctions.server.emit('close');
286
- }
287
- }
288
- else {
289
- HttpFunctions.server.kill('SIGINT');
290
- }
312
+ HttpFunctions.server.close((err) => {
313
+ logger_2.debug(err);
314
+ res();
315
+ });
291
316
  }
292
317
  else {
293
- resolve();
318
+ res();
294
319
  }
295
- });
320
+ }));
296
321
  });
297
322
  });
298
323
  }
@@ -24,26 +24,14 @@ const js_1 = require("../../util_modules/js");
24
24
  const logger_1 = require("../../util_modules/logger");
25
25
  const project_1 = require("../../util_modules/project");
26
26
  const shell_1 = require("../../util_modules/shell");
27
- const languages_1 = require("../prepare/languages");
28
- function prepareTarget(target) {
29
- var _a, _b;
30
- return __awaiter(this, void 0, void 0, function* () {
31
- if ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) {
32
- return languages_1.java([target]);
33
- }
34
- if ((_b = target.stack) === null || _b === void 0 ? void 0 : _b.startsWith(runtime_1.default.language.nodejs)) {
35
- return languages_1.node([target]);
36
- }
37
- throw new error_1.default('Unknown stack received', { exit: 2 });
38
- });
39
- }
40
27
  class LocalFunction {
41
28
  constructor(repl, target) {
42
- var _a;
29
+ var _a, _b, _c;
43
30
  if (target.type === undefined || !Object.values(constants_1.FN_TYPE).includes(target.type)) {
44
31
  throw new error_1.default('target type is not defined', { exit: 2 });
45
32
  }
46
33
  this.repl = repl;
34
+ this.watcher = target.watcher;
47
35
  this.target = js_1.JS.omit(target, ['zip_stream', 'watcher']);
48
36
  this.slave = null;
49
37
  this.nodeInvoker = path_1.normalize(path_1.join(__dirname, './invoker', target.type, 'node.js'));
@@ -54,38 +42,22 @@ class LocalFunction {
54
42
  this.responseFile = path_1.join(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_res_body');
55
43
  this.metaFile = path_1.join(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_meta.json');
56
44
  this.call = this._call.bind(this);
57
- if (target.watcher) {
58
- this.watcher = target.watcher;
59
- target.watcher.on('all', (eventName, pth) => __awaiter(this, void 0, void 0, function* () {
45
+ (_b = this.watcher) === null || _b === void 0 ? void 0 : _b.on('preparing', () => {
46
+ if (!this.repl.paused) {
60
47
  this.repl.pause();
61
- shell_1.clearLine(process.stdout);
62
- logger_1.info(`\n${eventName} of ${pth} in ${target.name} detected. Preparing your function, please wait...`);
63
- const finalTargets = yield prepareTarget(target);
64
- if (finalTargets === undefined || finalTargets.length === 0) {
65
- logger_1.warning('target [' +
66
- target.name +
67
- '] is not a valid one reason : ' +
68
- target.failure_reason);
69
- logger_1.info('Waiting for correction..');
70
- if (this.exitTimer) {
71
- this.exitTimer.refresh();
72
- }
73
- else {
74
- this.exitTimer = setTimeout(() => {
75
- logger_1.info('\n(To exit, press ^C)');
76
- }, 2000);
77
- }
78
- }
79
- else {
80
- logger_1.labeled(`functions(${target.name})`, 'ready!').MESSAGE();
81
- if (this.exitTimer) {
82
- clearTimeout(this.exitTimer);
83
- }
84
- this.repl.resume();
85
- this.repl.showPrompt();
86
- }
87
- }));
88
- }
48
+ }
49
+ });
50
+ (_c = this.watcher) === null || _c === void 0 ? void 0 : _c.on('compiled', () => {
51
+ logger_1.labeled(`functions(${target.name})`, 'ready!').MESSAGE();
52
+ if (this.repl.paused) {
53
+ repl.resume();
54
+ repl.showPrompt();
55
+ }
56
+ setTimeout(() => {
57
+ var _a;
58
+ (_a = this.watcher) === null || _a === void 0 ? void 0 : _a.emit('next');
59
+ }, 1000);
60
+ });
89
61
  }
90
62
  _call(rawData = {}) {
91
63
  let data;
@@ -236,9 +208,6 @@ class LocalFunction {
236
208
  if (this.slave) {
237
209
  this.slave.kill('SIGINT');
238
210
  }
239
- if (this.exitTimer) {
240
- clearTimeout(this.exitTimer);
241
- }
242
211
  if (this.watcher) {
243
212
  this.watcher.close().then(res).catch(res);
244
213
  }
@@ -12,7 +12,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const chokidar_1 = require("chokidar");
16
15
  const path_1 = require("path");
17
16
  const util_1 = require("util");
18
17
  const repl_server_1 = __importDefault(require("../repl-server"));
@@ -27,6 +26,7 @@ const option_1 = require("../util_modules/option");
27
26
  const http_functions_1 = __importDefault(require("./dependencies/http-functions"));
28
27
  const local_function_1 = __importDefault(require("./dependencies/local-function"));
29
28
  const prepare_1 = __importDefault(require("./prepare"));
29
+ const fn_watcher_1 = __importDefault(require("../fn-watcher"));
30
30
  exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
31
31
  const replServer = new repl_server_1.default({
32
32
  prompt: 'functions > ',
@@ -46,7 +46,9 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
46
46
  const httpServer = new http_functions_1.default(replServer);
47
47
  try {
48
48
  yield prepare_1.default([constants_1.FN_TYPE.basic, constants_1.FN_TYPE.cron, constants_1.FN_TYPE.event, constants_1.FN_TYPE.integration]);
49
- const targets = runtime_store_1.default.get('context.functions.targets', []).filter((target) => {
49
+ const targets = runtime_store_1.default
50
+ .get('context.functions.targets', [])
51
+ .filter((target) => {
50
52
  if (!target.valid) {
51
53
  logger_1.warning('target [' +
52
54
  target.name +
@@ -62,18 +64,15 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
62
64
  });
63
65
  }
64
66
  replServer.start();
65
- targets.forEach((target) => {
66
- if (option_1.getOptionValue('watch', false)) {
67
- target.watcher = chokidar_1.watch(target.source, {
68
- ignored: /(^|[\/\\])\../,
69
- cwd: target.source,
70
- ignoreInitial: true
71
- });
67
+ const watchOpt = option_1.getOptionValue('watch', false);
68
+ yield Promise.all(targets.map((target) => __awaiter(void 0, void 0, void 0, function* () {
69
+ if (watchOpt) {
70
+ yield fn_watcher_1.default(target);
72
71
  }
73
72
  const localFn = new local_function_1.default(replServer, target);
74
73
  replServer.setContext(target.name, localFn.call);
75
74
  target.localFn = localFn;
76
- });
75
+ })));
77
76
  if (runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], false)) {
78
77
  yield httpServer.start();
79
78
  }
@@ -16,7 +16,6 @@ const fn_utils_1 = require("../../fn-utils");
16
16
  const common_1 = require("../../fn-utils/lib/common");
17
17
  const runtime_store_1 = __importDefault(require("../../runtime-store"));
18
18
  const constants_1 = require("../../util_modules/constants");
19
- const runtime_1 = __importDefault(require("../../util_modules/constants/lib/runtime"));
20
19
  const languages_1 = require("./languages");
21
20
  exports.default = (fnTypes) => __awaiter(void 0, void 0, void 0, function* () {
22
21
  fn_utils_1.fnUtils.common.executeHook({ prefix: 'pre', command: 'serve' });
@@ -33,10 +32,7 @@ exports.default = (fnTypes) => __awaiter(void 0, void 0, void 0, function* () {
33
32
  return target;
34
33
  });
35
34
  runtime_store_1.default.set('context.functions.targets', refinedTargets);
36
- yield Promise.all([
37
- languages_1.node(refinedTargets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.nodejs)) && target.valid; })),
38
- languages_1.java(refinedTargets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) && target.valid; }))
39
- ]);
35
+ yield languages_1.prepareFunctions(refinedTargets);
40
36
  yield common_1.resolveAllFnPorts(refinedTargets);
41
37
  return refinedTargets;
42
38
  });
@@ -27,17 +27,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
27
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
28
  });
29
29
  };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
30
33
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.java = exports.node = void 0;
34
+ exports.prepareFunctions = void 0;
35
+ const runtime_1 = __importDefault(require("../../../util_modules/constants/lib/runtime"));
32
36
  function node(targets) {
33
37
  return __awaiter(this, void 0, void 0, function* () {
34
38
  return (yield Promise.resolve().then(() => __importStar(require('./node')))).default(targets);
35
39
  });
36
40
  }
37
- exports.node = node;
38
41
  function java(targets) {
39
42
  return __awaiter(this, void 0, void 0, function* () {
40
43
  return (yield Promise.resolve().then(() => __importStar(require('./java')))).default(targets);
41
44
  });
42
45
  }
43
- exports.java = java;
46
+ function prepareFunctions(targets) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ return Promise.all([
49
+ node(targets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.nodejs)) && target.valid; })),
50
+ java(targets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) && target.valid; }))
51
+ ]);
52
+ });
53
+ }
54
+ exports.prepareFunctions = prepareFunctions;
package/lib/track.js CHANGED
@@ -16,6 +16,7 @@ const path_1 = require("path");
16
16
  const readline_1 = require("readline");
17
17
  const config_store_1 = __importDefault(require("./config-store"));
18
18
  const endpoints_1 = require("./endpoints");
19
+ const error_1 = __importDefault(require("./error"));
19
20
  const runtime_store_1 = __importDefault(require("./runtime-store"));
20
21
  const fs_1 = require("./util_modules/fs");
21
22
  const logger_1 = require("./util_modules/logger");
@@ -52,7 +53,8 @@ exports.default = (event, message, duration) => __awaiter(void 0, void 0, void 0
52
53
  yield httpLogger.log(logInput);
53
54
  }
54
55
  }
55
- catch (err) {
56
+ catch (e) {
57
+ const err = error_1.default.getErrorInstance(e);
56
58
  logger_1.debug('Unable to send debug log to server: ' + err.message);
57
59
  }
58
60
  });