Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·150 lines (132 loc) · 4.33 KB
/
Copy pathindex.js
File metadata and controls
executable file
·150 lines (132 loc) · 4.33 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
const spawnSync = require('child_process').spawnSync
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.options = options
this.commands = {
syncToS3: {
usage: 'Deploys the `app` directory to your bucket',
lifecycleEvents: [
'sync'
]
},
domainInfo: {
usage: 'Fetches and prints out the deployed CloudFront domain names',
lifecycleEvents: [
'domainInfo'
]
},
bucketInfo: {
usage: 'Fetches and prints out the deployed CloudFront bucket names',
lifecycleEvents: [
'bucketInfo'
]
},
invalidateCloudFrontCache: {
usage: 'Invalidates CloudFront cache',
lifecycleEvents: [
'invalidateCache'
]
}
}
this.hooks = {
'syncToS3:sync': this.syncDirectory.bind(this),
'domainInfo:domainInfo': this.domainInfo.bind(this),
'bucketInfo:bucketInfo': this.bucketInfo.bind(this),
'invalidateCloudFrontCache:invalidateCache': this.invalidateCache.bind(this)
}
}
runAwsCommand(args) {
const result = spawnSync('aws', args)
const stdout = result.stdout.toString()
const sterr = result.stderr.toString()
if (stdout) this.serverless.cli.log(stdout)
if (sterr) this.serverless.cli.log(sterr)
return { stdout, sterr }
}
getDescribeStacksOutput(outputKey) {
const provider = this.serverless.getProvider('aws')
const stackName = provider.naming.getStackName(this.options.stage)
return provider
.request(
'CloudFormation',
'describeStacks',
{ StackName: stackName },
this.options.stage,
this.options.region
)
.then((result) => {
const outputs = result.Stacks[0].Outputs
const output = outputs.find(entry => entry.OutputKey === outputKey)
return output.OutputValue
})
}
syncDirectory() {
this.getDescribeStacksOutput('WebAppS3BucketOutput').then(s3Bucket => {
const s3LocalPath = this.serverless.variables.service.custom.s3LocalPath
const s3DestPath = this.serverless.variables.service.custom.s3DestPath || ''
const args = [
's3',
'sync',
s3LocalPath,
`s3://${s3Bucket}/${s3DestPath.replace(/^\//, '')}`
]
this.serverless.cli.log(args)
const result = spawnSync('aws', args)
const stdout = result && result.stdout && result.stdout.toString()
const sterr = result && result.stderr && result.stderr.toString()
this.serverless.cli.log(stdout || 'stdoud undefined')
this.serverless.cli.log(sterr || 'stderr undefined')
if (!sterr) this.serverless.cli.log('Successfully synced to the S3 bucket')
})
}
bucketInfo() {
this.getDescribeStacksOutput('WebAppS3BucketOutput').then(outputValue =>
this.serverless.cli.log(`Web App Bucket: ${outputValue || 'Not Found'}`)
)
}
async domainInfo() {
return await this.getDescribeStacksOutput('WebAppCloudFrontDistributionOutput').then(outputValue => {
this.serverless.cli.log(`Web App Domain: ${outputValue || 'Not Found'}`)
return outputValue || undefined}
)
}
async invalidateCache() {
const provider = this.serverless.getProvider('aws')
const domain = await this.domainInfo()
const result = await provider.request(
'CloudFront',
'listDistributions',
{},
this.options.stage,
this.options.region
)
const distributions = result.DistributionList.Items
const distribution = distributions.find( entry => entry.DomainName === domain )
if (distribution) {
this.serverless.cli.log(
`Invalidating CloudFront distribution with id: ${distribution.Id}`
)
const args = [
'cloudfront',
'create-invalidation',
'--distribution-id',
distribution.Id,
'--paths',
'/*'
]
const { sterr } = this.runAwsCommand(args)
if (!sterr)
this.serverless.cli.log('Successfully invalidated CloudFront cache')
else
throw new Error('Failed invalidating CloudFront cache')
} else {
const message = `Could not find distribution with domain ${domain}`
const error = new Error(message)
this.serverless.cli.log(message)
throw error
}
}
}
module.exports = ServerlessPlugin