-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
56 lines (45 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const _ = require('lodash');
module.exports = exports = class EncodeEnvironmentVariableObjects {
constructor(serverless, options) {
this._serverless = serverless;
this.commands = {
deploy: {
lifecycleEvents: [
'resources'
]
}
};
this.hooks = {
'before:deploy:resources': this.beforeDeployResources.bind(this)
};
}
beforeDeployResources() {
let envProperties = [];
if (this._serverless.service.custom && this._serverless.service.custom.encodeEnvObjects) {
envProperties = _.isString(this._serverless.service.custom.encodeEnvObjects) ? [this._serverless.service.custom.encodeEnvObjects] : this._serverless.service.custom.encodeEnvObjects;
} else {
envProperties = _.keys(this._serverless.service.provider.environment);
}
// encode global provider env objects
this._serverless.service.provider.environment = this.encodeEnvironmentObjects(this._serverless.service.provider.environment, envProperties);
// encode function specific env objects
_.forEach(this._serverless.service.functions, (v, k) => {
let envs = envProperties;
if (!this._serverless.service.custom || !this._serverless.service.custom.encodeEnvObjects) {
envs = envs.concat(_.keys(v.environment));
}
v.environment = this.encodeEnvironmentObjects(v.environment, envs);
});
}
encodeEnvironmentObjects(envVars, envProperties) {
if (_.isObject(envVars)) {
_.forEach(envVars, (v, k) => {
if (_.isObject(v) && _.indexOf(envProperties, k) >= 0) {
envVars[k] = new Buffer(JSON.stringify(v)).toString('base64');
}
});
}
return envVars;
}
};