pwrdrvr 0.4.0-alpha.13

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 (82) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +241 -0
  3. package/bin/run +5 -0
  4. package/dist/commands/delete.d.ts +18 -0
  5. package/dist/commands/delete.d.ts.map +1 -0
  6. package/dist/commands/delete.js +125 -0
  7. package/dist/commands/delete.js.map +1 -0
  8. package/dist/commands/nextjs-version-restore.d.ts +14 -0
  9. package/dist/commands/nextjs-version-restore.d.ts.map +1 -0
  10. package/dist/commands/nextjs-version-restore.js +53 -0
  11. package/dist/commands/nextjs-version-restore.js.map +1 -0
  12. package/dist/commands/nextjs-version.d.ts +18 -0
  13. package/dist/commands/nextjs-version.d.ts.map +1 -0
  14. package/dist/commands/nextjs-version.js +103 -0
  15. package/dist/commands/nextjs-version.js.map +1 -0
  16. package/dist/commands/preflight.d.ts +19 -0
  17. package/dist/commands/preflight.d.ts.map +1 -0
  18. package/dist/commands/preflight.js +135 -0
  19. package/dist/commands/preflight.js.map +1 -0
  20. package/dist/commands/publish-static.d.ts +25 -0
  21. package/dist/commands/publish-static.d.ts.map +1 -0
  22. package/dist/commands/publish-static.js +361 -0
  23. package/dist/commands/publish-static.js.map +1 -0
  24. package/dist/commands/publish.d.ts +39 -0
  25. package/dist/commands/publish.d.ts.map +1 -0
  26. package/dist/commands/publish.js +565 -0
  27. package/dist/commands/publish.js.map +1 -0
  28. package/dist/config/Application.d.ts +26 -0
  29. package/dist/config/Application.d.ts.map +1 -0
  30. package/dist/config/Application.js +99 -0
  31. package/dist/config/Application.js.map +1 -0
  32. package/dist/config/Config.d.ts +18 -0
  33. package/dist/config/Config.d.ts.map +1 -0
  34. package/dist/config/Config.js +71 -0
  35. package/dist/config/Config.js.map +1 -0
  36. package/dist/config/Deployer.d.ts +10 -0
  37. package/dist/config/Deployer.d.ts.map +1 -0
  38. package/dist/config/Deployer.js +17 -0
  39. package/dist/config/Deployer.js.map +1 -0
  40. package/dist/index.d.ts +2 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +6 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/lib/DeployClient.d.ts +102 -0
  45. package/dist/lib/DeployClient.d.ts.map +1 -0
  46. package/dist/lib/DeployClient.js +233 -0
  47. package/dist/lib/DeployClient.js.map +1 -0
  48. package/dist/lib/FilesExist.d.ts +5 -0
  49. package/dist/lib/FilesExist.d.ts.map +1 -0
  50. package/dist/lib/FilesExist.js +26 -0
  51. package/dist/lib/FilesExist.js.map +1 -0
  52. package/dist/lib/S3TransferUtility.d.ts +19 -0
  53. package/dist/lib/S3TransferUtility.d.ts.map +1 -0
  54. package/dist/lib/S3TransferUtility.js +94 -0
  55. package/dist/lib/S3TransferUtility.js.map +1 -0
  56. package/dist/lib/S3Uploader.d.ts +27 -0
  57. package/dist/lib/S3Uploader.d.ts.map +1 -0
  58. package/dist/lib/S3Uploader.js +77 -0
  59. package/dist/lib/S3Uploader.js.map +1 -0
  60. package/dist/lib/Versions.d.ts +33 -0
  61. package/dist/lib/Versions.d.ts.map +1 -0
  62. package/dist/lib/Versions.js +76 -0
  63. package/dist/lib/Versions.js.map +1 -0
  64. package/package.json +83 -0
  65. package/src/commands/delete.ts +135 -0
  66. package/src/commands/nextjs-version-restore.ts +70 -0
  67. package/src/commands/nextjs-version.ts +123 -0
  68. package/src/commands/preflight.ts +148 -0
  69. package/src/commands/publish-static.ts +416 -0
  70. package/src/commands/publish.ts +662 -0
  71. package/src/commands-deprecated/nextjs-docker-auto.skip +590 -0
  72. package/src/config/Application.ts +98 -0
  73. package/src/config/Config.ts +81 -0
  74. package/src/config/Deployer.ts +17 -0
  75. package/src/index.ts +1 -0
  76. package/src/lib/DeployClient.ts +334 -0
  77. package/src/lib/FilesExist.ts +25 -0
  78. package/src/lib/S3TransferUtility.spec.ts +15 -0
  79. package/src/lib/S3TransferUtility.ts +113 -0
  80. package/src/lib/S3Uploader.ts +94 -0
  81. package/src/lib/Versions.ts +101 -0
  82. package/src/lib/__snapshots__/S3TransferUtility.spec.ts.snap +12 -0
