forked from jonathanconway/serverless-single-page-app-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
187 lines (171 loc) · 5.31 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'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 `s3LocalPath` directory to your bucket',
lifecycleEvents: [
'sync',
],
},
emptyBucket: {
usage: 'Empties the deployed bucket',
lifecycleEvents: [
'empty',
],
},
bucketInfo: {
usage: 'Fetches and prints out the deployed CloudFront bucket names',
lifecycleEvents: [
'bucketInfo',
],
},
domainInfo: {
usage: 'Fetches and prints out the deployed CloudFront domain names',
lifecycleEvents: [
'domainInfo',
],
},
invalidateCloudFrontCache: {
usage: 'Invalidates CloudFront cache',
lifecycleEvents: [
'invalidateCache',
],
},
};
this.hooks = {
'syncToS3:sync': this.syncDirectory.bind(this),
'before:remove:remove': this.emptyBucket.bind(this),
'emptyBucket:empty': this.emptyBucket.bind(this),
'domainInfo:domainInfo': this.domainInfo.bind(this),
'bucketInfo:bucketInfo': this.bucketInfo.bind(this),
'invalidateCloudFrontCache:invalidateCache': this.invalidateCache.bind(
this,
),
};
}
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 // eslint-disable-line comma-dangle
)
.then((result) => {
const outputs = result.Stacks[0].Outputs;
const output = outputs.find(entry => entry.OutputKey === outputKey);
return output.OutputValue;
});
}
// syncs the `s3LocalPath` directory to the provided bucket
syncDirectory() {
this.getDescribeStacksOutput('WebAppS3BucketOutput').then(s3Bucket => {
const s3LocalPath = this.serverless.service.custom.s3LocalPath;
const args = [
's3',
'sync',
s3LocalPath,
`s3://${s3Bucket}/`,
];
this.serverless.cli.log(args);
const { sterr } = this.runAwsCommand(args);
if (!sterr) {
this.serverless.cli.log('Successfully synced to the S3 bucket');
}
});
}
// Empties the deployed bucket
emptyBucket() {
this.getDescribeStacksOutput('WebAppS3BucketOutput').then(s3Bucket => {
const args = [
's3',
'rm',
`s3://${s3Bucket}/`,
'--recursive'
];
const { sterr } = this.runAwsCommand(args);
if (!sterr) {
this.serverless.cli.log('Successfully synced to the S3 bucket');
}
});
}
// fetches the bucket name from the CloudFront outputs and prints it out
bucketInfo() {
this.getDescribeStacksOutput('WebAppS3BucketOutput').then(outputValue =>
this.serverless.cli.log(`Web App Bucket: ${outputValue || 'Not Found'}`)
);
}
// fetches the domain name from the CloudFront outputs and prints it out
domainInfo() {
return this.getDescribeStacksOutput('WebAppCloudFrontDistributionOutput').then(outputValue => {
this.serverless.cli.log(`Web App Domain: ${outputValue || 'Not Found'}`);
return outputValue;
});
}
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;
}
}
runAwsCommand(args) {
let command = 'aws';
if (this.serverless.variables.service.provider.region) {
command = `${command} --region ${this.serverless.variables.service.provider.region}`;
}
if (this.serverless.variables.service.provider.profile) {
command = `${command} --profile ${this.serverless.variables.service.provider.profile}`;
}
const result = spawnSync(command, args, { shell: true });
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 };
}
}
module.exports = ServerlessPlugin;