forked from trek10inc/serverless-cloudformation-changesets
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
58 lines (48 loc) · 1.88 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
57
58
'use strict'
const createChangeSet = require('./lib/createChangeSet')
const setBucketName = require('serverless/lib/plugins/aws/lib/set-bucket-name')
const changeSetPair = (params) => {
if (!params) {
return
}
// TODO: refactor to use optional chaining when node12 support is dropped
const paramPair = params.filter((param) => param.startsWith('changeset') )[0]
if (!paramPair) {
return
}
const [requireChangeSet, changeSetName] = paramPair.split('=')
return {
requireChangeSet: !!requireChangeSet,
changeSetName
}
}
class ServerlessCloudFormationChangeSets {
constructor (serverless, options) {
this.serverless = serverless
// TODO: refactor to use optional chaining when node12 support is dropped
this.options = Object.assign({}, serverless.service && serverless.service.custom && serverless.service.custom['cf-changesets'], options)
this.provider = this.serverless.getProvider('aws')
if (options.param) {
const { requireChangeSet, changeSetName } = changeSetPair(options.param)
this.options.requireChangeSet = requireChangeSet
this.options.changeSetName = changeSetName
}
if (this.options.requireChangeSet) {
this.hooks = {
'before:aws:deploy:deploy:updateStack': this.lockStackDeployment.bind(this),
'aws:deploy:deploy:updateStack': () => Promise.resolve()
.then(setBucketName.setBucketName.bind(this))
.then(createChangeSet.createChangeSet.bind(this)),
'after:aws:deploy:deploy:updateStack': this.unlockStackDeployment.bind(this)
}
}
}
lockStackDeployment () {
this.shouldNotDeploy = this.serverless.service.provider.shouldNotDeploy
this.serverless.service.provider.shouldNotDeploy = true
}
unlockStackDeployment () {
this.serverless.service.provider.shouldNotDeploy = this.shouldNotDeploy
}
}
module.exports = ServerlessCloudFormationChangeSets