@@ -0,0 +1,662 @@
1
+ import 'reflect-metadata';
2
+ import * as util from 'util';
3
+ import * as lambda from '@aws-sdk/client-lambda';
4
+ import * as s3 from '@aws-sdk/client-s3';
5
+ import * as sts from '@aws-sdk/client-sts';
6
+ import { Command, flags as flagsParser } from '@oclif/command';
7
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
8
+ const pMap = require('p-map');
9
+ import * as path from 'path';
10
+ import { pathExists, createReadStream } from 'fs-extra';
11
+ import { Listr, ListrTask } from 'listr2';
12
+ import { createVersions, IVersions } from '../lib/Versions';
13
+ import { Config, IConfig } from '../config/Config';
14
+ import DeployClient, {
15
+ DeployVersionArgs,
16
+ IDeployVersionPreflightResult,
17
+ } from '../lib/DeployClient';
18
+ import { S3Uploader } from '../lib/S3Uploader';
19
+ import { S3TransferUtility } from '../lib/S3TransferUtility';
20
+ import { Upload } from '@aws-sdk/lib-storage';
21
+ import { contentType } from 'mime-types';
22
+ import { TaskWrapper } from 'listr2/dist/lib/task-wrapper';
23
+ import { DefaultRenderer } from 'listr2/dist/renderer/default.renderer';
24
+ const asyncSetTimeout = util.promisify(setTimeout);
25
+
26
+ const lambdaClient = new lambda.LambdaClient({
27
+ maxAttempts: 8,
28
+ });
29
+
30
+ interface IContext {
31
+ preflightResult: IDeployVersionPreflightResult;
32
+ files: string[];
33
+
34
+ /**
35
+ * The type of ARN passed in on the config or command line
36
+ */
37
+ configLambdaArnType: 'function' | 'alias' | 'version';
38
+
39
+ /**
40
+ * The ARN of the Lambda alias to use for the deploy
41
+ */
42
+ lambdaAliasArn: string;
43
+
44
+ /**
45
+ * Lambda Function URL returned by `LambdaAlias` for lambda-url type
46
+ */
47
+ lambdaUrl: string;
48
+ }
49
+
50
+ export class PublishCommand extends Command {
51
+ static description =
52
+ 'Publish arbitrary framework app - deploy static assets to S3, alias the $LATEST Lambda function, and add integration/route to API Gateway.';
53
+
54
+ static examples = [
55
+ `$ pwrdrvr publish -d microapps-deployer-dev -l microapps-app-release-dev -a release -n 0.0.21
56
+ ✔ Get S3 Temp Credentials [1s]
57
+ ✔ Deploy to Lambda [0.6s]
58
+ ✔ Confirm Static Assets Folder Exists [0.0s]
59
+ ✔ Copy Static Files to Local Upload Dir [0.0s]
60
+ ✔ Enumerate Files to Upload to S3 [0.0s]
61
+ ✔ Upload Static Files to S3 [1s]
62
+ ✔ Creating MicroApp Application: release [0.0s]
63
+ ✔ Creating MicroApp Version: 0.0.21 [1s]
64
+ `,
65
+ ];
66
+
67
+ static flags = {
68
+ version: flagsParser.version({
69
+ char: 'v',
70
+ }),
71
+ help: flagsParser.help(),
72
+ // Deprecated
73
+ deployerLambdaName: flagsParser.string({
74
+ multiple: false,
75
+ required: false,
76
+ hidden: true,
77
+ }),
78
+ 'deployer-lambda-name': flagsParser.string({
79
+ char: 'd',
80
+ multiple: false,
81
+ exactlyOne: ['deployer-lambda-name', 'deployerLambdaName'],
82
+ description: 'Name of the deployer lambda function',
83
+ }),
84
+ // Deprecated
85
+ newVersion: flagsParser.string({
86
+ multiple: false,
87
+ required: false,
88
+ hidden: true,
89
+ }),
90
+ 'new-version': flagsParser.string({
91
+ char: 'n',
92
+ multiple: false,
93
+ exactlyOne: ['new-version', 'newVersion'],
94
+ description: 'New semantic version to apply',
95
+ }),
96
+ // Deprecated
97
+ appLambdaName: flagsParser.string({
98
+ multiple: false,
99
+ required: false,
100
+ hidden: true,
101
+ exclusive: ['app-lambda-name'],
102
+ }),
103
+ 'app-lambda-name': flagsParser.string({
104
+ char: 'l',
105
+ multiple: false,
106
+ required: false,
107
+ description: 'ARN of lambda version, alias, or function (name or ARN) to deploy',
108
+ }),
109
+ // Deprecated
110
+ appName: flagsParser.string({
111
+ multiple: false,
112
+ required: false,
113
+ hidden: true,
114
+ exclusive: ['app-name'],
115
+ }),
116
+ 'app-name': flagsParser.string({
117
+ char: 'a',
118
+ multiple: false,
119
+ exactlyOne: ['app-name', 'appName'],
120
+ description: 'MicroApps app name (this becomes the path the app is rooted at)',
121
+ }),
122
+ // Deprecated
123
+ staticAssetsPath: flagsParser.string({
124
+ multiple: false,
125
+ required: false,
126
+ hidden: true,
127
+ exclusive: ['static-assets-path'],
128
+ }),
129
+ 'static-assets-path': flagsParser.string({
130
+ char: 's',
131
+ multiple: false,
132
+ required: false,
133
+ description:
134
+ 'Path to files to be uploaded to S3 static bucket at app/version/ path. Do include app/version/ in path if files are already "rooted" under that path locally.',
135
+ }),
136
+ // Deprecated
137
+ defaultFile: flagsParser.string({
138
+ multiple: false,
139
+ required: false,
140
+ hidden: true,
141
+ exclusive: ['default-file'],
142
+ }),
143
+ 'default-file': flagsParser.string({
144
+ char: 'i',
145
+ multiple: false,
146
+ required: false,
147
+ description:
148
+ 'Default file to return when the app is loaded via the router without a version (e.g. when app/ is requested).',
149
+ }),
150
+ overwrite: flagsParser.boolean({
151
+ char: 'o',
152
+ required: false,
153
+ default: false,
154
+ description:
155
+ 'Allow overwrite - Warn but do not fail if version exists. Discouraged outside of test envs if cacheable static files have changed.',
156
+ }),
157
+ // Deprecated
158
+ noCache: flagsParser.boolean({
159
+ required: false,
160
+ default: false,
161
+ hidden: true,
162
+ }),
163
+ 'no-cache': flagsParser.boolean({
164
+ required: false,
165
+ default: false,
166
+ description: 'Force revalidation of CloudFront and browser caching of static assets',
167
+ }),
168
+ 'startup-type': flagsParser.enum({
169
+ multiple: false,
170
+ required: false,
171
+ options: ['iframe', 'direct'],
172
+ default: 'iframe',
173
+ description: 'How the app should be loaded',
174
+ }),
175
+ type: flagsParser.enum({
176
+ char: 't',
177
+ multiple: false,
178
+ required: false,
179
+ options: ['apigwy', 'lambda-url', 'url', 'static'],
180
+ default: 'lambda-url',
181
+ description: 'Type of the application and how its requests are routed',
182
+ }),
183
+ url: flagsParser.string({
184
+ char: 'u',
185
+ multiple: false,
186
+ required: false,
187
+ description: 'URL for `url` type applications',
188
+ }),
189
+ };
190
+
191
+ private VersionAndAlias: IVersions;
192
+
193
+ async run(): Promise<void> {
194
+ const config = Config.instance;
195
+
196
+ // const RUNNING_TEXT = ' RUNS ';
197
+ // const RUNNING = chalk.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';
198
+ const RUNNING = ''; //chalk.reset.inverse.yellow.bold(RUNNING_TEXT) + ' ';
199
+
200
+ const { flags: parsedFlags } = this.parse(PublishCommand);
201
+ const appLambdaName =
202
+ parsedFlags.appLambdaName ?? parsedFlags['app-lambda-name'] ?? config.app.lambdaName;
203
+ const appName = parsedFlags.appName ?? parsedFlags['app-name'] ?? config.app.name;
204
+ const deployerLambdaName =
205
+ parsedFlags.deployerLambdaName ??
206
+ parsedFlags['deployer-lambda-name'] ??
207
+ config.deployer.lambdaName;
208
+ const semVer = parsedFlags.newVersion ?? parsedFlags['new-version'] ?? config.app.semVer;
209
+ const staticAssetsPath =
210
+ parsedFlags.staticAssetsPath ??
211
+ parsedFlags['static-assets-path'] ??
212
+ config.app.staticAssetsPath;
213
+ const defaultFile =
214
+ parsedFlags.defaultFile ?? parsedFlags['default-file'] ?? config.app.defaultFile;
215
+ const overwrite = parsedFlags.overwrite;
216
+ const noCache = parsedFlags.noCache ?? parsedFlags['no-cache'];
217
+
218
+ // Override the config value
219
+ config.deployer.lambdaName = deployerLambdaName;
220
+ config.app.lambdaName = appLambdaName;
221
+ config.app.name = appName;
222
+ config.app.semVer = semVer;
223
+ config.app.staticAssetsPath = staticAssetsPath;
224
+ config.app.defaultFile = defaultFile;
225
+
226
+ // Get the account ID and region from STS
227
+ // TODO: Move this to the right place
228
+ if (config.app.awsAccountID === '' || config.app.awsRegion === '') {
229
+ const stsClient = new sts.STSClient({
230
+ maxAttempts: 8,
231
+ });
232
+ const stsResponse = await stsClient.send(new sts.GetCallerIdentityCommand({}));
233
+ if (config.app.awsAccountID === '') {
234
+ config.app.awsAccountID = stsResponse.Account;
235
+ }
236
+ if (config.app.awsRegion === '') {
237
+ config.app.awsRegion = stsClient.config.region as string;
238
+ }
239
+ }
240
+
241
+ this.VersionAndAlias = createVersions(semVer);
242
+
243
+ if (config.app.staticAssetsPath === undefined) {
244
+ this.error('staticAssetsPath must be specified');
245
+ }
246
+
247
+ //
248
+ // Setup Tasks
249
+ //
250
+
251
+ const tasks = new Listr<IContext>(
252
+ [
253
+ {
254
+ enabled: () => !!config.app.staticAssetsPath,
255
+ title: 'Confirm Static Assets Folder Exists',
256
+ task: async (ctx, task) => {
257
+ const origTitle = task.title;
258
+ task.title = RUNNING + origTitle;
259
+
260
+ // Check that Static Assets Folder exists
261
+ if (!(await pathExists(config.app.staticAssetsPath))) {
262
+ this.error(`Static asset path does not exist: ${config.app.staticAssetsPath}`);
263
+ }
264
+
265
+ task.title = origTitle;
266
+ },
267
+ },
268
+ {
269
+ title: 'Preflight Request / Get S3 Temp Credentials',
270
+ task: async (ctx, task) => {
271
+ const origTitle = task.title;
272
+ task.title = RUNNING + origTitle;
273
+
274
+ // Confirm the Version Does Not Exist in Published State
275
+ task.output = `Checking if deployed app/version already exists for ${config.app.name}/${semVer}`;
276
+ ctx.preflightResult = await DeployClient.DeployVersionPreflight({
277
+ config,
278
+ overwrite,
279
+ output: (message: string) => (task.output = message),
280
+ });
281
+ if (ctx.preflightResult.exists) {
282
+ if (!overwrite) {
283
+ throw new Error(
284
+ `App/Version already exists: ${config.app.name}/${config.app.semVer}`,
285
+ );
286
+ } else {
287
+ task.title = `Warning: App/Version already exists: ${config.app.name}/${config.app.semVer}`;
288
+ }
289
+ } else {
290
+ task.title = `App/Version does not exist: ${config.app.name}/${config.app.semVer}`;
291
+ }
292
+ },
293
+ },
294
+ {
295
+ enabled: () => !!appLambdaName,
296
+ title: 'Check if Lambda ARN has Alias or Version',
297
+ task: (ctx, task) => {
298
+ if (appLambdaName.match(/:/g)?.length === 7) {
299
+ if (/^[0-9]+$/.test(appLambdaName.substring(appLambdaName.lastIndexOf(':') + 1))) {
300
+ ctx.configLambdaArnType = 'version';
301
+ task.output = `Lambda ARN has Version: ${config.app.lambdaName}`;
302
+ } else {
303
+ ctx.configLambdaArnType = 'alias';
304
+ task.output = `Lambda ARN has Alias: ${config.app.lambdaName}`;
305
+ ctx.lambdaAliasArn = config.app.lambdaName;
306
+ }
307
+ } else {
308
+ ctx.configLambdaArnType = 'function';
309
+ task.output = `Lambda ARN does not have Alias: ${config.app.lambdaName}`;
310
+ }
311
+ },
312
+ },
313
+ // {
314
+ // enabled: (ctx) =>
315
+ // ctx.configLambdaArnType === 'function' &&
316
+ // ctx.preflightResult.response.capabilities?.['createAlias'] === 'true',
317
+ // title: 'Reject Remove Version Creation if Lambda ARN is Function',
318
+ // task: (ctx, task) => {
319
+ // this.error(
320
+ // `Lambda ARN is a Function, cannot create a version remotely, pass a Lambda ARN with a version: ${config.app.lambdaName}`,
321
+ // );
322
+ // },
323
+ // },
324
+ {
325
+ enabled: (ctx) =>
326
+ ['version', 'function'].includes(ctx.configLambdaArnType) &&
327
+ // If the deployer service can create aliases, let it
328
+ ctx.preflightResult.response.capabilities?.createAlias === 'true',
329
+ title: 'Remotely Create Lambda Alias and, optionally, Version',
330
+ task: async (ctx, task) => {
331
+ const origTitle = task.title;
332
+ task.title = RUNNING + origTitle;
333
+
334
+ task.output = `Create or update alias ${config.app.name}/${semVer}`;
335
+ const { response } = await DeployClient.LambdaAlias({
336
+ appName: config.app.name,
337
+ semVer: config.app.semVer,
338
+ deployerLambdaName: config.deployer.lambdaName,
339
+ lambdaVersionArn: config.app.lambdaName,
340
+ overwrite,
341
+ output: (message: string) => (task.output = message),
342
+ });
343
+
344
+ ctx.lambdaAliasArn = response.lambdaAliasARN;
345
+ ctx.lambdaUrl = response.functionUrl;
346
+ task.title = origTitle;
347
+ },
348
+ },
349
+ {
350
+ enabled: (ctx) =>
351
+ ['function', 'version'].includes(ctx.configLambdaArnType) &&
352
+ // If the deployer service can create aliases, let it
353
+ ctx.preflightResult.response.capabilities?.['createAlias'] !== 'true',
354
+ title: 'Locally Create Lambda Alias and, optionally, Version',
355
+ task: async (ctx, task) => {
356
+ // Allow overwriting a non-overwritable app if the prior
357
+ // publish was not completely successful - in that case
358
+ // the lambda alias may exist and need updating
359
+ const allowOverwrite = overwrite || !ctx.preflightResult.exists;
360
+ const origTitle = task.title;
361
+ task.title = RUNNING + origTitle;
362
+
363
+ // Update the Lambda function
364
+ await this.deployToLambda({
365
+ config,
366
+ versions: this.VersionAndAlias,
367
+ overwrite: allowOverwrite,
368
+ task,
369
+ ctx,
370
+ });
371
+
372
+ task.title = origTitle;
373
+ },
374
+ },
375
+ {
376
+ enabled: () => !!config.app.staticAssetsPath,
377
+ title: 'Copy Static Files to Local Upload Dir',
378
+ task: async (ctx, task) => {
379
+ const origTitle = task.title;
380
+ task.title = RUNNING + origTitle;
381
+
382
+ // Copy files to local dir to be uploaded
383
+ await S3Uploader.CopyToUploadDir(config, ctx.preflightResult.response.s3UploadUrl);
384
+
385
+ task.title = origTitle;
386
+ },
387
+ },
388
+ {
389
+ enabled: () => !!config.app.staticAssetsPath,
390
+ title: 'Enumerate Files to Upload to S3',
391
+ task: (ctx, task) => {
392
+ const origTitle = task.title;
393
+ task.title = RUNNING + origTitle;
394
+
395
+ ctx.files = S3TransferUtility.GetFiles(S3Uploader.TempDir);
396
+
397
+ task.title = origTitle;
398
+ },
399
+ },
400
+ {
401
+ enabled: () => !!config.app.staticAssetsPath,
402
+ title: 'Upload Static Files to S3',
403
+ task: async (ctx, task) => {
404
+ const origTitle = task.title;
405
+ task.title = RUNNING + origTitle;
406
+
407
+ const { bucketName, destinationPrefix } = S3Uploader.ParseUploadPath(
408
+ ctx.preflightResult.response.s3UploadUrl,
409
+ );
410
+
411
+ // Use temp credentials for S3
412
+ const s3Client = new s3.S3Client({
413
+ maxAttempts: 16,
414
+ credentials: {
415
+ accessKeyId: ctx.preflightResult.response.awsCredentials.accessKeyId,
416
+ secretAccessKey: ctx.preflightResult.response.awsCredentials.secretAccessKey,
417
+ sessionToken: ctx.preflightResult.response.awsCredentials.sessionToken,
418
+ },
419
+ });
420
+
421
+ // Setup caching on static assets
422
+ // NoCache - Only used for test deploys, requires browser and CloudFront to refetch every time
423
+ // Default - 24 hours
424
+ const CacheControl = noCache
425
+ ? 'max-age=0, must-revalidate, public'
426
+ : `max-age=${24 * 60 * 60}, public`;
427
+
428
+ const pathWithoutAppAndVer = path.join(S3Uploader.TempDir, destinationPrefix);
429
+
430
+ // Listr causes OOM if passes a list of, say, 5,000 to 20,000 files
431
+ if (ctx.files.length > 200) {
432
+ const fileCountMsgInterval = Math.floor(ctx.files.length / 10);
433
+ let filesPublished = 0;
434
+
435
+ await pMap(
436
+ ctx.files,
437
+ async (filePath: string) => {
438
+ // Can't use tasks for each file
439
+ const relFilePath = path.relative(pathWithoutAppAndVer, filePath);
440
+
441
+ if (
442
+ ctx.files.length > 1000 &&
443
+ (filesPublished % fileCountMsgInterval === 0 ||
444
+ filesPublished === ctx.files.length)
445
+ ) {
446
+ task.output = `Uploaded ${filesPublished} of ${ctx.files.length} files`;
447
+ } else if (ctx.files.length <= 1000) {
448
+ task.output = `Uploading ${relFilePath}`;
449
+ }
450
+
451
+ const upload = new Upload({
452
+ client: s3Client,
453
+ leavePartsOnError: false,
454
+ params: {
455
+ Bucket: bucketName,
456
+ Key: path.relative(S3Uploader.TempDir, filePath),
457
+ Body: createReadStream(filePath),
458
+ ContentType:
459
+ contentType(path.basename(filePath)) || 'application/octet-stream',
460
+ CacheControl,
461
+ },
462
+ });
463
+ await upload.done();
464
+ filesPublished++;
465
+ },
466
+ { concurrency: 40 },
467
+ );
468
+ } else {
469
+ const tasks: ListrTask<IContext>[] = ctx.files.map((filePath) => ({
470
+ task: async (ctx: IContext, subtask) => {
471
+ const relFilePath = path.relative(pathWithoutAppAndVer, filePath);
472
+
473
+ const origTitle = relFilePath;
474
+ subtask.title = RUNNING + origTitle;
475
+
476
+ const upload = new Upload({
477
+ client: s3Client,
478
+ leavePartsOnError: false,
479
+ params: {
480
+ Bucket: bucketName,
481
+ Key: path.relative(S3Uploader.TempDir, filePath),
482
+ Body: createReadStream(filePath),
483
+ ContentType:
484
+ contentType(path.basename(filePath)) || 'application/octet-stream',
485
+ CacheControl,
486
+ },
487
+ });
488
+ await upload.done();
489
+
490
+ subtask.title = origTitle;
491
+ },
492
+ }));
493
+
494
+ task.title = origTitle;
495
+
496
+ return task.newListr(tasks, {
497
+ concurrent: 20,
498
+ rendererOptions: {
499
+ clearOutput: false,
500
+ showErrorMessage: true,
501
+ showTimer: true,
502
+ },
503
+ });
504
+ }
505
+ },
506
+ },
507
+ {
508
+ title: `Creating MicroApp Application: ${config.app.name}`,
509
+ task: async (ctx, task) => {
510
+ const origTitle = task.title;
511
+ task.title = RUNNING + origTitle;
512
+
513
+ // Call Deployer to Create App if Not Exists
514
+ await DeployClient.CreateApp({ config });
515
+
516
+ task.title = origTitle;
517
+ },
518
+ },
519
+ {
520
+ title: `Creating MicroApp Version: ${config.app.semVer}`,
521
+ task: async (ctx, task) => {
522
+ const origTitle = task.title;
523
+ task.title = RUNNING + origTitle;
524
+
525
+ const appType: 'lambda' | 'lambda-url' | 'static' | 'url' =
526
+ parsedFlags.type === 'apigwy'
527
+ ? 'lambda'
528
+ : (parsedFlags.type as 'lambda-url' | 'static' | 'url');
529
+
530
+ const request: DeployVersionArgs = {
531
+ appName: config.app.name,
532
+ semVer: config.app.semVer,
533
+ deployerLambdaName: config.deployer.lambdaName,
534
+ lambdaAliasArn: ctx.lambdaAliasArn,
535
+ defaultFile: config.app.defaultFile,
536
+ appType,
537
+ url: ctx.lambdaUrl ?? parsedFlags.url,
538
+ ...(['lambda', 'lambda-url', 'static'].includes(appType)
539
+ ? { startupType: parsedFlags['startup-type'] as 'iframe' | 'direct' }
540
+ : { startupType: 'direct' }),
541
+ overwrite,
542
+ output: (message: string) => (task.output = message),
543
+ };
544
+
545
+ // Use DeployVersionLite if createAlias is supported
546
+ if (ctx.preflightResult.response.capabilities?.['createAlias'] === 'true') {
547
+ task.output = 'Using DeployVersionLite';
548
+ await DeployClient.DeployVersionLite(request);
549
+ } else {
550
+ // Use legacy DeployVersion if createAlias is not supported
551
+ task.output = 'Using DeployVersion';
552
+ await DeployClient.DeployVersion(request);
553
+ }
554
+
555
+ task.title = origTitle;
556
+ },
557
+ },
558
+ ],
559
+ {
560
+ rendererOptions: {
561
+ showTimer: true,
562
+ },
563
+ },
564
+ );
565
+
566
+ try {
567
+ await tasks.run();
568
+ } finally {
569
+ await S3Uploader.removeTempDirIfExists();
570
+ }
571
+ }
572
+
573
+ /**
574
+ * Publish an app version to Lambda
575
+ *
576
+ * @param config
577
+ * @param versions
578
+ * @deprecated 2023-01-16 - The deployer svc now creates the alias and, optionally, the version
579
+ */
580
+ private async deployToLambda(opts: {
581
+ config: IConfig;
582
+ versions: IVersions;
583
+ overwrite: boolean;
584
+ task: TaskWrapper<IContext, typeof DefaultRenderer>;
585
+ ctx: IContext;
586
+ }): Promise<void> {
587
+ const { config, overwrite, versions, task, ctx } = opts;
588
+
589
+ let lambdaVersion = '';
590
+ let lambdaArnBase = config.app.lambdaName;
591
+
592
+ // Create Lambda version
593
+ if (ctx.configLambdaArnType === 'function') {
594
+ task.output = 'Creating version for Lambda $LATEST';
595
+ const resultUpdate = await lambdaClient.send(
596
+ new lambda.PublishVersionCommand({
597
+ FunctionName: config.app.lambdaName,
598
+ }),
599
+ );
600
+ lambdaVersion = resultUpdate.Version;
601
+ task.output = `Lambda version created: ${resultUpdate.Version}`;
602
+
603
+ let lastUpdateStatus = resultUpdate.LastUpdateStatus;
604
+ for (let i = 0; i < 5; i++) {
605
+ // When the function is created the status will be "Pending"
606
+ // and we have to wait until it's done creating
607
+ // before we can point an alias to it
608
+ if (lastUpdateStatus === 'Successful') {
609
+ task.output = `Lambda function updated, version: ${lambdaVersion}`;
610
+ break;
611
+ }
612
+
613
+ // If it didn't work, wait and try again
614
+ await asyncSetTimeout(1000 * i);
615
+
616
+ const resultGet = await lambdaClient.send(
617
+ new lambda.GetFunctionCommand({
618
+ FunctionName: config.app.lambdaName,
619
+ Qualifier: lambdaVersion,
620
+ }),
621
+ );
622
+
623
+ // Save the last update status so we can check on re-loop
624
+ lastUpdateStatus = resultGet?.Configuration?.LastUpdateStatus;
625
+ }
626
+ } else {
627
+ task.output = 'Lambda is already versioned, skipping version creation';
628
+ lambdaVersion = config.app.lambdaName.split(':')?.pop() || '';
629
+ lambdaArnBase = config.app.lambdaName.substring(0, config.app.lambdaName.lastIndexOf(':'));
630
+ }
631
+
632
+ // Create Lambda alias point
633
+ task.output = `Creating the lambda alias for the new version: ${lambdaVersion}`;
634
+ try {
635
+ const resultLambdaAlias = await lambdaClient.send(
636
+ new lambda.CreateAliasCommand({
637
+ FunctionName: lambdaArnBase,
638
+ Name: versions.alias,
639
+ FunctionVersion: lambdaVersion,
640
+ }),
641
+ );
642
+ task.output = `Lambda alias created, name: ${resultLambdaAlias.Name}`;
643
+ ctx.lambdaAliasArn = resultLambdaAlias.AliasArn;
644
+ } catch (error) {
645
+ if (overwrite && error.name === 'ResourceConflictException') {
646
+ task.output = `Alias exists, updating the lambda alias for version: ${lambdaVersion}`;
647
+
648
+ const resultLambdaAlias = await lambdaClient.send(
649
+ new lambda.UpdateAliasCommand({
650
+ FunctionName: lambdaArnBase,
651
+ Name: versions.alias,
652
+ FunctionVersion: lambdaVersion,
653
+ }),
654
+ );
655
+ task.output = `Lambda alias updated, name: ${resultLambdaAlias.Name}`;
656
+ ctx.lambdaAliasArn = resultLambdaAlias.AliasArn;
657
+ } else {
658
+ throw error;
659
+ }
660
+ }
661
+ }
662
+ }