@aws-amplify/cli-internal
Version:
Amplify CLI
76 lines • 4.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackendRenderer = void 0;
const typescript_1 = __importDefault(require("typescript"));
const ts_1 = require("../ts");
const factory = typescript_1.default.factory;
class BackendRenderer {
render(options) {
const nodes = [];
for (const imp of options.namespaceImports) {
nodes.push(this.renderNamespaceImport(imp.alias, imp.source));
}
nodes.push(this.renderDefineBackendImport());
nodes.push(ts_1.TS.namedImport('aws-cdk-lib', 'Tags'));
nodes.push(ts_1.newLineIdentifier);
nodes.push(this.renderDefineBackendCall(options.defineBackendEntries));
nodes.push(ts_1.newLineIdentifier);
nodes.push(this.renderBackendTypeExport());
nodes.push(ts_1.newLineIdentifier);
for (const call of options.postDefineBackendCalls) {
nodes.push(ts_1.TS.declareConst(call.variableName, factory.createIdentifier(call.expression)));
}
for (const statement of options.postDefineBackendStatements) {
nodes.push(factory.createExpressionStatement(factory.createIdentifier(statement)));
}
if (options.postDefineBackendCalls.length > 0 || options.postDefineBackendStatements.length > 0) {
nodes.push(ts_1.newLineIdentifier);
}
for (const entry of options.escapeHatchCalls) {
nodes.push(this.renderEscapeHatchCall(entry));
}
nodes.push(ts_1.newLineIdentifier);
nodes.push(this.renderPostRefactorFunction(options.postRefactorCalls));
nodes.push(ts_1.newLineIdentifier);
nodes.push(this.renderCommentedPostRefactor());
return factory.createNodeArray(nodes);
}
renderNamespaceImport(alias, source) {
return ts_1.TS.namespaceImport(alias, source);
}
renderDefineBackendImport() {
return ts_1.TS.namedImport('@aws-amplify/backend', 'defineBackend');
}
renderDefineBackendCall(entries) {
const properties = entries.map((entry) => factory.createPropertyAssignment(factory.createIdentifier(entry.key), ts_1.TS.propAccess(entry.alias, entry.exportName)));
const objectLiteral = factory.createObjectLiteralExpression(properties, true);
const callExpr = factory.createCallExpression(factory.createIdentifier('defineBackend'), undefined, [objectLiteral]);
return ts_1.TS.declareConst('backend', callExpr);
}
renderBackendTypeExport() {
return factory.createTypeAliasDeclaration([factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], 'Backend', undefined, factory.createTypeQueryNode(factory.createIdentifier('backend')));
}
renderPostRefactorFunction(calls) {
const statements = calls.map((statement) => factory.createExpressionStatement(factory.createIdentifier(statement)));
statements.push(factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier('Tags'), factory.createIdentifier('of')), undefined, [factory.createPropertyAccessExpression(factory.createIdentifier('backend'), factory.createIdentifier('stack'))]), factory.createIdentifier('add')), undefined, [factory.createStringLiteral('gen2-migration/post-refactor'), factory.createStringLiteral('true')])));
return factory.createFunctionDeclaration([factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], undefined, 'postRefactor', undefined, [], undefined, factory.createBlock(statements, true));
}
renderEscapeHatchCall(entry) {
const args = [factory.createIdentifier('backend')];
for (const arg of entry.extraArgs) {
args.push(factory.createIdentifier(arg));
}
return factory.createExpressionStatement(factory.createCallExpression(ts_1.TS.propAccess(entry.alias, 'applyEscapeHatches'), undefined, args));
}
renderCommentedPostRefactor() {
const emptyStatement = factory.createEmptyStatement();
typescript_1.default.addSyntheticLeadingComment(emptyStatement, typescript_1.default.SyntaxKind.SingleLineCommentTrivia, ' Uncomment after refactor', true);
typescript_1.default.addSyntheticLeadingComment(emptyStatement, typescript_1.default.SyntaxKind.SingleLineCommentTrivia, ' postRefactor();', true);
return emptyStatement;
}
}
exports.BackendRenderer = BackendRenderer;
//# sourceMappingURL=backend.renderer.js.map