@swell/cli 2.0.1-alpha.12 → 2.0.1-alpha.16

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 (49) hide show
  1. package/dist/commands/app/install.d.ts +5 -2
  2. package/dist/commands/app/install.js +41 -38
  3. package/dist/commands/app/push.d.ts +3 -3
  4. package/dist/commands/app/push.js +46 -49
  5. package/dist/commands/app/version.js +3 -3
  6. package/dist/commands/create/app.d.ts +8 -8
  7. package/dist/commands/create/app.js +74 -71
  8. package/dist/commands/create/content.d.ts +2 -2
  9. package/dist/commands/create/content.js +10 -10
  10. package/dist/commands/create/function.d.ts +1 -1
  11. package/dist/commands/create/function.js +20 -20
  12. package/dist/commands/create/model.d.ts +2 -2
  13. package/dist/commands/create/model.js +11 -11
  14. package/dist/commands/create/notification.d.ts +2 -2
  15. package/dist/commands/create/notification.js +36 -38
  16. package/dist/commands/login.d.ts +1 -1
  17. package/dist/commands/login.js +8 -7
  18. package/dist/commands/logs.js +2 -0
  19. package/dist/create-collection-command.d.ts +6 -6
  20. package/dist/create-collection-command.js +19 -19
  21. package/dist/create-command.js +6 -4
  22. package/dist/lib/api.d.ts +3 -2
  23. package/dist/lib/api.js +11 -8
  24. package/dist/lib/app-config.d.ts +1 -0
  25. package/dist/lib/app-config.js +5 -3
  26. package/dist/lib/apps/index.d.ts +1 -1
  27. package/dist/lib/apps/index.js +1 -1
  28. package/dist/lib/apps/paths.d.ts +1 -1
  29. package/dist/lib/apps/paths.js +1 -1
  30. package/dist/lib/bundle.js +15 -3
  31. package/dist/lib/constants.js +2 -3
  32. package/dist/lib/create/content.d.ts +1 -1
  33. package/dist/lib/create/content.js +1 -1
  34. package/dist/lib/create/function.d.ts +2 -2
  35. package/dist/lib/create/function.js +2 -2
  36. package/dist/lib/create/index.d.ts +1 -1
  37. package/dist/lib/create/index.js +6 -6
  38. package/dist/lib/create/model.d.ts +2 -2
  39. package/dist/lib/create/notification.d.ts +2 -2
  40. package/dist/lib/logs/index.js +1 -1
  41. package/dist/lib/sessions.js +2 -2
  42. package/dist/lib/stores.d.ts +4 -1
  43. package/dist/lib/stores.js +6 -3
  44. package/dist/lib/swell-function-wrapper.d.ts +40 -1
  45. package/dist/lib/swell-function-wrapper.js +107 -22
  46. package/dist/remote-app-command.d.ts +2 -2
  47. package/dist/remote-app-command.js +18 -18
  48. package/oclif.manifest.json +60 -57
  49. package/package.json +2 -1
@@ -1,9 +1,16 @@
1
1
  "use strict";
2
2
  const origialConsoleLog = console.log;
3
3
  addEventListener('fetch', (event) => {
4
- event.respondWith(handleRequest(event.request, event.env, event));
4
+ event.respondWith(request(event.request, event.env, event));
5
5
  });
