UNPKG

@aws-amplify/cli-internal

Version:
331 lines • 21.3 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AmplifyHelperTransformer = void 0; const ts = __importStar(require("typescript")); class AmplifyHelperTransformer { static transform(sourceFile, projectName) { const projectInfoVariables = new Set(); const amplifyResourcePropsParams = new Set(); let hasDependencies = false; const dependencyVariables = new Set(); const removedModuleIdentifiers = new Set(); const transformer = (context) => { return (node) => { function visit(node) { var _a, _b; if (ts.isImportDeclaration(node)) { const moduleSpecifier = node.moduleSpecifier; if (ts.isStringLiteral(moduleSpecifier)) { if (moduleSpecifier.text.includes('amplify-dependent-resources-ref')) { if (node.importClause) { if (node.importClause.name && ts.isIdentifier(node.importClause.name)) { removedModuleIdentifiers.add(node.importClause.name.text); } if (node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings)) { for (const specifier of node.importClause.namedBindings.elements) { removedModuleIdentifiers.add(specifier.name.text); } } } return undefined; } if (moduleSpecifier.text === '@aws-amplify/cli-extensibility-helper') { return undefined; } if (moduleSpecifier.text.startsWith('@aws-cdk/')) { const v2Module = moduleSpecifier.text.replace('@aws-cdk/', 'aws-cdk-lib/'); return ts.factory.updateImportDeclaration(node, node.modifiers, node.importClause, ts.factory.createStringLiteral(v2Module), node.assertClause); } } } if (ts.isCallExpression(node)) { const expression = node.expression; if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.name) && expression.name.text === 'ref') { const isCdkFnRef = ts.isPropertyAccessExpression(expression.expression) && ts.isIdentifier(expression.expression.name) && expression.expression.name.text === 'Fn'; const isFnRef = ts.isIdentifier(expression.expression) && expression.expression.text === 'Fn'; if ((isCdkFnRef || isFnRef) && node.arguments.length === 1) { const arg = node.arguments[0]; if (ts.isStringLiteral(arg) && arg.text === 'env') { return ts.factory.createIdentifier('branchName'); } if (ts.isPropertyAccessExpression(arg)) { const chain = AmplifyHelperTransformer.getPropertyAccessChain(arg); const parts = chain.split('.'); if (parts.length >= 4 && dependencyVariables.has(parts[0])) { const gen1Category = parts[1]; const gen2Category = AmplifyHelperTransformer.CATEGORY_MAP[gen1Category] || gen1Category; const attribute = parts[3]; const mappedAttr = (_a = AmplifyHelperTransformer.ATTRIBUTE_MAP[gen1Category]) === null || _a === void 0 ? void 0 : _a[attribute]; const gen2Path = mappedAttr || attribute; if (gen1Category === 'function') { return AmplifyHelperTransformer.createPropertyAccessFromString(`backend.${gen2Category}.${parts[2]}.resources.${gen2Path}`); } return AmplifyHelperTransformer.createPropertyAccessFromString(`backend.${gen2Category}.resources.${gen2Path}`); } } } } } if (ts.isVariableStatement(node)) { const declaration = node.declarationList.declarations[0]; if (declaration && declaration.initializer && ts.isCallExpression(declaration.initializer) && ts.isPropertyAccessExpression(declaration.initializer.expression) && ts.isIdentifier(declaration.initializer.expression.expression) && declaration.initializer.expression.expression.text === 'AmplifyHelpers' && declaration.initializer.expression.name.text === 'getProjectInfo' && ts.isIdentifier(declaration.name)) { projectInfoVariables.add(declaration.name.text); return undefined; } if (declaration && declaration.initializer && ts.isCallExpression(declaration.initializer)) { const callExpr = declaration.initializer; const isAddResourceDependency = ts.isPropertyAccessExpression(callExpr.expression) && callExpr.expression.name.text === 'addResourceDependency'; if (isAddResourceDependency) { if (ts.isIdentifier(declaration.name)) { dependencyVariables.add(declaration.name.text); } hasDependencies = true; return undefined; } } if (declaration && declaration.initializer && ts.isNewExpression(declaration.initializer)) { const newExpr = declaration.initializer; const exprText = newExpr.expression; const isCfnParameter = (ts.isPropertyAccessExpression(exprText) && exprText.name.text === 'CfnParameter') || (ts.isIdentifier(exprText) && exprText.text === 'CfnParameter'); if (isCfnParameter && newExpr.arguments && newExpr.arguments.length >= 2) { const secondArg = newExpr.arguments[1]; if (ts.isStringLiteral(secondArg) && secondArg.text === 'env') { return undefined; } } } if (declaration && declaration.type && ts.isTypeReferenceNode(declaration.type)) { const typeName = declaration.type.typeName; if (ts.isIdentifier(typeName) && typeName.text === 'AmplifyDependentResourcesAttributes') { return undefined; } } if (declaration && declaration.initializer && ts.isCallExpression(declaration.initializer) && ts.isIdentifier(declaration.initializer.expression) && removedModuleIdentifiers.has(declaration.initializer.expression.text)) { if (ts.isIdentifier(declaration.name)) { dependencyVariables.add(declaration.name.text); hasDependencies = true; } return undefined; } if (declaration && declaration.initializer && ts.isAsExpression(declaration.initializer)) { const asExpr = declaration.initializer; if (ts.isTypeReferenceNode(asExpr.type)) { const typeName = asExpr.type.typeName; if (ts.isIdentifier(typeName) && typeName.text === 'AmplifyDependentResourcesAttributes') { const updatedDeclaration = ts.factory.updateVariableDeclaration(declaration, declaration.name, declaration.exclamationToken, declaration.type, asExpr.expression); const updatedDeclarationList = ts.factory.updateVariableDeclarationList(node.declarationList, [updatedDeclaration]); return ts.factory.updateVariableStatement(node, node.modifiers, updatedDeclarationList); } } } } if (ts.isPropertyAccessExpression(node)) { const expression = node.expression; if (ts.isCallExpression(expression) && ts.isPropertyAccessExpression(expression.expression) && ts.isIdentifier(expression.expression.expression) && expression.expression.expression.text === 'AmplifyHelpers' && expression.expression.name.text === 'getProjectInfo') { const propertyName = node.name.text; if (propertyName === 'envName') return ts.factory.createIdentifier('branchName'); if (propertyName === 'projectName') return ts.factory.createIdentifier('projectName'); } if (ts.isIdentifier(expression) && projectInfoVariables.has(expression.text)) { const propertyName = node.name.text; if (propertyName === 'envName') return ts.factory.createIdentifier('branchName'); if (propertyName === 'projectName') return ts.factory.createIdentifier('projectName'); } if (ts.isIdentifier(expression) && amplifyResourcePropsParams.has(expression.text)) { const propertyName = node.name.text; if (propertyName === 'resourceName') return ts.factory.createIdentifier('id'); if (propertyName === 'category') return ts.factory.createStringLiteral('custom'); } if (!ts.isPropertyAccessExpression(node.parent)) { const fullAccess = AmplifyHelperTransformer.getPropertyAccessChain(node); const parts = fullAccess.split('.'); if (parts.length >= 4 && (parts[0].includes('amplifyResources') || dependencyVariables.has(parts[0]))) { const gen1Category = parts[1]; const gen2Category = AmplifyHelperTransformer.CATEGORY_MAP[gen1Category] || gen1Category; const resourceName = parts[2]; const gen1Attribute = parts[3]; const mappedAttribute = (_b = AmplifyHelperTransformer.ATTRIBUTE_MAP[gen1Category]) === null || _b === void 0 ? void 0 : _b[gen1Attribute]; const gen2Property = mappedAttribute || parts.slice(3).join('.'); if (gen1Category === 'function') { return AmplifyHelperTransformer.createPropertyAccessFromString(`backend.${gen2Category}.${resourceName}.resources.${gen2Property}`); } return AmplifyHelperTransformer.createPropertyAccessFromString(`backend.${gen2Category}.resources.${gen2Property}`); } } } if (ts.isConstructorDeclaration(node)) { node.parameters.forEach((param) => { if (param.type && ts.isTypeReferenceNode(param.type) && ts.isIdentifier(param.name)) { const typeText = param.type.getText(); if (typeText.includes('AmplifyResourceProps')) { amplifyResourcePropsParams.add(param.name.text); } } }); } if (ts.isExpressionStatement(node)) { const expr = node.expression; if (ts.isNewExpression(expr)) { const exprText = expr.expression; const isCfnParameter = (ts.isPropertyAccessExpression(exprText) && exprText.name.text === 'CfnParameter') || (ts.isIdentifier(exprText) && exprText.text === 'CfnParameter'); if (isCfnParameter && expr.arguments && expr.arguments.length >= 2) { const secondArg = expr.arguments[1]; if (ts.isStringLiteral(secondArg) && secondArg.text === 'env') { return undefined; } } } } const visitedNode = ts.visitEachChild(node, visit, context); if (ts.isClassDeclaration(visitedNode) && visitedNode.heritageClauses) { const newHeritageClauses = visitedNode.heritageClauses.map((clause) => { const newTypes = clause.types.map((type) => { const typeText = type.expression.getText(); if (typeText.endsWith('.Stack') || typeText.endsWith('.NestedStack')) { return ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier('Construct'), type.typeArguments); } return type; }); return ts.factory.updateHeritageClause(clause, newTypes); }); return ts.factory.updateClassDeclaration(visitedNode, visitedNode.modifiers, visitedNode.name, visitedNode.typeParameters, newHeritageClauses, visitedNode.members); } if (ts.isConstructorDeclaration(visitedNode)) { const baseParams = visitedNode.parameters.slice(0, 2); if (hasDependencies) { const backendParam = ts.factory.createParameterDeclaration(undefined, undefined, 'backend', undefined, ts.factory.createTypeReferenceNode('Backend', undefined), undefined); baseParams.push(backendParam); } return ts.factory.updateConstructorDeclaration(visitedNode, visitedNode.modifiers, baseParams, visitedNode.body); } if (ts.isCallExpression(visitedNode) && visitedNode.expression.kind === ts.SyntaxKind.SuperKeyword) { const newArgs = visitedNode.arguments.slice(0, 2); return ts.factory.updateCallExpression(visitedNode, visitedNode.expression, visitedNode.typeArguments, newArgs); } return visitedNode; } return ts.visitNode(node, visit); }; }; const result = ts.transform(sourceFile, [transformer]); return result.transformed[0]; } static addBranchNameVariable(sourceFile, projectName) { const hasBranchName = sourceFile.statements.some((stmt) => ts.isVariableStatement(stmt) && stmt.declarationList.declarations.some((decl) => ts.isIdentifier(decl.name) && decl.name.text === 'branchName')); const hasProjectName = sourceFile.statements.some((stmt) => ts.isVariableStatement(stmt) && stmt.declarationList.declarations.some((decl) => ts.isIdentifier(decl.name) && decl.name.text === 'projectName')); const branchNameDeclaration = ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([ ts.factory.createVariableDeclaration('branchName', undefined, undefined, ts.factory.createBinaryExpression(ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('process'), 'env'), 'AWS_BRANCH'), ts.SyntaxKind.QuestionQuestionToken, ts.factory.createStringLiteral('sandbox'))), ], ts.NodeFlags.Const)); const projectNameDeclaration = projectName ? ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration('projectName', undefined, undefined, ts.factory.createStringLiteral(projectName))], ts.NodeFlags.Const)) : undefined; const newStatements = []; newStatements.push(...sourceFile.statements.filter((statement) => ts.isImportDeclaration(statement))); if (!hasBranchName) newStatements.push(branchNameDeclaration); if (!hasProjectName && projectNameDeclaration) newStatements.push(projectNameDeclaration); newStatements.push(...sourceFile.statements.filter((statement) => !ts.isImportDeclaration(statement))); return ts.factory.updateSourceFile(sourceFile, newStatements); } static getPropertyAccessChain(node) { const parts = []; let current = node; while (ts.isPropertyAccessExpression(current)) { parts.unshift(current.name.text); current = current.expression; } if (ts.isIdentifier(current)) { parts.unshift(current.text); } return parts.join('.'); } static createPropertyAccessFromString(accessPath) { const parts = accessPath.split('.'); let expression = ts.factory.createIdentifier(parts[0]); for (let i = 1; i < parts.length; i++) { expression = ts.factory.createPropertyAccessExpression(expression, parts[i]); } return expression; } } exports.AmplifyHelperTransformer = AmplifyHelperTransformer; AmplifyHelperTransformer.CATEGORY_MAP = { function: 'functions', api: 'data', storage: 'storage', auth: 'auth', }; AmplifyHelperTransformer.ATTRIBUTE_MAP = { auth: { UserPoolId: 'userPool.userPoolId', UserPoolArn: 'userPool.userPoolArn', IdentityPoolId: 'identityPool.identityPoolId', AppClientID: 'userPoolClient.userPoolClientId', AppClientIDWeb: 'userPoolClient.userPoolClientId', }, api: { GraphQLAPIIdOutput: 'cfnResources.cfnGraphqlApi.attrApiId', GraphQLAPIEndpointOutput: 'cfnResources.cfnGraphqlApi.attrGraphQlUrl', GraphQLAPIKeyOutput: 'cfnResources.cfnApiKey.Default.attrApiKey', }, storage: { BucketName: 'bucket.bucketName', }, function: { Name: 'lambda.functionName', Arn: 'lambda.functionArn', LambdaExecutionRole: 'lambda.role', }, }; //# sourceMappingURL=amplify-helper-transformer.js.map