-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (54 loc) · 1.46 KB
/
index.js
File metadata and controls
66 lines (54 loc) · 1.46 KB
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
59
60
61
62
63
64
65
66
'use strict'
class CreateDeploymentBucketPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.options = options
this.provider = this.serverless.getProvider('aws')
this.hooks = {
'aws:common:validate:validate': this.onAWSValidate.bind(this)
}
}
onAWSValidate() {
// ignore if not deploy
if (!this.serverless.processedInput.commands.includes('deploy')) {
return
}
const bucketName = this.getBucketName()
if (!bucketName) {
this.serverless.cli.log(
'No custom deployment bucket is set, nothing to create'
)
return
}
return this.bucketExists(bucketName)
.then(exists => {
if (exists) {
this.serverless.cli.log(
`"${bucketName}" bucket already exists, nothing to create`
)
return Promise.resolve()
}
return this.createBucket(bucketName)
.then(() =>
this.serverless.cli.log(`"${bucketName}" bucket was created`)
)
})
}
getBucketName() {
return this.serverless
.service
.provider
.deploymentBucket
}
bucketExists(bucketName) {
return this.provider
.request('S3', 'headBucket', { Bucket: bucketName })
.then(() => true)
.catch(() => false)
}
createBucket(bucketName) {
return this.provider
.request('S3', 'createBucket', { Bucket: bucketName })
}
}
module.exports = CreateDeploymentBucketPlugin