@aws-amplify/cli-internal
Version:
Amplify CLI
147 lines • 7.85 kB
JavaScript
;
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.AnalyticsKinesisGenerator = exports.DEFINE_ANALYTICS_VARIABLE_NAME = void 0;
const node_path_1 = __importDefault(require("node:path"));
const promises_1 = __importDefault(require("node:fs/promises"));
const cdkFromCfn = __importStar(require("cdk-from-cfn"));
const cfn_condition_resolver_1 = require("../../../refactor/resolvers/cfn-condition-resolver");
const client_cloudformation_1 = require("@aws-sdk/client-cloudformation");
const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core");
const ts_1 = require("../../ts");
const kinesis_renderer_1 = require("./kinesis.renderer");
const prettier = __importStar(require("prettier"));
exports.DEFINE_ANALYTICS_VARIABLE_NAME = 'analyticsResult';
class AnalyticsKinesisGenerator {
constructor(gen1App, backendGenerator, outputDir, resource, logger) {
this.gen1App = gen1App;
this.backendGenerator = backendGenerator;
this.outputDir = outputDir;
this.resource = resource;
this.renderer = new kinesis_renderer_1.AnalyticsRenderer(resource);
this.logger = logger;
}
async plan() {
const resourceMeta = this.gen1App.resourceMeta(this.resource);
const logicalId = resourceMeta.providerMetadata.logicalId;
const resourceName = this.resource.resourceName;
const constructFileName = AnalyticsKinesisGenerator.fileName(resourceName);
const constructClassName = AnalyticsKinesisGenerator.className(resourceName);
const constructFilePath = node_path_1.default.join(this.outputDir, 'amplify', 'analytics', `${constructFileName}.ts`);
const analyticsDir = node_path_1.default.join(this.outputDir, 'amplify', 'analytics');
return [
{
resource: this.resource,
validate: () => undefined,
describe: async () => [`Generate amplify/analytics/${constructFileName}.ts`],
execute: async () => {
const template = this.gen1App.json(`analytics/${resourceName}/kinesis-cloudformation-template.json`);
const parameters = await this.fetchNestedStackParameters(logicalId);
const finalTemplate = await preTransmute(template, parameters);
const tsFile = cdkFromCfn.transmute(JSON.stringify(finalTemplate), 'typescript', constructClassName, 'construct');
const formatted = prettier.format(tsFile, {
parser: 'typescript',
singleQuote: true,
tabWidth: 2,
printWidth: 80,
});
this.logger.info(`Rendering analytics/${constructFileName}.ts`);
await promises_1.default.mkdir(node_path_1.default.dirname(constructFilePath), { recursive: true });
await promises_1.default.writeFile(constructFilePath, formatted, 'utf-8');
},
},
{
resource: this.resource,
validate: () => undefined,
describe: async () => [`Generate amplify/analytics/resource.ts`],
execute: async () => {
const shardCount = parseInt(this.gen1App.resourceMetaOutput(this.resource, 'kinesisStreamShardCount'), 10);
const streamName = this.gen1App.resourceMetaOutput(this.resource, 'kinesisStreamId');
this.logger.info('Rendering analytics/resource.ts');
const nodes = this.renderer.render({
constructClassName,
constructFileName,
shardCount,
streamName,
});
const content = ts_1.TS.printNodes(nodes);
await promises_1.default.mkdir(analyticsDir, { recursive: true });
await promises_1.default.writeFile(node_path_1.default.join(analyticsDir, 'resource.ts'), content, 'utf-8');
this.backendGenerator.addNamespaceImport('analytics', './analytics/resource');
this.backendGenerator.addPostDefineBackendCall(exports.DEFINE_ANALYTICS_VARIABLE_NAME, `analytics.defineAnalytics(backend)`);
this.backendGenerator.addPostRefactorCall(`analytics.postRefactor(${exports.DEFINE_ANALYTICS_VARIABLE_NAME});`);
},
},
];
}
static className(resourceName) {
return resourceName.charAt(0).toUpperCase() + resourceName.slice(1);
}
static fileName(resourceName) {
return `${resourceName}-construct`.toLowerCase();
}
async fetchNestedStackParameters(logicalId) {
var _a, _b, _c;
this.logger.debug(`Fetching nested stack parameters for '${logicalId}'`);
const nestedStackName = await this.gen1App.aws.findResourcePhysicalId(this.gen1App.rootStackName, logicalId);
if (!nestedStackName) {
throw new amplify_cli_core_1.AmplifyError('NestedStackNotFoundError', {
message: `Nested stack not found for logical ID '${logicalId}' in stack '${this.gen1App.rootStackName}'`,
resolution: 'Verify the CloudFormation stack exists and has not been manually modified.',
});
}
const stacksResponse = await this.gen1App.clients.cloudFormation.send(new client_cloudformation_1.DescribeStacksCommand({ StackName: nestedStackName }));
return (_c = (_b = (_a = stacksResponse.Stacks) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.Parameters) !== null && _c !== void 0 ? _c : [];
}
}
exports.AnalyticsKinesisGenerator = AnalyticsKinesisGenerator;
async function preTransmute(template, parameters) {
var _a;
const result = JSON.parse(JSON.stringify(template));
if ((_a = result.Parameters) === null || _a === void 0 ? void 0 : _a.env) {
result.Parameters['branchName'] = result.Parameters.env;
delete result.Parameters.env;
}
const updateRefs = (obj) => {
if (typeof obj === 'object' && obj !== null) {
const record = obj;
if (record.Ref === 'env') {
record.Ref = 'branchName';
}
Object.values(record).forEach(updateRefs);
}
};
updateRefs(result.Resources);
if (parameters.length > 0) {
const resolved = (0, cfn_condition_resolver_1.resolveConditions)(result, parameters);
delete resolved.Conditions;
return resolved;
}
return result;
}
//# sourceMappingURL=kinesis.generator.js.map