|
| 1 | +"use strict"; |
| 2 | +var __defProp = Object.defineProperty; |
| 3 | +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 4 | +var __getOwnPropNames = Object.getOwnPropertyNames; |
| 5 | +var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 6 | +var __esm = (fn, res) => function __init() { |
| 7 | + return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; |
| 8 | +}; |
| 9 | +var __export = (target, all) => { |
| 10 | + for (var name in all) |
| 11 | + __defProp(target, name, { get: all[name], enumerable: true }); |
| 12 | +}; |
| 13 | +var __copyProps = (to, from, except, desc) => { |
| 14 | + if (from && typeof from === "object" || typeof from === "function") { |
| 15 | + for (let key of __getOwnPropNames(from)) |
| 16 | + if (!__hasOwnProp.call(to, key) && key !== except) |
| 17 | + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 18 | + } |
| 19 | + return to; |
| 20 | +}; |
| 21 | +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 22 | + |
| 23 | +// packages/@aws-cdk/aws-amplify-alpha/lib/asset-deployment-handler/common.ts |
| 24 | +var ResourceHandler; |
| 25 | +var init_common = __esm({ |
| 26 | + "packages/@aws-cdk/aws-amplify-alpha/lib/asset-deployment-handler/common.ts"() { |
| 27 | + "use strict"; |
| 28 | + ResourceHandler = class { |
| 29 | + constructor(event) { |
| 30 | + this.requestType = event.RequestType; |
| 31 | + this.requestId = event.RequestId; |
| 32 | + this.logicalResourceId = event.LogicalResourceId; |
| 33 | + this.physicalResourceId = event.PhysicalResourceId; |
| 34 | + this.event = event; |
| 35 | + } |
| 36 | + onEvent() { |
| 37 | + switch (this.requestType) { |
| 38 | + case "Create": |
| 39 | + return this.onCreate(); |
| 40 | + case "Update": |
| 41 | + return this.onUpdate(); |
| 42 | + case "Delete": |
| 43 | + return this.onDelete(); |
| 44 | + } |
| 45 | + throw new Error(`Invalid request type ${this.requestType}`); |
| 46 | + } |
| 47 | + isComplete() { |
| 48 | + switch (this.requestType) { |
| 49 | + case "Create": |
| 50 | + return this.isCreateComplete(); |
| 51 | + case "Update": |
| 52 | + return this.isUpdateComplete(); |
| 53 | + case "Delete": |
| 54 | + return this.isDeleteComplete(); |
| 55 | + } |
| 56 | + throw new Error(`Invalid request type ${this.requestType}`); |
| 57 | + } |
| 58 | + log(x) { |
| 59 | + console.log(JSON.stringify(x, void 0, 2)); |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +// packages/@aws-cdk/aws-amplify-alpha/lib/asset-deployment-handler/handler.ts |
| 66 | +var handler_exports = {}; |
| 67 | +__export(handler_exports, { |
| 68 | + AmplifyAssetDeploymentHandler: () => AmplifyAssetDeploymentHandler |
| 69 | +}); |
| 70 | +function parseProps(props) { |
| 71 | + return props; |
| 72 | +} |
| 73 | +var import_client_s3, import_s3_request_presigner, AmplifyAssetDeploymentHandler; |
| 74 | +var init_handler = __esm({ |
| 75 | + "packages/@aws-cdk/aws-amplify-alpha/lib/asset-deployment-handler/handler.ts"() { |
| 76 | + "use strict"; |
| 77 | + import_client_s3 = require("@aws-sdk/client-s3"); |
| 78 | + import_s3_request_presigner = require("@aws-sdk/s3-request-presigner"); |
| 79 | + init_common(); |
| 80 | + AmplifyAssetDeploymentHandler = class extends ResourceHandler { |
| 81 | + constructor(amplify2, s32, event) { |
| 82 | + super(event); |
| 83 | + this.props = parseProps(this.event.ResourceProperties); |
| 84 | + this.amplify = amplify2; |
| 85 | + this.s3 = s32; |
| 86 | + } |
| 87 | + // ------ |
| 88 | + // CREATE |
| 89 | + // ------ |
| 90 | + async onCreate() { |
| 91 | + console.log("deploying to Amplify with options:", JSON.stringify(this.props, void 0, 2)); |
| 92 | + const jobs = await this.amplify.listJobs({ |
| 93 | + appId: this.props.AppId, |
| 94 | + branchName: this.props.BranchName, |
| 95 | + maxResults: 1 |
| 96 | + }); |
| 97 | + if (jobs.jobSummaries && jobs.jobSummaries.find((summary) => summary.status === "PENDING")) { |
| 98 | + return Promise.reject("Amplify job already running. Aborting deployment."); |
| 99 | + } |
| 100 | + const command = new import_client_s3.GetObjectCommand({ |
| 101 | + Bucket: this.props.S3BucketName, |
| 102 | + Key: this.props.S3ObjectKey |
| 103 | + }); |
| 104 | + const assetUrl = await (0, import_s3_request_presigner.getSignedUrl)(this.s3, command); |
| 105 | + const deployment = await this.amplify.startDeployment({ |
| 106 | + appId: this.props.AppId, |
| 107 | + branchName: this.props.BranchName, |
| 108 | + sourceUrl: assetUrl |
| 109 | + }); |
| 110 | + return { |
| 111 | + AmplifyJobId: deployment.jobSummary?.jobId |
| 112 | + }; |
| 113 | + } |
| 114 | + async isCreateComplete() { |
| 115 | + return this.isActive(this.event.AmplifyJobId); |
| 116 | + } |
| 117 | + // ------ |
| 118 | + // DELETE |
| 119 | + // ------ |
| 120 | + async onDelete() { |
| 121 | + return; |
| 122 | + } |
| 123 | + async isDeleteComplete() { |
| 124 | + return { |
| 125 | + IsComplete: true |
| 126 | + }; |
| 127 | + } |
| 128 | + // ------ |
| 129 | + // UPDATE |
| 130 | + // ------ |
| 131 | + async onUpdate() { |
| 132 | + return this.onCreate(); |
| 133 | + } |
| 134 | + async isUpdateComplete() { |
| 135 | + return this.isActive(this.event.AmplifyJobId); |
| 136 | + } |
| 137 | + async isActive(jobId) { |
| 138 | + if (!jobId) { |
| 139 | + throw new Error("Unable to determine Amplify job status without job id"); |
| 140 | + } |
| 141 | + const job = await this.amplify.getJob({ |
| 142 | + appId: this.props.AppId, |
| 143 | + branchName: this.props.BranchName, |
| 144 | + jobId |
| 145 | + }); |
| 146 | + if (job.job?.summary?.status === "SUCCEED") { |
| 147 | + return { |
| 148 | + IsComplete: true, |
| 149 | + Data: { |
| 150 | + JobId: jobId, |
| 151 | + Status: job.job.summary.status |
| 152 | + } |
| 153 | + }; |
| 154 | + } |
| 155 | + if (job.job?.summary?.status === "FAILED" || job.job?.summary?.status === "CANCELLED") { |
| 156 | + throw new Error(`Amplify job failed with status: ${job.job?.summary?.status}`); |
| 157 | + } else { |
| 158 | + return { |
| 159 | + IsComplete: false |
| 160 | + }; |
| 161 | + } |
| 162 | + } |
| 163 | + }; |
| 164 | + } |
| 165 | +}); |
| 166 | + |
| 167 | +// packages/@aws-cdk/aws-amplify-alpha/lib/asset-deployment-handler/index.js |
| 168 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 169 | +exports.isComplete = exports.onEvent = void 0; |
| 170 | +var client_amplify_1 = require("@aws-sdk/client-amplify"); |
| 171 | +var client_s3_1 = require("@aws-sdk/client-s3"); |
| 172 | +var handler_1 = (init_handler(), __toCommonJS(handler_exports)); |
| 173 | +var AMPLIFY_ASSET_DEPLOYMENT_RESOURCE_TYPE = "Custom::AmplifyAssetDeployment"; |
| 174 | +var sdkConfig = { logger: console }; |
| 175 | +var amplify = new client_amplify_1.Amplify(sdkConfig); |
| 176 | +var s3 = new client_s3_1.S3(sdkConfig); |
| 177 | +async function onEvent(event) { |
| 178 | + const provider = createResourceHandler(event); |
| 179 | + return provider.onEvent(); |
| 180 | +} |
| 181 | +exports.onEvent = onEvent; |
| 182 | +async function isComplete(event) { |
| 183 | + const provider = createResourceHandler(event); |
| 184 | + return provider.isComplete(); |
| 185 | +} |
| 186 | +exports.isComplete = isComplete; |
| 187 | +function createResourceHandler(event) { |
| 188 | + switch (event.ResourceType) { |
| 189 | + case AMPLIFY_ASSET_DEPLOYMENT_RESOURCE_TYPE: |
| 190 | + return new handler_1.AmplifyAssetDeploymentHandler(amplify, s3, event); |
| 191 | + default: |
| 192 | + throw new Error(`Unsupported resource type "${event.ResourceType}"`); |
| 193 | + } |
| 194 | +} |
0 commit comments