/
Service.js
283 lines (243 loc) · 8.94 KB
/
Service.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'use strict';
const { ServerlessError, logWarning } = require('./Error');
const path = require('path');
const _ = require('lodash');
const BbPromise = require('bluebird');
const semver = require('semver');
const serverlessConfigFileUtils = require('../utils/getServerlessConfigFile');
const currentVersion = require('../../package').version;
class Service {
constructor(serverless, data) {
// #######################################################################
// ## KEEP SYNCHRONIZED WITH EQUIVALENT IN ~/lib/plugins/print/print.js ##
// #######################################################################
this.serverless = serverless;
// Default properties
this.service = null;
this.serviceObject = null;
this.provider = {
stage: 'dev',
variableSyntax: '\\${([^{}:]+?(?:\\(|:)[^:{}][^{}]*?)}',
};
this.custom = {};
this.plugins = [];
this.pluginsData = {};
this.functions = {};
this.resources = {};
this.package = {};
this.configValidationMode = 'warn';
this.disabledDeprecations = [];
if (data) this.update(data);
}
load(rawOptions) {
const that = this;
const options = rawOptions || {};
if (!options.stage && options.s) options.stage = options.s;
if (!options.region && options.r) options.region = options.r;
const servicePath = this.serverless.config.servicePath;
// skip if the service path is not found
// because the user might be creating a new service
if (!servicePath) {
return BbPromise.resolve();
}
return BbPromise.all([
serverlessConfigFileUtils.getServerlessConfigFilePath(this.serverless),
serverlessConfigFileUtils.getServerlessConfigFile(this.serverless),
])
.then(args => that.loadServiceFileParam(...args))
.catch(error => {
if (this.serverless.cli.isHelpRequest(this.serverless.processedInput)) {
return null;
}
throw error;
});
}
loadServiceFileParam(serviceFilename, serverlessFileParam) {
const that = this;
that.serviceFilename = path.basename(serviceFilename);
const serverlessFile = serverlessFileParam;
// basic service level validation
const version = this.serverless.utils.getVersion();
let ymlVersion = serverlessFile.frameworkVersion;
if (ymlVersion && !semver.validRange(ymlVersion)) {
if (serverlessFile.configValidationMode === 'error') {
throw new ServerlessError(
'Configured "frameworkVersion" does not represent a valid semver version range.',
'INVALID_FRAMEWORK_VERSION'
);
}
logWarning(
'Configured "frameworkVersion" does not represent a valid semver version range, version validation is skipped'
);
ymlVersion = null;
}
if (!this.isLocallyInstalled && !ymlVersion && process.env.SLS_DEBUG) {
this.serverless.cli.log(
'To ensure safe major version upgrades ensure "frameworkVersion" setting in ' +
'service configuration ' +
`(recommended setup: "frameworkVersion: ^${currentVersion}")\n`
);
}
if (
ymlVersion &&
version !== ymlVersion &&
!semver.satisfies(semver.coerce(version).raw, ymlVersion)
) {
const errorMessage = [
`The Serverless version (${version}) does not satisfy the`,
` "frameworkVersion" (${ymlVersion}) in ${this.serviceFilename}`,
].join('');
throw new ServerlessError(errorMessage, 'FRAMEWORK_VERSION_MISMATCH');
}
if (!serverlessFile.service) {
throw new ServerlessError(
`"service" property is missing in ${this.serviceFilename}`,
'SERVICE_NAME_MISSING'
);
}
if (_.isObject(serverlessFile.service) && !serverlessFile.service.name) {
throw new ServerlessError(
`"service" is missing the "name" property in ${this.serviceFilename}`,
'SERVICE_NAME_MISSING'
); // eslint-disable-line max-len
}
if (!serverlessFile.provider) {
throw new ServerlessError(
`"provider" property is missing in ${this.serviceFilename}`,
'PROVIDER_NAME_MISSING'
);
}
// #######################################################################
// ## KEEP SYNCHRONIZED WITH EQUIVALENT IN ~/lib/plugins/print/print.js ##
// #######################################################################
// #####################################################################
// ## KEEP SYNCHRONIZED WITH EQUIVALENT IN ~/lib/classes/Variables.js ##
// ## there, see `getValueFromSelf` ##
// ## here, see below ##
// #####################################################################
if (!_.isObject(serverlessFile.provider)) {
const providerName = serverlessFile.provider;
serverlessFile.provider = {
name: providerName,
};
}
if (_.isObject(serverlessFile.service)) {
that.serviceObject = serverlessFile.service;
that.service = serverlessFile.service.name;
} else {
that.serviceObject = { name: serverlessFile.service };
that.service = serverlessFile.service;
}
that.app = serverlessFile.app;
that.tenant = serverlessFile.tenant;
that.org = serverlessFile.org;
that.custom = serverlessFile.custom;
that.plugins = serverlessFile.plugins;
that.resources = serverlessFile.resources;
that.functions = serverlessFile.functions || {};
that.configValidationMode = serverlessFile.configValidationMode || 'warn';
that.disabledDeprecations = serverlessFile.disabledDeprecations;
// merge so that the default settings are still in place and
// won't be overwritten
that.provider = _.merge(that.provider, serverlessFile.provider);
if (serverlessFile.package) {
that.package = serverlessFile.package;
}
if (that.provider.name === 'aws') {
that.layers = serverlessFile.layers || {};
}
that.outputs = serverlessFile.outputs;
this.initialServerlessConfig = serverlessFile;
return this;
}
setFunctionNames(rawOptions) {
const that = this;
const options = rawOptions || {};
options.stage = options.stage || options.s;
options.region = options.region || options.r;
// setup function.name property
const stageNameForFunction = options.stage || this.provider.stage;
Object.entries(that.functions).forEach(([functionName, functionObj]) => {
if (!functionObj.events) {
that.functions[functionName].events = [];
}
if (!functionObj.name) {
that.functions[
functionName
].name = `${that.service}-${stageNameForFunction}-${functionName}`;
}
});
}
mergeArrays() {
['resources', 'functions'].forEach(key => {
if (Array.isArray(this[key])) {
this[key] = this[key].reduce((memo, value) => {
if (value) {
if (typeof value === 'object') {
return _.merge(memo, value);
}
throw new Error(`Non-object value specified in ${key} array: ${value}`);
}
return memo;
}, {});
}
});
}
validate() {
const userConfig = Object.assign({}, this.initialServerlessConfig || {});
userConfig.service = this.serviceObject;
userConfig.provider = this.provider;
// Ensure to validate normalized (after mergeArrays) input
if (userConfig.functions) userConfig.functions = this.functions;
if (userConfig.resources) userConfig.resources = this.resources;
if (this.serviceObject && this.configValidationMode !== 'off') {
this.serverless.configSchemaHandler.validateConfig(userConfig);
}
return this;
}
update(data) {
return _.merge(this, data);
}
getServiceName() {
return this.serviceObject.name;
}
getServiceObject() {
return this.serviceObject;
}
getAllFunctions() {
return Object.keys(this.functions);
}
getAllLayers() {
return this.layers ? Object.keys(this.layers) : [];
}
getAllFunctionsNames() {
return this.getAllFunctions().map(func => this.getFunction(func).name);
}
getFunction(functionName) {
if (functionName in this.functions) {
return this.functions[functionName];
}
throw new ServerlessError(`Function "${functionName}" doesn't exist in this Service`);
}
getLayer(layerName) {
if (layerName in this.layers) {
return this.layers[layerName];
}
throw new ServerlessError(`Layer "${layerName}" doesn't exist in this Service`);
}
getEventInFunction(eventName, functionName) {
const event = this.getFunction(functionName).events.find(e => Object.keys(e)[0] === eventName);
if (event) {
return event;
}
throw new ServerlessError(`Event "${eventName}" doesn't exist in function "${functionName}"`);
}
getAllEventsInFunction(functionName) {
return this.getFunction(functionName).events;
}
publish(dataParam) {
const data = dataParam || {};
this.pluginsData = _.merge(this.pluginsData, data);
}
}
module.exports = Service;