serverless-pseudo-parameters
Advanced tools
Comparing version 1.2.5 to 1.3.0
'use strict'; | ||
const _ = require('lodash'); | ||
const chalk = require('chalk'); | ||
class ServerlessAWSPseudoParameters { | ||
@@ -14,3 +10,3 @@ constructor(serverless, options) { | ||
}; | ||
this.skipRegionReplace = _.get(serverless.service, 'custom.pseudoParameters.skipRegionReplace', false) | ||
this.skipRegionReplace = get(serverless.service, 'custom.pseudoParameters.skipRegionReplace', false) | ||
} | ||
@@ -20,7 +16,7 @@ | ||
const template = this.serverless.service.provider.compiledCloudFormationTemplate; | ||
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources; | ||
const skipRegionReplace = this.skipRegionReplace; | ||
const consoleLog = this.serverless.cli.consoleLog; | ||
consoleLog(`${chalk.yellow.underline('AWS Pseudo Parameters')}`); | ||
consoleLog(yellow(underline('AWS Pseudo Parameters'))); | ||
@@ -33,8 +29,6 @@ if (skipRegionReplace) { | ||
// reference. If found, replace the value with an Fn::Sub reference | ||
_.forEach(template.Resources, function(resource, identifier){ | ||
replaceChildNodes(resource.Properties, identifier); | ||
}); | ||
Object.keys(resources).forEach(identifier => replaceChildNodes(resources[identifier].Properties, identifier)); | ||
function isDict(v) { | ||
return typeof v==='object' && v!==null && !(v instanceof Array) && !(v instanceof Date); | ||
return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date); | ||
} | ||
@@ -66,43 +60,50 @@ | ||
function replaceChildNodes(dictionary, name) { | ||
_.forEach(dictionary, function(value, key){ | ||
Object.keys(dictionary).forEach((key) => { | ||
// if a region name is mentioned, replace it with a reference (unless we are skipping automatic replacements) | ||
if(typeof value === 'string' && !skipRegionReplace && containsRegion(value)) { | ||
var regionFinder = new RegExp(regions().join("|")); | ||
value = value.replace(regionFinder, '#{AWS::Region}'); | ||
} | ||
let value = dictionary[key]; | ||
// if a region name is mentioned, replace it with a reference (unless we are skipping automatic replacements) | ||
if (typeof value === 'string' && !skipRegionReplace && containsRegion(value)) { | ||
const regionFinder = new RegExp(regions().join("|")); | ||
value = value.replace(regionFinder, '#{AWS::Region}'); | ||
} | ||
// we only want to possibly replace strings with an Fn::Sub | ||
if(typeof value === 'string' && value.search(/#{AWS::([a-zA-Z]+)}/) >= 0) { | ||
var aws_regex = /#{AWS::([a-zA-Z]+)}/g; | ||
var m; | ||
// we only want to possibly replace strings with an Fn::Sub | ||
if (typeof value === 'string' && value.search(/#{AWS::([a-zA-Z]+)}/) >= 0) { | ||
const aws_regex = /#{AWS::([a-zA-Z]+)}/g; | ||
dictionary[key] = { | ||
"Fn::Sub": value.replace(aws_regex, '${AWS::$1}') | ||
} | ||
dictionary[key] = { | ||
"Fn::Sub": value.replace(aws_regex, '${AWS::$1}') | ||
}; | ||
// do some fancy logging | ||
do { | ||
var m = aws_regex.exec(value); | ||
if (m) { | ||
var msg = name + '::' + key + ' Replaced ' + chalk.yellow(m[1]) + ' with ' + chalk.yellow('${AWS::' + m[1] + '}'); | ||
// this.serverless.cli.consoleLog(message); | ||
consoleLog('AWS Pseudo Parameter: ' + msg) | ||
} | ||
} while (m); | ||
// do some fancy logging | ||
let m = aws_regex.exec(value); | ||
while (m) { | ||
consoleLog('AWS Pseudo Parameter: ' + name + '::' + key + ' Replaced ' + yellow(m[1]) + ' with ' + yellow('${AWS::' + m[1] + '}')); | ||
m = aws_regex.exec(value); | ||
} | ||
} | ||
// dicts and arrays need to be looped through | ||
if (isDict(value) || isArray(value)) { | ||
dictionary[key] = replaceChildNodes(value, name + '::' + key); | ||
} | ||
// dicts and arrays need to be looped through | ||
if (isDict(value) || isArray(value)) { | ||
dictionary[key] = replaceChildNodes(value, name + '::' + key); | ||
} | ||
}); | ||
return dictionary; | ||
} | ||
}); | ||
return dictionary; | ||
} | ||
function yellow(str) { | ||
return '\u001B[33m' + str + '\u001B[39m'; | ||
} | ||
function underline(str) { | ||
return '\u001B[4m' + str + '\u001B[24m'; | ||
} | ||
} | ||
} | ||
function get(obj, path, def) { | ||
return path.split('.').filter(Boolean).every(step => !(step && (obj = obj[step]) === undefined)) ? obj : def; | ||
} | ||
module.exports = ServerlessAWSPseudoParameters; |
{ | ||
"name": "serverless-pseudo-parameters", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"devDependencies": {}, | ||
@@ -10,6 +10,3 @@ "license": "MIT", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"lodash": "^4.13.1", | ||
"chalk": "^1.1.1" | ||
} | ||
"dependencies": {} | ||
} |
@@ -8,3 +8,3 @@ Serverless AWS Pseudo Parameters | ||
You can now use `#{AWS::AccountId}` etc. in any of your config strings, and this plugin replaces those values with the proper pseudo parameter `Fn::Sub` CloudFormation function. | ||
You can now use `#{AWS::AccountId}`, `#{AWS::Region}`, etc. in any of your config strings, and this plugin replaces those values with the proper pseudo parameter `Fn::Sub` CloudFormation function. | ||
@@ -15,3 +15,3 @@ Installation | ||
``` | ||
```yaml | ||
plugins: | ||
@@ -27,3 +27,3 @@ - serverless-pseudo-parameters | ||
``` | ||
```yaml | ||
service: users-bucket-thingy | ||
@@ -45,3 +45,3 @@ | ||
``` | ||
```json | ||
"Type": "AWS::S3::Bucket", | ||
@@ -57,3 +57,3 @@ "Properties": { | ||
``` | ||
```yaml | ||
service: foobar-handler | ||
@@ -80,1 +80,9 @@ | ||
``` | ||
The plugin also automatically replace _hardcoded_ region in `serverless.yml`. This feature can be disabled using: | ||
```yaml | ||
custom: | ||
pseudoParameters: | ||
skipRegionReplace: true | ||
``` |
5818
0
84
83
- Removedchalk@^1.1.1
- Removedlodash@^4.13.1
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)