@aws-amplify/cli-internal
Version:
Amplify CLI
163 lines • 11.7 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.TS = exports.newLineIdentifier = void 0;
const typescript_1 = __importDefault(require("typescript"));
const prettier = __importStar(require("prettier"));
const factory = typescript_1.default.factory;
const printer = typescript_1.default.createPrinter({ newLine: typescript_1.default.NewLineKind.LineFeed });
const sourceFile = typescript_1.default.createSourceFile('output.ts', '', typescript_1.default.ScriptTarget.Latest, false, typescript_1.default.ScriptKind.TS);
exports.newLineIdentifier = factory.createIdentifier('\n');
class TS {
static printNodes(nodes, printWidth) {
const raw = printer.printList(typescript_1.default.ListFormat.MultiLine, nodes, sourceFile);
return prettier.format(raw, {
parser: 'typescript',
singleQuote: true,
tabWidth: 2,
...(printWidth !== undefined && { printWidth }),
});
}
static printNode(node) {
return printer.printNode(typescript_1.default.EmitHint.Unspecified, node, sourceFile);
}
static declareConst(name, initializer) {
return factory.createVariableStatement([], factory.createVariableDeclarationList([factory.createVariableDeclaration(name, undefined, undefined, initializer)], typescript_1.default.NodeFlags.Const));
}
static propAccess(root, ...segments) {
let expr = typeof root === 'string' ? factory.createIdentifier(root) : root;
for (const segment of segments) {
expr = factory.createPropertyAccessExpression(expr, factory.createIdentifier(segment));
}
return expr;
}
static constFromBackend(name, ...path) {
return TS.declareConst(name, TS.propAccess('backend', ...path));
}
static namedImport(source, ...identifiers) {
const specifiers = identifiers.map((id) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(id)));
return factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports(specifiers)), factory.createStringLiteral(source));
}
static typeImport(source, ...identifiers) {
const specifiers = identifiers.map((id) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(id)));
return factory.createImportDeclaration(undefined, factory.createImportClause(true, undefined, factory.createNamedImports(specifiers)), factory.createStringLiteral(source));
}
static namespaceImport(alias, source) {
return factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamespaceImport(factory.createIdentifier(alias))), factory.createStringLiteral(source));
}
static castAssign(expr, typeName, prop, value) {
return factory.createExpressionStatement(factory.createAssignment(factory.createPropertyAccessExpression(factory.createParenthesizedExpression(factory.createAsExpression(expr, factory.createTypeReferenceNode(typeName))), factory.createIdentifier(prop)), value));
}
static stringProp(key, value) {
return factory.createPropertyAssignment(factory.createIdentifier(key), factory.createStringLiteral(value));
}
static enumProp(key, enumName, value) {
return factory.createPropertyAssignment(factory.createIdentifier(key), factory.createPropertyAccessExpression(factory.createIdentifier(enumName), factory.createIdentifier(value)));
}
static exportedFunction(name, body, extraParams) {
const params = [
factory.createParameterDeclaration(undefined, undefined, 'backend', undefined, factory.createTypeReferenceNode('Backend')),
...(extraParams !== null && extraParams !== void 0 ? extraParams : []),
];
return factory.createFunctionDeclaration([factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], undefined, name, undefined, params, undefined, factory.createBlock([...body], true));
}
static assignProp(target, property, value) {
return factory.createExpressionStatement(factory.createAssignment(factory.createPropertyAccessExpression(factory.createIdentifier(target), factory.createIdentifier(property)), TS.jsValue(value)));
}
static jsValue(value) {
if (value === undefined)
return factory.createIdentifier('undefined');
if (typeof value === 'boolean')
return value ? factory.createTrue() : factory.createFalse();
if (typeof value === 'number')
return factory.createNumericLiteral(value);
if (typeof value === 'string')
return factory.createStringLiteral(value);
if (Array.isArray(value))
return factory.createArrayLiteralExpression(value.map((v) => factory.createStringLiteral(v)));
if (typeof value === 'object') {
const props = Object.entries(value).map(([key, val]) => factory.createPropertyAssignment(key, TS.jsValue(val)));
return factory.createObjectLiteralExpression(props, true);
}
return factory.createIdentifier('undefined');
}
static createBranchNameDeclaration() {
return TS.declareConst('branchName', factory.createIdentifier('process.env.AWS_BRANCH ?? "sandbox"'));
}
static retentionLoop(stackNodeExpr, types) {
const cfnResourceId = factory.createIdentifier('cfnResource');
const isCfnResourceCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier('CfnResource'), factory.createIdentifier('isCfnResource')), undefined, [factory.createIdentifier('c')]);
let filterCondition;
if (types.length === 1) {
filterCondition = factory.createBinaryExpression(factory.createPropertyAccessExpression(factory.createIdentifier('c'), factory.createIdentifier('cfnResourceType')), typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken, factory.createStringLiteral(types[0]));
}
else {
filterCondition = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createArrayLiteralExpression(types.map((t) => factory.createStringLiteral(t))), factory.createIdentifier('includes')), undefined, [factory.createPropertyAccessExpression(factory.createIdentifier('c'), factory.createIdentifier('cfnResourceType'))]);
}
const combinedCondition = factory.createBinaryExpression(isCfnResourceCall, typescript_1.default.SyntaxKind.AmpersandAmpersandToken, filterCondition);
const filterCallback = factory.createArrowFunction(undefined, undefined, [factory.createParameterDeclaration(undefined, undefined, 'c')], undefined, factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), combinedCondition);
const iterableExpr = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createCallExpression(factory.createPropertyAccessExpression(stackNodeExpr, factory.createIdentifier('findAll')), undefined, []), factory.createIdentifier('filter')), undefined, [filterCallback]);
const updateOverride = factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createParenthesizedExpression(factory.createAsExpression(cfnResourceId, factory.createTypeReferenceNode('CfnResource'))), factory.createIdentifier('addOverride')), undefined, [factory.createStringLiteral('UpdateReplacePolicy'), factory.createStringLiteral('Retain')]));
const deletionOverride = factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createParenthesizedExpression(factory.createAsExpression(cfnResourceId, factory.createTypeReferenceNode('CfnResource'))), factory.createIdentifier('addOverride')), undefined, [factory.createStringLiteral('DeletionPolicy'), factory.createStringLiteral('Retain')]));
return factory.createForOfStatement(undefined, factory.createVariableDeclarationList([factory.createVariableDeclaration(cfnResourceId)], typescript_1.default.NodeFlags.Const), iterableExpr, factory.createBlock([updateOverride, deletionOverride], true));
}
static extractFilePathFromHandler(handler) {
const lastDotIndex = handler.lastIndexOf('.');
if (lastDotIndex === -1) {
return `./${handler}.js`;
}
return `./${handler.substring(0, lastDotIndex)}.js`;
}
static renderResourceTsFile({ additionalImportedBackendIdentifiers = {}, backendFunctionConstruct, functionCallParameter, exportedVariableName, postImportStatements, postExportStatements, }) {
const backendFunctionIdentifier = factory.createIdentifier(backendFunctionConstruct);
const importStatements = TS.renderImportStatements(additionalImportedBackendIdentifiers);
const functionCall = factory.createCallExpression(backendFunctionIdentifier, undefined, [functionCallParameter]);
const exportedVariable = factory.createVariableDeclaration(exportedVariableName, undefined, undefined, functionCall);
const exportStatement = factory.createVariableStatement([factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], factory.createVariableDeclarationList([exportedVariable], typescript_1.default.NodeFlags.Const));
return factory.createNodeArray([
...importStatements,
...(postImportStatements !== undefined && postImportStatements.length > 0 ? [exports.newLineIdentifier, ...postImportStatements] : []),
exports.newLineIdentifier,
exportStatement,
...(postExportStatements !== undefined && postExportStatements.length > 0 ? [exports.newLineIdentifier, ...postExportStatements] : []),
]);
}
static renderImportStatements(additionalImportedBackendIdentifiers) {
const importStatements = [];
for (const [packageName, identifiers] of Object.entries(additionalImportedBackendIdentifiers)) {
const importSpecifiers = [];
identifiers.forEach((identifier) => {
importSpecifiers.push(factory.createImportSpecifier(false, undefined, factory.createIdentifier(identifier)));
});
importStatements.push(factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports(importSpecifiers)), factory.createStringLiteral(packageName)));
}
return importStatements;
}
}
exports.TS = TS;
//# sourceMappingURL=ts.js.map