@aws-amplify/cli-internal
Version:
Amplify CLI
104 lines • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveConditions = void 0;
const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core");
const cfn_template_1 = require("../../_common/cfn-template");
function resolveConditions(template, parameters) {
const conditions = template.Conditions;
if (!conditions || Object.keys(conditions).length === 0)
return template;
const conditionValues = new Map();
for (const [conditionKey, conditionDef] of Object.entries(conditions)) {
const fnType = Object.keys(conditionDef)[0];
if (!Object.values(cfn_template_1.CFNFunction).includes(fnType))
continue;
const statements = conditionDef[fnType];
if ((fnType === cfn_template_1.CFNFunction.Or || fnType === cfn_template_1.CFNFunction.And) && statements.length > 2) {
throw new amplify_cli_core_1.AmplifyError('CFNConditionError', {
message: `${fnType} with ${statements.length} operands is not supported (condition '${conditionKey}'). Only 2 operands are handled.`,
});
}
const [left, right] = statements;
conditionValues.set(conditionKey, evaluateCondition(conditions, left, right, parameters, fnType));
}
const resolved = JSON.parse(JSON.stringify(template));
for (const [, resource] of Object.entries(resolved.Resources)) {
for (const [propName, propValue] of Object.entries(resource.Properties)) {
if (typeof propValue === 'object' && propValue !== null && !Array.isArray(propValue)) {
resource.Properties[propName] = resolveIfCondition(propValue, conditionValues);
}
else if (Array.isArray(propValue)) {
resource.Properties[propName] = propValue.map((item) => typeof item === 'object' && item !== null ? resolveIfCondition(item, conditionValues) : item);
}
}
}
for (const [logicalId, resource] of Object.entries(resolved.Resources)) {
const condition = resource.Condition;
if (condition && conditionValues.has(condition) && !conditionValues.get(condition)) {
delete resolved.Resources[logicalId];
}
}
return resolved;
}
exports.resolveConditions = resolveConditions;
function resolveStatement(conditions, statement, parameters) {
var _a;
if (typeof statement !== 'object')
return statement;
const record = statement;
if ('Condition' in record) {
const nestedName = record.Condition;
const nestedDef = conditions[nestedName];
const nestedFnType = Object.keys(nestedDef)[0];
const nestedStatements = nestedDef[nestedFnType];
return evaluateCondition(conditions, nestedStatements[0], nestedStatements[1], parameters, nestedFnType);
}
const fnKey = Object.keys(record).find((k) => Object.values(cfn_template_1.CFNFunction).includes(k));
if (fnKey) {
const nestedStatements = record[fnKey];
return evaluateCondition(conditions, nestedStatements[0], nestedStatements[1], parameters, fnKey);
}
if ('Ref' in record) {
const paramKey = record.Ref;
const value = (_a = parameters.find((p) => p.ParameterKey === paramKey)) === null || _a === void 0 ? void 0 : _a.ParameterValue;
if (value === undefined) {
throw new amplify_cli_core_1.AmplifyError('CFNConditionError', {
message: `Condition references parameter '${paramKey}' but no value was provided`,
});
}
return value;
}
throw new amplify_cli_core_1.AmplifyError('CFNConditionError', {
message: `Unsupported condition statement: ${JSON.stringify(statement)}`,
});
}
function evaluateCondition(conditions, left, right, parameters, fnType) {
const resolvedLeft = resolveStatement(conditions, left, parameters);
const resolvedRight = right !== undefined ? resolveStatement(conditions, right, parameters) : undefined;
switch (fnType) {
case cfn_template_1.CFNFunction.Equals:
return resolvedLeft === resolvedRight;
case cfn_template_1.CFNFunction.Not:
return !resolvedLeft;
case cfn_template_1.CFNFunction.Or:
return !!(resolvedLeft || resolvedRight);
case cfn_template_1.CFNFunction.And:
return !!(resolvedLeft && resolvedRight);
default:
throw new amplify_cli_core_1.AmplifyError('CFNConditionError', {
message: `Unsupported condition function: ${fnType}`,
});
}
}
function resolveIfCondition(propValue, conditionValues) {
const record = propValue;
if (cfn_template_1.CFNFunction.If in record) {
const ifCondition = record[cfn_template_1.CFNFunction.If];
const conditionName = ifCondition[0];
if (conditionValues.has(conditionName)) {
return (conditionValues.get(conditionName) ? ifCondition[1] : ifCondition[2]);
}
}
return propValue;
}
//# sourceMappingURL=cfn-condition-resolver.js.map