6
- async function handleRequest(originalRequest, _env, context) {
6
+ /**
7
+ * Handle a function request.
8
+ * @param {Request} originalRequest from cloudflare
9
+ * @param {*} _env cloudflare environment vars
10
+ * @param {*} context cloudflare conttext
11
+ * @returns {Promise<SwellResponse>}
12
+ */
13
+ async function request(originalRequest, _env, context) {
7
14
  const req = new SwellRequest(originalRequest, context);
8
15
  await req.initialize();
9
16
  let response;
@@ -12,16 +19,10 @@ async function handleRequest(originalRequest, _env, context) {
12
19
  }
13
20
  catch (err) {
14
21
  // Log the error for Swell
15
- console.error(err.stack);
22
+ console.error(err);
16
23
  response = new SwellResponse({ error: err.message }, { status: err.status || 500 });
17
24
  }
18
- // Create a response by default if one is not returned
19
- if (!(response instanceof Response)) {
20
- response = new SwellResponse(response);
21
- }
22
- // Send logs back to Swell
23
- context.waitUntil(req.ingestLogs(response));
24
- return response;
25
+ return SwellResponse._respond(req, response, context);
25
26
  }
26
27
  /**
27
28
  * Invokes one of the function types that are available.
@@ -74,6 +75,9 @@ async function executeModuleHandler(req, context) {
74
75
  }
75
76
  throw new Error(`Function does not export a method to handle ${method.toUpperCase()} requests`);
76
77
  }
78
+ /**
79
+ * Class representing a Swell request.
80
+ */
77
81
  class SwellRequest {
78
82
  constructor(req, context) {
79
83
  this.originalRequest = req;
@@ -141,11 +145,11 @@ class SwellRequest {
141
145
  origialConsoleLog(...line);
142
146
  this._logs.push({
143
147
  date: Date.now(),
144
- line: line.map((l) => JSON.stringify(l)),
148
+ line: line.map((l) => (l instanceof Error ? l.stack : JSON.stringify(l))),
145
149
  ...(level !== 'info' ? { level } : {}),
146
150
  });
147
151
  }
148
- async ingestLogs(response) {
152
+ getIngestableLogs(response) {
149
153
  if (!this.logParams) {
150
154
  return;
151
155
  }
@@ -153,23 +157,51 @@ class SwellRequest {
153
157
  this.logParams.time = Date.now() - this.logParams.$start;
154
158
  delete this.logParams.$start;
155
159
  }
156
- const result = await this.swell.post('/:logs', {
157
- $ingest_function_logs: {
158
- params: {
159
- ...this.logParams,
160
- message: {
161
- ...this.logParams?.message,
162
- logs: this._logs,
163
- status: response.status,
164
- },
160
+ return {
161
+ params: {
162
+ ...this.logParams,
163
+ message: {
164
+ ...this.logParams?.message,
165
+ logs: this._logs,
166
+ status: response.status,
165
167
  },
166
168
  },
169
+ };
170
+ }
171
+ async ingestLogs(response) {
172
+ const ingestableLogs = this.getIngestableLogs(response);
173
+ if (!ingestableLogs) {
174
+ return;
175
+ }
176
+ const result = await this.swell.post('/:logs', {
177
+ $ingest_function_logs: ingestableLogs,
167
178
  });
168
179
  if (!result?.success) {
169
180
  console.error('Error ingesting logs', result);
170
181
  }
171
182
  }
183
+ /**
184
+ * Merge values into app data for the current request.
185
+ * @param {object|string} idOrValues string to indicate app ID, or values to merge
186
+ * @param {object|undefined} values values to merge into app data
187
+ * @returns {object|undefined} existing app data merged with values if passed
188
+ */
189
+ appValues(idOrValues, values = undefined) {
190
+ const appId = typeof idOrValues === 'string' ? appIdOrValues : this.appId;
191
+ const appValues = typeof idOrValues === 'string' ? values : idOrValues;
192
+ if (!appId || !isOrdinaryObject(appValues)) {
193
+ return undefined;
194
+ }
195
+ return {
196
+ $app: {
197
+ [appId]: appValues,
198
+ },
199
+ };
200
+ }
172
201
  }
202
+ /**
203
+ * Class representing the Swell backend API.
204
+ */
173
205
  class SwellAPI {
174
206
  constructor(req, context) {
175
207
  this.request = req;
@@ -261,6 +293,9 @@ class SwellAPI {
261
293
  return this.makeRequest('GET', `/settings/${id}`);
262
294
  }
263
295
  }
296
+ /**
297
+ * Class representing a Swell error.
298
+ */
264
299
  class SwellError extends Error {
265
300
  constructor(message, options = {}) {
266
301
  let formattedMessage;
@@ -278,10 +313,13 @@ class SwellError extends Error {
278
313
  this.status = options.status || 500;
279
314
  }
280
315
  }
316
+ /**
317
+ * Class representing a Swell response.
318
+ */
281
319
  class SwellResponse extends Response {
282
320
  constructor(data, options = {}) {
283
321
  const resultHeaders = {};
284
- let result;
322
+ let result = '';
285
323
  if (typeof data === 'string') {
286
324
  result = data;
287
325
  resultHeaders['Content-Type'] = 'text/plain;charset=UTF-8';
@@ -298,5 +336,52 @@ class SwellResponse extends Response {
298
336
  ...(options.headers || {}),
299
337
  },
300
338
  });
339
+ // Saved for future access
340
+ this._swellData = data;
341
+ this._swellOptions = options || {};
301
342
  }
343
+ static _respond(req, response, context) {
344
+ let finalResponse = response;
345
+ // Convert a plain Response instance to SwellResponse
346
+ if (finalResponse instanceof Response &&
347
+ !(finalResponse instanceof SwellResponse)) {
348
+ finalResponse = new SwellResponse(response.body, {
349
+ status: response.status,
350
+ headers: response.headers,
351
+ });
352
+ }
353
+ else if (!(finalResponse instanceof SwellResponse)) {
354
+ finalResponse = new SwellResponse(response);
355
+ }
356
+ // Send logs back with the response for event hooks
357
+ if (req.data?.$event?.hook) {
358
+ return SwellResponse._respondWithLogs(finalResponse, req);
359
+ }
360
+ // Ingest logs in the background
361
+ context.waitUntil(req.ingestLogs(finalResponse));
362
+ return finalResponse;
363
+ }
364
+ static _respondWithLogs(response, req) {
365
+ const ingestableLogs = req.getIngestableLogs(response);
366
+ // Rebuild response with logs
367
+ const responseData = response instanceof SwellResponse ? response?._swellData : response;
368
+ const resultData = ingestableLogs
369
+ ? {
370
+ $logs: ingestableLogs,
371
+ $data: responseData,
372
+ }
373
+ : responseData;
374
+ return new SwellResponse(resultData, response?._swellOptions);
375
+ }
376
+ }
377
+ /**
378
+ * Helper to determine if a value is an ordinary object.
379
+ * @param {any} obj
380
+ * @returns {boolean}
381
+ */
382
+ function isOrdinaryObject(val) {
383
+ return (typeof val === 'object' &&
384
+ val !== null &&
385
+ Object.getPrototypeOf(val) === Object.prototype);
302
386
  }
387
+ ;
@@ -14,12 +14,14 @@ export declare abstract class RemoteAppCommand extends AppCommand {
14
14
  protected getApp(id?: string): Promise<App>;
15
15
  protected getAppWithConfig(id?: string): Promise<App>;
16
16
  protected getCreateUpdateApp(updateApp?: App): Promise<App>;
17
+ protected getVersions(version?: null | string | undefined): Promise<AppVersion[]>;
17
18
  protected handleRequestErrors(request: () => any, spinnerError?: () => void): Promise<any>;
18
19
  init(): Promise<void>;
19
20
  protected postConfig(configJson: any): Promise<any>;
20
21
  protected pushConfigFile(appConfig: AppConfig): Promise<any>;
21
22
  protected removeConfig(config: AppConfig): Promise<any>;
22
23
  protected showErrors(errors: any, spinnerError?: () => void): void;
24
+ protected swellRcPath(): string;
23
25
  protected syncAppConfigs(): Promise<{
24
26
  installed: string[];
25
27
  removed: string[];
@@ -31,6 +33,4 @@ export declare abstract class RemoteAppCommand extends AppCommand {
31
33
  */
32
34
  private initApp;
33
35
  private showOrientation;
34
- protected getVersions(version?: string | undefined | null): Promise<AppVersion[]>;
35
- protected swellRcPath(): string;
36
36
  }
@@ -5,9 +5,9 @@ import ora from 'ora';
5
5
  import { AppCommand } from './app-command.js';
6
6
  import { appConfigFileExists, readRcFile, writeRcFile, } from './lib/app-config.js';
7
7
  import { FunctionProcessingError, IgnoringFileError, allConfigFilesPaths, appConfigFromFile, appLogoIcon, batchAppConfigsByType, configFilePath, isAppLogoIcon, } from './lib/apps/index.js';
8
- import { toAppId } from './lib/create/index.js';
9
8
  import { default as swellConfig } from './lib/config.js';
10
9
  import { getLoginHost } from './lib/constants.js';
10
+ import { toAppId } from './lib/create/index.js';
11
11
  import style from './lib/style.js';
12
12
  /**
13
13
  * Extends the AppCommand with methods that ease managing remote apps and
@@ -22,7 +22,7 @@ export class RemoteAppCommand extends AppCommand {
22
22
  dashboardUrl(flags = {}, storeId) {
23
23
  const { env } = flags;
24
24
  const envFlag = env || this.ctor.orientation?.env;
25
- const outputEnv = envFlag === 'live' ? '' : !envFlag ? '' : 'test';
25
+ const outputEnv = envFlag === 'live' ? '' : envFlag ? 'test' : '';
26
26
  return `${getLoginHost(storeId)}/admin/${outputEnv ? `${outputEnv}/` : ''}apps/${this.app.id}`;
27
27
  }
28
28
  async deleteConfig(configId) {
@@ -142,7 +142,7 @@ export class RemoteAppCommand extends AppCommand {
142
142
  adminPath: `/apps/${this.appConfig.store.id}`,
143
143
  });
144
144
  }
145
- catch (err) {
145
+ catch {
146
146
  // noop
147
147
  }
148
148
  spinner.start(`Creating app...`);
@@ -180,6 +180,17 @@ export class RemoteAppCommand extends AppCommand {
180
180
  }
181
181
  return {};
182
182
  }
183
+ async getVersions(version) {
184
+ const versionsResponse = await this.api.get({ adminPath: `/apps/${this.app.id}/versions` }, {
185
+ query: {
186
+ limit: 1000,
187
+ sort: 'version desc',
188
+ ...(version ? { version } : undefined),
189
+ },
190
+ });
191
+ this.debugJson('getApp: versionsResponse', versionsResponse);
192
+ return versionsResponse.results || [];
193
+ }
183
194
  async handleRequestErrors(request, spinnerError) {
184
195
  try {
185
196
  const response = await request();
@@ -291,6 +302,9 @@ export class RemoteAppCommand extends AppCommand {
291
302
  .join(' '));
292
303
  }
293
304
  }
305
+ swellRcPath() {
306
+ return path.join(this.appPath, '.swellrc');
307
+ }
294
308
  async syncAppConfigs() {
295
309
  const configsToPush = [];
296
310
  const installed = [];
@@ -306,8 +320,8 @@ export class RemoteAppCommand extends AppCommand {
306
320
  const batches = batchAppConfigsByType(configsToPush);
307
321
  for (const batch of batches) {
308
322
  this.log(style.appConfigName(batch.label));
309
- // eslint-disable-next-line no-await-in-loop
310
323
  for (const appConfig of batch.configs) {
324
+ // eslint-disable-next-line no-await-in-loop
311
325
  const installedConfig = await this.pushConfigFile(appConfig);
312
326
  if (installedConfig)
313
327
  installed.push(installedConfig.id);
@@ -375,18 +389,4 @@ export class RemoteAppCommand extends AppCommand {
375
389
  this.log(orientationOutput.join(' '));
376
390
  this.log();
377
391
  }
378
- async getVersions(version) {
379
- const versionsResponse = await this.api.get({ adminPath: `/apps/${this.app.id}/versions` }, {
380
- query: {
381
- limit: 1000,
382
- sort: 'version desc',
383
- ...(version ? { version } : undefined),
384
- },
385
- });
386
- this.debugJson('getApp: versionsResponse', versionsResponse);
387
- return versionsResponse.results || [];
388
- }
389
- swellRcPath() {
390
- return path.join(this.appPath, '.swellrc');
391
- }
392
392
  }
@@ -8,6 +8,13 @@
8
8
  "<%= config.bin %> <%= command.id %>"
9
9
  ],
10
10
  "flags": {
11
+ "force": {
12
+ "char": "f",
13
+ "description": "show all available stores and switch the current session if needed",
14
+ "name": "force",
15
+ "allowNo": false,
16
+ "type": "boolean"
17
+ },
11
18
  "store": {
12
19
  "char": "s",
13
20
  "description": "store to login to",
@@ -15,13 +22,6 @@
15
22
  "hasDynamicHelp": false,
16
23
  "multiple": false,
17
24
  "type": "option"
18
- },
19
- "force": {
20
- "char": "f",
21
- "description": "show all available stores and switch the current session if needed",
22
- "name": "force",
23
- "allowNo": false,
24
- "type": "boolean"
25
25
  }
26
26
  },
27
27
  "hasDynamicHelp": false,
@@ -442,6 +442,9 @@
442
442
  "strict": true,
443
443
  "summary": "Install an existing app in another store environment.",
444
444
  "enableJsonFlag": false,
445
+ "orientation": {
446
+ "env": "test"
447
+ },
445
448
  "isESM": true,
446
449
  "relativePath": [
447
450
  "dist",
@@ -703,15 +706,6 @@
703
706
  "multiple": false,
704
707
  "type": "option"
705
708
  },
706
- "label": {
707
- "char": "l",
708
- "description": "content model label",
709
- "name": "label",
710
- "default": "",
711
- "hasDynamicHelp": false,
712
- "multiple": false,
713
- "type": "option"
714
- },
715
709
  "description": {
716
710
  "char": "d",
717
711
  "description": "content model description",
@@ -730,10 +724,10 @@
730
724
  "multiple": false,
731
725
  "type": "option"
732
726
  },
733
- "views": {
734
- "char": "v",
735
- "description": "list of content model views delimited by commas, i.e. list,edit,new",
736
- "name": "views",
727
+ "label": {
728
+ "char": "l",
729
+ "description": "content model label",
730
+ "name": "label",
737
731
  "default": "",
738
732
  "hasDynamicHelp": false,
739
733
  "multiple": false,
@@ -744,6 +738,15 @@
744
738
  "name": "overwrite",
745
739
  "allowNo": false,
746
740
  "type": "boolean"
741
+ },
742
+ "views": {
743
+ "char": "v",
744
+ "description": "list of content model views delimited by commas, i.e. list,edit,new",
745
+ "name": "views",
746
+ "default": "",
747
+ "hasDynamicHelp": false,
748
+ "multiple": false,
749
+ "type": "option"
747
750
  }
748
751
  },
749
752
  "hasDynamicHelp": false,
@@ -823,6 +826,12 @@
823
826
  "multiple": false,
824
827
  "type": "option"
825
828
  },
829
+ "overwrite": {
830
+ "description": "overwrite existing function file",
831
+ "name": "overwrite",
832
+ "allowNo": false,
833
+ "type": "boolean"
834
+ },
826
835
  "route": {
827
836
  "char": "r",
828
837
  "description": "access type used with route trigger",
@@ -844,12 +853,6 @@
844
853
  "hasDynamicHelp": false,
845
854
  "multiple": false,
846
855
  "type": "option"
847
- },
848
- "overwrite": {
849
- "description": "overwrite existing function file",
850
- "name": "overwrite",
851
- "allowNo": false,
852
- "type": "boolean"
853
856
  }
854
857
  },
855
858
  "hasDynamicHelp": false,
@@ -928,15 +931,6 @@
928
931
  "multiple": false,
929
932
  "type": "option"
930
933
  },
931
- "label": {
932
- "char": "l",
933
- "description": "data model label, i.e. Products",
934
- "name": "label",
935
- "default": "",
936
- "hasDynamicHelp": false,
937
- "multiple": false,
938
- "type": "option"
939
- },
940
934
  "description": {
941
935
  "char": "d",
942
936
  "description": "data model description",
@@ -946,6 +940,15 @@
946
940
  "multiple": false,
947
941
  "type": "option"
948
942
  },
943
+ "events": {
944
+ "char": "e",
945
+ "description": "list of data model events delimited by commas",
946
+ "name": "events",
947
+ "default": "created,updated,deleted",
948
+ "hasDynamicHelp": false,
949
+ "multiple": false,
950
+ "type": "option"
951
+ },
949
952
  "fields": {
950
953
  "char": "f",
951
954
  "description": "list of data model fields delimited by commas",
@@ -955,11 +958,11 @@
955
958
  "multiple": false,
956
959
  "type": "option"
957
960
  },
958
- "events": {
959
- "char": "e",
960
- "description": "list of data model events delimited by commas",
961
- "name": "events",
962
- "default": "created,updated,deleted",
961
+ "label": {
962
+ "char": "l",
963
+ "description": "data model label, i.e. Products",
964
+ "name": "label",
965
+ "default": "",
963
966
  "hasDynamicHelp": false,
964
967
  "multiple": false,
965
968
  "type": "option"
@@ -1021,6 +1024,13 @@
1021
1024
  "multiple": false,
1022
1025
  "type": "option"
1023
1026
  },
1027
+ "admin": {
1028
+ "char": "a",
1029
+ "description": "indicate when the notification is sent to store admins only",
1030
+ "name": "admin",
1031
+ "allowNo": false,
1032
+ "type": "boolean"
1033
+ },
1024
1034
  "description": {
1025
1035
  "char": "d",
1026
1036
  "description": "notification description",
@@ -1030,15 +1040,6 @@
1030
1040
  "multiple": false,
1031
1041
  "type": "option"
1032
1042
  },
1033
- "subject": {
1034
- "char": "s",
1035
- "description": "notification email subject",
1036
- "name": "subject",
1037
- "default": "",
1038
- "hasDynamicHelp": false,
1039
- "multiple": false,
1040
- "type": "option"
1041
- },
1042
1043
  "event": {
1043
1044
  "char": "e",
1044
1045
  "description": "data model event to trigger the notification event, i.e. created",
@@ -1048,13 +1049,6 @@
1048
1049
  "multiple": false,
1049
1050
  "type": "option"
1050
1051
  },
1051
- "admin": {
1052
- "char": "a",
1053
- "description": "indicate when the notification is sent to store admins only",
1054
- "name": "admin",
1055
- "allowNo": false,
1056
- "type": "boolean"
1057
- },
1058
1052
  "once": {
1059
1053
  "char": "o",
1060
1054
  "description": "indicate the notification should be sent only once when conditions are met, instead of repeating",
@@ -1067,6 +1061,15 @@
1067
1061
  "name": "overwrite",
1068
1062
  "allowNo": false,
1069
1063
  "type": "boolean"
1064
+ },
1065
+ "subject": {
1066
+ "char": "s",
1067
+ "description": "notification email subject",
1068
+ "name": "subject",
1069
+ "default": "",
1070
+ "hasDynamicHelp": false,
1071
+ "multiple": false,
1072
+ "type": "option"
1070
1073
  }
1071
1074
  },
1072
1075
  "hasDynamicHelp": false,
@@ -1086,5 +1089,5 @@
1086
1089
  ]
1087
1090
  }
1088
1091
  },
1089
- "version": "2.0.1-alpha.12"
1092
+ "version": "2.0.1-alpha.16"
1090
1093
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swell/cli",
3
- "version": "2.0.1-alpha.12",
3
+ "version": "2.0.1-alpha.16",
4
4
  "type": "module",
5
5
  "description": "Swell's command line interface/utility",
6
6
  "keywords": [
@@ -88,6 +88,7 @@
88
88
  }
89
89
  },
90
90
  "scripts": {
91
+ "preinstall": "npm i -g ts-node",
91
92
  "build": "shx rm -rf dist && shx rm -f tsconfig.tsbuildinfo && tsc -b",
92
93
  "lint": "eslint . --ext .ts --config .eslintrc",
93
94
  "postpack": "shx rm -f oclif.manifest.json",