1
- ' use strict' ;
1
+ " use strict" ;
2
2
3
- const spawnSync = require ( ' child_process' ) . spawnSync ;
3
+ const spawnSync = require ( " child_process" ) . spawnSync ;
4
4
5
5
class ServerlessPlugin {
6
6
constructor ( serverless , options ) {
7
7
this . serverless = serverless ;
8
8
this . options = options ;
9
9
this . commands = {
10
10
syncToS3 : {
11
- usage : 'Deploys the `app` directory to your bucket' ,
12
- lifecycleEvents : [
13
- 'sync' ,
14
- ] ,
11
+ usage : "Deploys the `app` directory to your bucket" ,
12
+ lifecycleEvents : [ "sync" ] ,
15
13
} ,
16
14
domainInfo : {
17
- usage : 'Fetches and prints out the deployed CloudFront domain names' ,
18
- lifecycleEvents : [
19
- 'domainInfo' ,
20
- ] ,
15
+ usage : "Fetches and prints out the deployed CloudFront domain names" ,
16
+ lifecycleEvents : [ "domainInfo" ] ,
21
17
} ,
22
18
invalidateCloudFrontCache : {
23
- usage : 'Invalidates CloudFront cache' ,
24
- lifecycleEvents : [
25
- 'invalidateCache' ,
26
- ] ,
19
+ usage : "Invalidates CloudFront cache" ,
20
+ lifecycleEvents : [ "invalidateCache" ] ,
27
21
} ,
28
22
} ;
29
23
30
24
this . hooks = {
31
- 'syncToS3:sync' : this . syncDirectory . bind ( this ) ,
32
- 'domainInfo:domainInfo' : this . domainInfo . bind ( this ) ,
33
- 'invalidateCloudFrontCache:invalidateCache' : this . invalidateCache . bind (
34
- this ,
35
- ) ,
25
+ "syncToS3:sync" : this . syncDirectory . bind ( this ) ,
26
+ "domainInfo:domainInfo" : this . domainInfo . bind ( this ) ,
27
+ "invalidateCloudFrontCache:invalidateCache" :
28
+ this . invalidateCache . bind ( this ) ,
36
29
} ;
37
30
}
38
31
39
32
runAwsCommand ( args ) {
40
- let command = ' aws' ;
33
+ let command = " aws" ;
41
34
if ( this . serverless . variables . service . provider . region ) {
42
35
command = `${ command } --region ${ this . serverless . variables . service . provider . region } ` ;
43
36
}
@@ -60,84 +53,85 @@ class ServerlessPlugin {
60
53
// syncs the `app` directory to the provided bucket
61
54
syncDirectory ( ) {
62
55
const s3Bucket = this . serverless . variables . service . custom . s3Bucket ;
63
- const buildFolder = this . serverless . variables . service . custom . client . distributionFolder ;
56
+ const buildFolder =
57
+ this . serverless . variables . service . custom . client . distributionFolder ;
64
58
const args = [
65
- 's3' ,
66
- ' sync' ,
59
+ "s3" ,
60
+ " sync" ,
67
61
`${ buildFolder } /` ,
68
62
`s3://${ s3Bucket } /` ,
69
- ' --delete' ,
63
+ " --delete" ,
70
64
] ;
71
65
const { sterr } = this . runAwsCommand ( args ) ;
72
66
if ( ! sterr ) {
73
- this . serverless . cli . log ( ' Successfully synced to the S3 bucket' ) ;
67
+ this . serverless . cli . log ( " Successfully synced to the S3 bucket" ) ;
74
68
} else {
75
- throw new Error ( ' Failed syncing to the S3 bucket' ) ;
69
+ throw new Error ( " Failed syncing to the S3 bucket" ) ;
76
70
}
77
71
}
78
72
79
73
// fetches the domain name from the CloudFront outputs and prints it out
80
74
async domainInfo ( ) {
81
- const provider = this . serverless . getProvider ( ' aws' ) ;
75
+ const provider = this . serverless . getProvider ( " aws" ) ;
82
76
const stackName = provider . naming . getStackName ( this . options . stage ) ;
83
77
const result = await provider . request (
84
- ' CloudFormation' ,
85
- ' describeStacks' ,
78
+ " CloudFormation" ,
79
+ " describeStacks" ,
86
80
{ StackName : stackName } ,
87
81
this . options . stage ,
88
- this . options . region ,
82
+ this . options . region
89
83
) ;
90
84
91
85
const outputs = result . Stacks [ 0 ] . Outputs ;
92
86
const output = outputs . find (
93
- entry => entry . OutputKey === ' WebAppCloudFrontDistributionOutput' ,
87
+ ( entry ) => entry . OutputKey === " WebAppCloudFrontDistributionOutput"
94
88
) ;
95
89
96
90
if ( output && output . OutputValue ) {
97
91
this . serverless . cli . log ( `Web App Domain: ${ output . OutputValue } ` ) ;
98
92
return output . OutputValue ;
99
93
}
100
94
101
- this . serverless . cli . log ( ' Web App Domain: Not Found' ) ;
102
- const error = new Error ( ' Could not extract Web App Domain' ) ;
95
+ this . serverless . cli . log ( " Web App Domain: Not Found" ) ;
96
+ const error = new Error ( " Could not extract Web App Domain" ) ;
103
97
throw error ;
104
98
}
105
99
106
100
async invalidateCache ( ) {
107
- const provider = this . serverless . getProvider ( ' aws' ) ;
101
+ const provider = this . serverless . getProvider ( " aws" ) ;
108
102
109
103
const domain = await this . domainInfo ( ) ;
110
104
111
105
const result = await provider . request (
112
- ' CloudFront' ,
113
- ' listDistributions' ,
106
+ " CloudFront" ,
107
+ " listDistributions" ,
114
108
{ } ,
115
109
this . options . stage ,
116
- this . options . region ,
110
+ this . options . region
117
111
) ;
118
112
119
113
const distributions = result . DistributionList . Items ;
120
114
const distribution = distributions . find (
121
- entry => entry . DomainName === domain ,
115
+ ( entry ) => entry . DomainName === domain
122
116
) ;
123
117
124
118
if ( distribution ) {
125
119
this . serverless . cli . log (
126
- `Invalidating CloudFront distribution with id: ${ distribution . Id } ` ,
120
+ `Invalidating CloudFront distribution with id: ${ distribution . Id } `
127
121
) ;
128
122
const args = [
129
- ' cloudfront' ,
130
- ' create-invalidation' ,
131
- ' --distribution-id' ,
123
+ " cloudfront" ,
124
+ " create-invalidation" ,
125
+ " --distribution-id" ,
132
126
distribution . Id ,
133
- ' --paths' ,
127
+ " --paths" ,
134
128
'"/*"' ,
135
129
] ;
136
130
const { sterr } = this . runAwsCommand ( args ) ;
137
131
if ( ! sterr ) {
138
- this . serverless . cli . log ( ' Successfully invalidated CloudFront cache' ) ;
132
+ this . serverless . cli . log ( " Successfully invalidated CloudFront cache" ) ;
139
133
} else {
140
- throw new Error ( ' Failed invalidating CloudFront cache' ) ;
134
+ throw new Error ( " Failed invalidating CloudFront cache" ) ;
141
135
}
142
136
} else {
143
137
const message = `Could not find distribution with domain ${ domain } ` ;
@@ -148,4 +142,4 @@ class ServerlessPlugin {
148
142
}
149
143
}
150
144
151
- module . exports = ServerlessPlugin ;
145
+ module . exports = ServerlessPlugin ;
0 commit comments