|
| 1 | +import { IntrinsicFunction } from '../types/cloudFormation'; |
| 2 | +import fs from 'fs'; |
| 3 | +import { Substitutions } from '../types/plugin'; |
| 4 | +import { Api } from './Api'; |
| 5 | + |
| 6 | +type JsResolverConfig = { |
| 7 | + path: string; |
| 8 | + substitutions?: Substitutions; |
| 9 | +}; |
| 10 | + |
| 11 | +export class JsResolver { |
| 12 | + constructor(private api: Api, private config: JsResolverConfig) {} |
| 13 | + |
| 14 | + compile(): string | IntrinsicFunction { |
| 15 | + if (!fs.existsSync(this.config.path)) { |
| 16 | + throw new this.api.plugin.serverless.classes.Error( |
| 17 | + `The resolver handler file '${this.config.path}' does not exist`, |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + const requestTemplateContent = fs.readFileSync(this.config.path, 'utf8'); |
| 22 | + return this.processTemplateSubstitutions(requestTemplateContent); |
| 23 | + } |
| 24 | + |
| 25 | + processTemplateSubstitutions(template: string): string | IntrinsicFunction { |
| 26 | + const substitutions = { |
| 27 | + ...this.api.config.substitutions, |
| 28 | + ...this.config.substitutions, |
| 29 | + }; |
| 30 | + const availableVariables = Object.keys(substitutions); |
| 31 | + const templateVariables: string[] = []; |
| 32 | + let searchResult; |
| 33 | + const variableSyntax = RegExp(/#([\w\d-_]+)#/g); |
| 34 | + while ((searchResult = variableSyntax.exec(template)) !== null) { |
| 35 | + templateVariables.push(searchResult[1]); |
| 36 | + } |
| 37 | + |
| 38 | + const replacements = availableVariables |
| 39 | + .filter((value) => templateVariables.includes(value)) |
| 40 | + .filter((value, index, array) => array.indexOf(value) === index) |
| 41 | + .reduce( |
| 42 | + (accum, value) => |
| 43 | + Object.assign(accum, { [value]: substitutions[value] }), |
| 44 | + {}, |
| 45 | + ); |
| 46 | + |
| 47 | + // if there are substitutions for this template then add fn:sub |
| 48 | + if (Object.keys(replacements).length > 0) { |
| 49 | + return this.substituteGlobalTemplateVariables(template, replacements); |
| 50 | + } |
| 51 | + |
| 52 | + return template; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates Fn::Join object from given template where all given substitutions |
| 57 | + * are wrapped in Fn::Sub objects. This enables template to have also |
| 58 | + * characters that are not only alphanumeric, underscores, periods, and colons. |
| 59 | + * |
| 60 | + * @param {*} template |
| 61 | + * @param {*} substitutions |
| 62 | + */ |
| 63 | + substituteGlobalTemplateVariables( |
| 64 | + template: string, |
| 65 | + substitutions: Substitutions, |
| 66 | + ): IntrinsicFunction { |
| 67 | + const variables = Object.keys(substitutions).join('|'); |
| 68 | + const regex = new RegExp(`\\#(${variables})#`, 'g'); |
| 69 | + const substituteTemplate = template.replace(regex, '|||$1|||'); |
| 70 | + |
| 71 | + const templateJoin = substituteTemplate |
| 72 | + .split('|||') |
| 73 | + .filter((part) => part !== ''); |
| 74 | + const parts: (string | IntrinsicFunction)[] = []; |
| 75 | + for (let i = 0; i < templateJoin.length; i += 1) { |
| 76 | + if (templateJoin[i] in substitutions) { |
| 77 | + const subs = { [templateJoin[i]]: substitutions[templateJoin[i]] }; |
| 78 | + parts[i] = { 'Fn::Sub': [`\${${templateJoin[i]}}`, subs] }; |
| 79 | + } else { |
| 80 | + parts[i] = templateJoin[i]; |
| 81 | + } |
| 82 | + } |
| 83 | + return { 'Fn::Join': ['', parts] }; |
| 84 | + } |
| 85 | +} |
0 commit comments