forked from sdd/serverless-apig-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (109 loc) · 5.53 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
'use strict';
const { merge } = require('lodash');
const { get } = require('lodash');
const { map } = require('lodash');
const { cloneDeep } = require('lodash');
const pify = require('pify');
const fs = pify(require('fs'));
const path = require('path');
const ServerlessAWSPlugin = require('./lib/ServerlessAWSPlugin');
const checkBucketExists = require('./lib/checkBucketExists');
const getBucketName = require('./lib/getBucketName');
const getDistFolder = require('./lib/getDistFolder');
const purgeBucket = require('./lib/purgeBucket');
const uploadFileToBucket = require('./lib/uploadFileToBucket');
const uploadFolderToBucket = require('./lib/uploadFolderToBucket');
module.exports = class ServerlessApigS3 extends ServerlessAWSPlugin {
constructor(serverless, options) {
super(serverless, options);
this.stackName = serverless.service.service;
this.commands = {
client: {
usage: 'Deploy client code',
lifecycleEvents:[ 'client', 'deploy' ],
commands: {
deploy: {
usage: 'Deploy serverless client code',
lifecycleEvents:[ 'deploy' ]
}
}
}
};
this.hooks = {
'before:package:createDeploymentArtifacts': () => this.mergeApigS3Resources(),
'client:client': () => { this.serverless.cli.log(this.commands.client.usage); },
'client:deploy:deploy': () => this.deploy()
};
Object.assign(this,
checkBucketExists,
getBucketName,
getDistFolder,
purgeBucket,
uploadFileToBucket,
uploadFolderToBucket
);
}
updateIamRoleAndPolicyNames(roleResource) {
roleResource[ "Properties" ][ "RoleName" ] = this.stackName + "_" + roleResource[ "Properties" ][ "RoleName" ] + "_" + this.options.stage;
roleResource[ "Properties" ][ "Policies" ][ 0 ][ "PolicyName" ] = this.stackName + "_" + roleResource[ "Properties" ][ "Policies" ][ 0 ][ "PolicyName" ] + "_" + thiis.options.stage;
return roleResource
}
async mergeApigS3Resources() {
const oldCwd = process.cwd();
const ownResources = await this.serverless.yamlParser.parse(
path.resolve(__dirname, 'resources.yml')
);
process.chdir(oldCwd);
const withIndex = get(this.serverless, 'service.custom.apigs3.withIndex', true);
if(!withIndex) {
delete ownResources['Resources']['ApiGatewayMethodIndexGet'];
delete ownResources['Resources']['ApiGatewayMethodDefaultRouteGet'];
delete ownResources['Resources']['ApiGatewayResourceDefaultRoute'];
}
const topFiles = get(this.serverless, 'service.custom.apigs3.topFiles', false);
if(topFiles) {
this.getDistFolder();
const dirContents = map(await fs.readdir(this.clientPath),
name => path.join(this.clientPath, name)
);
const dotFiles = get(this.serverless, 'service.custom.apigs3.dotFiles', false);
await Promise.all(dirContents.map(async item => {
const stat = await fs.stat(item);
if (!stat.isFile()) return;
const pathPart = path.basename(item);
if (pathPart === 'index.html' || (pathPart[0] === '.' && !dotFiles)) return;
const routeName = this.aws.naming.getResourceLogicalId(pathPart);
const routeId = this.aws.naming.extractResourceId(routeName);
const route = cloneDeep(ownResources['Resources']['ApiGatewayResourceAssets']);
route['Properties']['PathPart'] = pathPart;
ownResources['Resources'][routeName] = route;
const methodName = this.aws.naming.getMethodLogicalId(routeId, 'Get');
const method = cloneDeep(ownResources['Resources']['ApiGatewayMethodDefaultRouteGet']);
method['Properties']['Integration']['Uri']['Fn::Join'][1].splice(4, 1, '/' + pathPart);
method['Properties']['ResourceId']['Ref'] = routeName;
ownResources['Resources'][methodName] = method;
}));
}
const resourceName = get(this.serverless, 'service.custom.apigs3.resourceName', 'assets');
ownResources['Resources']['ApiGatewayResourceAssets']['Properties']['PathPart'] = resourceName;
const resourcePath = get(this.serverless, 'service.custom.apigs3.resourcePath', '');
if (resourcePath) {
const method = ownResources['Resources']['ApiGatewayMethodAssetsItemGet'];
method['Properties']['Integration']['Uri']['Fn::Join'][1].splice(4, 0, resourcePath);
}
const existing = this.serverless.service.provider.compiledCloudFormationTemplate;
ownResources[ "Resources" ][ "IamRoleApiGatewayS3" ] = this.updateIamRoleAndPolicyNames(ownResources[ "Resources" ][ "IamRoleApiGatewayS3" ], this.serverless.service.provider.stage);
merge(existing, ownResources);
}
async deploy() {
this.getDistFolder();
await this.getBucketName();
await this.checkBucketExists();
this.log('Deploying client to stage "' + this.stage + '" in region "' + this.region + '"...');
this.log('emptying current bucket contents...');
await this.purgeBucket(this.bucketName);
this.log('uploading content...');
await this.uploadFolderToBucket(this.clientPath, this.bucketName);
this.log('Deployment complete.');
}
};