Skip to content

Commit 7a4f42e

Browse files
committed
Merge branch 'master' of github.com:svdgraaf/serverless-pseudo-parameters
2 parents fc51529 + 09cdef0 commit 7a4f42e

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

lib/index.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
'use strict';
22

3-
const _ = require('lodash');
4-
const chalk = require('chalk');
5-
6-
73
class ServerlessAWSPseudoParameters {
84
constructor(serverless, options) {
95
this.serverless = serverless;
106
this.options = options;
117
this.hooks = {
128
'before:deploy:deploy': this.addParameters.bind(this),
139
};
14-
this.skipRegionReplace = _.get(serverless.service, 'custom.pseudoParameters.skipRegionReplace', false)
10+
this.skipRegionReplace = get(serverless.service, 'custom.pseudoParameters.skipRegionReplace', false)
1511
}
1612

1713
addParameters() {
1814

19-
const template = this.serverless.service.provider.compiledCloudFormationTemplate;
15+
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
2016
const skipRegionReplace = this.skipRegionReplace;
2117
const consoleLog = this.serverless.cli.consoleLog;
2218

23-
consoleLog(`${chalk.yellow.underline('AWS Pseudo Parameters')}`);
19+
consoleLog(yellow(underline('AWS Pseudo Parameters')));
2420

2521
if (skipRegionReplace) {
2622
consoleLog('Skipping automatic replacement of regions with account region!');
2723
}
2824

2925
// loop through all resources, and check all (string) properties for any #{AWS::}
3026
// reference. If found, replace the value with an Fn::Sub reference
31-
_.forEach(template.Resources, function(resource, identifier){
32-
replaceChildNodes(resource.Properties, identifier);
33-
});
27+
Object.keys(resources).forEach(identifier => replaceChildNodes(resources[identifier].Properties, identifier));
3428

3529
function isDict(v) {
36-
return typeof v==='object' && v!==null && !(v instanceof Array) && !(v instanceof Date);
30+
return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date);
3731
}
3832

3933
function isArray(v) {
@@ -60,45 +54,52 @@ class ServerlessAWSPseudoParameters {
6054
}
6155

6256
function replaceChildNodes(dictionary, name) {
63-
_.forEach(dictionary, function(value, key){
64-
65-
// if a region name is mentioned, replace it with a reference (unless we are skipping automatic replacements)
66-
if(typeof value === 'string' && !skipRegionReplace && containsRegion(value)) {
67-
var regionFinder = new RegExp(regions().join("|"));
68-
value = value.replace(regionFinder, '#{AWS::Region}');
57+
Object.keys(dictionary).forEach((key) => {
58+
59+
let value = dictionary[key];
60+
// if a region name is mentioned, replace it with a reference (unless we are skipping automatic replacements)
61+
if (typeof value === 'string' && !skipRegionReplace && containsRegion(value)) {
62+
const regionFinder = new RegExp(regions().join("|"));
63+
value = value.replace(regionFinder, '#{AWS::Region}');
64+
}
65+
66+
// we only want to possibly replace strings with an Fn::Sub
67+
if (typeof value === 'string' && value.search(/#{AWS::([a-zA-Z]+)}/) >= 0) {
68+
const aws_regex = /#{AWS::([a-zA-Z]+)}/g;
69+
70+
dictionary[key] = {
71+
"Fn::Sub": value.replace(aws_regex, '${AWS::$1}')
72+
};
73+
74+
// do some fancy logging
75+
let m = aws_regex.exec(value);
76+
while (m) {
77+
consoleLog('AWS Pseudo Parameter: ' + name + '::' + key + ' Replaced ' + yellow(m[1]) + ' with ' + yellow('${AWS::' + m[1] + '}'));
78+
m = aws_regex.exec(value);
6979
}
80+
}
7081

71-
// we only want to possibly replace strings with an Fn::Sub
72-
if(typeof value === 'string' && value.search(/#{AWS::([a-zA-Z]+)}/) >= 0) {
73-
var aws_regex = /#{AWS::([a-zA-Z]+)}/g;
74-
var m;
75-
76-
dictionary[key] = {
77-
"Fn::Sub": value.replace(aws_regex, '${AWS::$1}')
78-
}
79-
80-
// do some fancy logging
81-
do {
82-
var m = aws_regex.exec(value);
83-
if (m) {
84-
var msg = name + '::' + key + ' Replaced ' + chalk.yellow(m[1]) + ' with ' + chalk.yellow('${AWS::' + m[1] + '}');
85-
// this.serverless.cli.consoleLog(message);
86-
consoleLog('AWS Pseudo Parameter: ' + msg)
87-
}
88-
} while (m);
89-
}
82+
// dicts and arrays need to be looped through
83+
if (isDict(value) || isArray(value)) {
84+
dictionary[key] = replaceChildNodes(value, name + '::' + key);
85+
}
9086

91-
// dicts and arrays need to be looped through
92-
if (isDict(value) || isArray(value)) {
93-
dictionary[key] = replaceChildNodes(value, name + '::' + key);
94-
}
87+
});
88+
return dictionary;
89+
}
9590

96-
});
97-
return dictionary;
98-
}
91+
function yellow(str) {
92+
return '\u001B[33m' + str + '\u001B[39m';
93+
}
9994

95+
function underline(str) {
96+
return '\u001B[4m' + str + '\u001B[24m';
97+
}
10098
}
99+
}
101100

101+
function get(obj, path, def) {
102+
return path.split('.').filter(Boolean).every(step => !(step && (obj = obj[step]) === undefined)) ? obj : def;
102103
}
103104

104105
module.exports = ServerlessAWSPseudoParameters;

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77
"git": "https://github.com/svdgraaf/serverless-pseudo-parameters"
88
},
99
"main": "lib/index.js",
10-
"dependencies": {
11-
"lodash": "^4.13.1",
12-
"chalk": "^1.1.1"
13-
}
10+
"dependencies": {}
1411
}

0 commit comments

Comments
 (0)