-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (120 loc) · 3.58 KB
/
index.js
File metadata and controls
140 lines (120 loc) · 3.58 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
const _ = require('lodash');
const BaseServerlessPlugin = require('base-serverless-plugin');
const cloudFrontInvalidatePlugin = require('serverless-cloudfront-invalidate');
const s3SyncPlugin = require('serverless-s3-sync');
const certificate = require('./src/certificate');
const utils = require('./src/utils');
const externalPlugins = require('./src/externalPlugins');
const configureApiGateway = require('./src/configureApiGateway');
const loadConfig = require('./src/loadConfig');
const awsUtils = require('./src/aws.utils');
const route53 = require('./src/route53');
const validate = require('./src/validate');
const LOG_PREFFIX = '[ServerlessCdnStack] -';
const USR_CONF = 'cdnStack';
class ServerlessPlugin extends BaseServerlessPlugin {
/**
* Default Constructor
*
* @param {object} serverless the serverless instance
* @param {object} options command line arguments
*/
constructor(serverless, options) {
super(serverless, options, LOG_PREFFIX, USR_CONF);
this.pluginPath = __dirname;
Object.assign(
this,
certificate,
externalPlugins,
loadConfig,
utils,
awsUtils,
configureApiGateway,
route53,
validate
);
this.onceInit = _.once(() => this.initialize());
this.hooks = {
'before:package:initialize': this.dispatchAction.bind(
this,
this.beforeInitialize
),
'package:initialize': this.dispatchAction.bind(this, this.injectTemplate),
};
}
/**
* Add plugins after me
*
*/
asyncInit() {
this.addPlugins([cloudFrontInvalidatePlugin, s3SyncPlugin]);
}
/**
* Action Wrapper check plugin condition before perform action
*
* @param {function} funAction serverless plugin action
*
*/
async dispatchAction(funAction, varResolver = undefined) {
if (this.isPluginDisabled()) {
this.log('warning: plugin is disabled');
return '';
}
await this.onceInit();
return funAction.call(this, varResolver);
}
/**
* Before Deploy Hooks
*
*/
async beforeInitialize() {
await this.validateResources();
if (this.isCertificateWithDomainName(this.cfg.certificate)) {
this.cfg.certificate = await this.getCertificateArn(this.cfg.certificate);
}
}
/**
* Initialize and call another hook plugin
*
* @returns {promise} another hook plugin promise if need
*/
async initialize() {
this.loadConfig();
this.configureExternalPlugins();
if (!_.isEmpty(this.cfg.beforeSpawn)) {
return this.serverless.pluginManager.spawn(this.cfg.beforeSpawn);
}
return Promise.resolve();
}
/**
* Add yml templates rendered on demand
* to CloudFormation template
*
*/
async injectTemplate() {
if (!_.isEmpty(this.cfg.logging.bucketName)) {
this.mergeCFormationResources(
this.render('./resources/s3-logging.hbs', this.cfg)
);
}
if (!_.isEmpty(this.cfg.apigatewayStr)) {
await this.configureApiGateway();
this.mergeCFormationResources(
this.render('./resources/apigateway.hbs', this.cfg)
);
}
this.mergeCFormationResources([
this.render('./resources/s3-bucket.hbs', this.cfg),
this.render('./resources/s3-policy.hbs', this.cfg),
this.render('./resources/cloudfront-identify.hbs', this.cfg),
this.render('./resources/cloudfront.hbs', this.cfg),
]);
if (this.cfg.createInRoute53) {
this.cfg.zoneId = await this.getHostedZoneId(this.cfg.cname);
this.mergeCFormationResources(
this.render('./resources/route53.hbs', this.cfg)
);
}
}
}
module.exports = ServerlessPlugin;