-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (41 loc) · 1.84 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
module.exports = class SthreeEnvPlugin {
constructor(serverless, options){
this._serverless = serverless;
this._options = options;
this.hooks = {
'before:deploy:createDeploymentArtifacts':this.beforeDeployResource.bind(this)
}
}
getSThreeBucket(bucketName, keyName, stage, region) {
return this._serverless.getProvider(this._serverless.service.provider.name).request('S3', 'getObject', {
Bucket: bucketName,
Key: keyName,
}, stage, region);
}
appendEnvironment(currData, data) {
let obj = {};
obj = JSON.parse(data);
//do other stuff later on
return Object.assign(obj, currData);
}
beforeDeployResource() {
this._serverless.cli.log('SthreeEnvPlugin : ' + 'is running');
let stage = (this._options.stage ? this._options.stage : this._serverless.service.provider.stage);
let region = this._serverless.service.provider.region;
let resourceName = this._serverless.service.service + '-config-' + stage;
return this.getSThreeBucket(resourceName, 'config.json', stage, region).then(function(data, err){
this._serverless.cli.log('SthreeEnvPlugin : getting config from bucket '+ resourceName);
if (err) {
this._serverless.cli.log('SthreeEnvPlugin : ' + err);
}
let body = data.Body;
try {
this._serverless.service.provider.environment = this.appendEnvironment(this._serverless.service.provider.environment, body);
this._serverless.cli.log('SthreeEnvPlugin : env var contains '+ Object.keys(this._serverless.service.provider.environment));
} catch(e) {
this._serverless.cli.log('SthreeEnvPlugin : ' + e);
}
return true;
}.bind(this));
}
};