@pwrdrvr/microapps-publish 0.0.16 → 0.0.20

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.
@@ -1,17 +1,25 @@
1
1
  import * as lambda from '@aws-sdk/client-lambda';
2
-
3
- // eslint-disable-next-line import/no-extraneous-dependencies,import/no-unresolved
2
+ import { TaskWrapper } from 'listr2/dist/lib/task-wrapper';
3
+ import { DefaultRenderer } from 'listr2/dist/renderer/default.renderer';
4
4
  import {
5
5
  IDeployVersionPreflightRequest,
6
6
  IDeployVersionPreflightResponse,
7
7
  ICreateApplicationRequest,
8
8
  IDeployerResponse,
9
9
  IDeployVersionRequest,
10
- } from '@pwrdrvr/microapps-deployer';
10
+ } from '@pwrdrvr/microapps-deployer-lib';
11
11
  import { IConfig } from './config/Config';
12
+ import { IContext } from './index';
13
+
14
+ export interface IDeployVersionPreflightResult {
15
+ exists: boolean;
16
+ response: IDeployVersionPreflightResponse;
17
+ }
12
18
 
13
19
  export default class DeployClient {
14
- static readonly _client = new lambda.LambdaClient({});
20
+ static readonly _client = new lambda.LambdaClient({
21
+ maxAttempts: 8,
22
+ });
15
23
  static readonly _decoder = new TextDecoder('utf-8');
16
24
 
17
25
  public static async CreateApp(config: IConfig): Promise<void> {
@@ -41,7 +49,8 @@ export default class DeployClient {
41
49
 
42
50
  public static async DeployVersionPreflight(
43
51
  config: IConfig,
44
- ): Promise<{ exists: boolean; response: IDeployVersionPreflightResponse }> {
52
+ task: TaskWrapper<IContext, typeof DefaultRenderer>,
53
+ ): Promise<IDeployVersionPreflightResult> {
45
54
  const request = {
46
55
  type: 'deployVersionPreflight',
47
56
  appName: config.app.name,
@@ -59,7 +68,7 @@ export default class DeployClient {
59
68
  Buffer.from(response.Payload).toString('utf-8'),
60
69
  ) as IDeployVersionPreflightResponse;
61
70
  if (dResponse.statusCode === 404) {
62
- console.log(`App/Version do not exist: ${config.app.name}/${config.app.semVer}`);
71
+ task.output = `App/Version do not exist: ${config.app.name}/${config.app.semVer}`;
63
72
  return { exists: false, response: dResponse };
64
73
  } else {
65
74
  return { exists: true, response: dResponse };
@@ -69,7 +78,18 @@ export default class DeployClient {
69
78
  }
70
79
  }
71
80
 
72
- public static async DeployVersion(config: IConfig): Promise<void> {
81
+ /**
82
+ * Copy S3 static assets from staging to live bucket.
83
+ * Create API Gateway Integration for app (if needed).
84
+ * Give API Gateway permission to call the Lambda.
85
+ * Create API Gateway routes for this specific version.
86
+ * @param config
87
+ * @param task
88
+ */
89
+ public static async DeployVersion(
90
+ config: IConfig,
91
+ task: TaskWrapper<IContext, typeof DefaultRenderer>,
92
+ ): Promise<void> {
73
93
  const request = {
74
94
  type: 'deployVersion',
75
95
  appName: config.app.name,
@@ -89,9 +109,10 @@ export default class DeployClient {
89
109
  Buffer.from(response.Payload).toString('utf-8'),
90
110
  ) as IDeployerResponse;
91
111
  if (dResponse.statusCode === 201) {
92
- console.log(`Deploy succeeded: ${config.app.name}/${config.app.semVer}`);
112
+ task.output = `Deploy succeeded: ${config.app.name}/${config.app.semVer}`;
93
113
  } else {
94
- console.log(`Deploy failed with: ${dResponse.statusCode}`);
114
+ task.output = `Deploy failed with: ${dResponse.statusCode}`;
115
+ throw new Error(`Lambda call to DeployVersionfailed with: ${dResponse.statusCode}`);
95
116
  }
96
117
  } else {
97
118
  throw new Error(`Lambda call to DeployVersion failed: ${JSON.stringify(response)}`);
@@ -6,12 +6,19 @@ import { promises as fs, createReadStream } from 'fs';
6
6
  import * as path from 'path';
7
7
  import * as s3 from '@aws-sdk/client-s3';
8
8
  import { Upload } from '@aws-sdk/lib-storage';
9
- // eslint-disable-next-line import/no-extraneous-dependencies,import/no-unresolved
10
- import { IDeployVersionPreflightResponse } from '@pwrdrvr/microapps-deployer';
9
+ import { IDeployVersionPreflightResponse } from '@pwrdrvr/microapps-deployer-lib';
11
10
  import { contentType } from 'mime-types';
12
11
  import pMap from 'p-map';
13
12
 
14
13
  export default class S3TransferUtility {
14
+ /**
15
+ * @deprecated 2021-11-27
16
+ *
17
+ * @param s3Path
18
+ * @param destPrefixPath
19
+ * @param bucketName
20
+ * @param preflightResponse
21
+ */
15
22
  public static async UploadDir(
16
23
  s3Path: string,
17
24
  destPrefixPath: string,
@@ -20,6 +27,7 @@ export default class S3TransferUtility {
20
27
  ): Promise<void> {
21
28
  // Use temp credentials for S3
22
29
  const s3Client = new s3.S3Client({
30
+ maxAttempts: 16,
23
31
  credentials: {
24
32
  accessKeyId: preflightResponse.awsCredentials.accessKeyId,
25
33
  secretAccessKey: preflightResponse.awsCredentials.secretAccessKey,
@@ -69,7 +77,12 @@ export default class S3TransferUtility {
69
77
  // Recursive getFiles from
70
78
  // https://stackoverflow.com/a/45130990/831465
71
79
 
72
- private static async GetFiles(dir: string): Promise<string | string[]> {
80
+ /**
81
+ * Resursively enumerate the files to be uploaded
82
+ * @param dir
83
+ * @returns
84
+ */
85
+ public static async GetFiles(dir: string): Promise<string | string[]> {
73
86
  const dirents = await fs.readdir(dir, { withFileTypes: true });
74
87
  const files = await Promise.all(
75
88
  dirents.map((dirent) => {
package/src/S3Uploader.ts CHANGED
@@ -1,23 +1,55 @@
1
1
  import path from 'path';
2
- // eslint-disable-next-line import/no-extraneous-dependencies,import/no-unresolved
3
- import { IDeployVersionPreflightResponse } from '@pwrdrvr/microapps-deployer';
2
+ import { IDeployVersionPreflightResponse } from '@pwrdrvr/microapps-deployer-lib';
4
3
  import fs from 'fs-extra';
5
4
  import { IConfig } from './config/Config';
6
5
  import S3TransferUtility from './S3TransferUtility';
7
6
 
8
7
  export default class S3Uploader {
8
+ /**
9
+ * Copy files to local upload directory
10
+ * @param config
11
+ * @param s3UploadPath
12
+ * @param preflightResponse
13
+ */
14
+ public static async CopyToUploadDir(config: IConfig, s3UploadPath: string): Promise<void> {
15
+ const { destinationPrefix } = S3Uploader.ParseUploadPath(s3UploadPath);
16
+
17
+ // Make a local root dir for the upload
18
+ const tempUploadPath = path.join(S3Uploader._tempDir, destinationPrefix);
19
+ await S3Uploader.removeTempDirIfExists();
20
+ await fs.mkdir(tempUploadPath, { recursive: true });
21
+
22
+ // Copy the files in the source dir to the root dir
23
+ // Note: It would be faster to move the files, then move them back
24
+ await fs.copy(config.app.staticAssetsPath, tempUploadPath);
25
+ }
26
+
27
+ public static ParseUploadPath(s3UploadPath: string): {
28
+ bucketName: string;
29
+ destinationPrefix: string;
30
+ } {
31
+ // Parse the S3 Source URI
32
+ const uri = new URL(s3UploadPath);
33
+ const bucketName = uri.host;
34
+ const destinationPrefix = uri.pathname.length >= 1 ? uri.pathname.slice(1) : '';
35
+
36
+ return { bucketName, destinationPrefix };
37
+ }
38
+
39
+ /**
40
+ * Upload files to S3
41
+ * @deprecated 2021-11-27
42
+ * @param config
43
+ * @param s3UploadPath
44
+ * @param preflightResponse
45
+ */
9
46
  public static async Upload(
10
47
  config: IConfig,
11
48
  s3UploadPath: string,
12
49
  preflightResponse: IDeployVersionPreflightResponse,
13
50
  ): Promise<void> {
14
51
  try {
15
- //const destinationPrefix = `${config.app.name}/${config.app.semVer}`;
16
-
17
- // Parse the S3 Source URI
18
- const uri = new URL(s3UploadPath);
19
- const bucketName = uri.host;
20
- const destinationPrefix = uri.pathname.length >= 1 ? uri.pathname.slice(1) : '';
52
+ const { destinationPrefix, bucketName } = S3Uploader.ParseUploadPath(s3UploadPath);
21
53
 
22
54
  // Make a local root dir for the upload
23
55
  const tempUploadPath = path.join(S3Uploader._tempDir, destinationPrefix);
@@ -56,4 +88,7 @@ export default class S3Uploader {
56
88
  }
57
89
 
58
90
  private static readonly _tempDir = './deploytool-temp';
91
+ public static get TempDir(): string {
92
+ return S3Uploader._tempDir;
93
+ }
59
94
  }