UNPKG

@aws-amplify/cli-internal

Version:
104 lines 4.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudFormationService = void 0; const client_cloudformation_1 = require("@aws-sdk/client-cloudformation"); const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core"); const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); const aws_cfn_1 = __importDefault(require("@aws-amplify/amplify-provider-awscloudformation/lib/aws-utils/aws-cfn")); const zip_util_1 = require("@aws-amplify/amplify-provider-awscloudformation/lib/zip-util"); const aws_s3_1 = require("@aws-amplify/amplify-provider-awscloudformation/lib/aws-utils/aws-s3"); const constants_1 = require("@aws-amplify/amplify-provider-awscloudformation/lib/constants"); class CloudFormationService { constructor(print) { this.print = print; } async getClient(context) { const cfn = await new aws_cfn_1.default(context, 'drift:detect'); return cfn.cfn; } async validateStackExists(client, stackName) { var _a; try { await client.send(new client_cloudformation_1.GetTemplateCommand({ StackName: stackName, TemplateStage: 'Original', })); return true; } catch (error) { if (error.name === 'ValidationError' || ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('does not exist'))) { return false; } throw new amplify_cli_core_1.AmplifyError('StackNotFoundError', { message: `Failed to validate stack existence: ${error.message}`, resolution: 'Check your AWS credentials and permissions.', }, error); } } async syncCloudBackendFromS3(context) { try { if (!amplify_cli_core_1.stateManager.metaFileExists()) { this.print.debug('Skipping S3 sync: Project not initialized'); return false; } const amplifyDir = amplify_cli_core_1.pathManager.getAmplifyDirPath(); const tempDir = path.join(amplifyDir, '.temp'); const currentCloudBackendDir = amplify_cli_core_1.pathManager.getCurrentCloudBackendDirPath(); if (!fs.existsSync(currentCloudBackendDir)) { this.print.debug('Skipping S3 sync: No cloud backend directory found'); return false; } const s3 = await aws_s3_1.S3.getInstance(context); let currentCloudBackendZip; try { currentCloudBackendZip = await (0, zip_util_1.downloadZip)(s3, tempDir, constants_1.S3BackendZipFileName, context.amplify.getEnvInfo().envName); } catch (err) { if ((err === null || err === void 0 ? void 0 : err.name) === 'NoSuchBucket') { this.print.debug('Skipping S3 sync: No deployment bucket found'); return false; } this.print.warn(`Warning: Could not sync from S3: ${err.message}`); return false; } const unzippedDir = await (0, zip_util_1.extractZip)(tempDir, currentCloudBackendZip); await fs.remove(currentCloudBackendDir); await fs.move(unzippedDir, currentCloudBackendDir); await fs.remove(tempDir); return true; } catch (error) { this.print.warn(`Warning: S3 sync failed: ${error.message}`); return false; } } } exports.CloudFormationService = CloudFormationService; //# sourceMappingURL=cloudformation-service.js.map