-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
extendedValidate.js
95 lines (83 loc) · 3.81 KB
/
extendedValidate.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
'use strict';
const path = require('path');
const BbPromise = require('bluebird');
const _ = require('lodash');
const findReferences = require('../../utils/findReferences');
module.exports = {
extendedValidate() {
// Restore state
const serviceStateFileName = this.provider.naming.getServiceStateFileName();
const serviceStateFilePath = path.join(
this.serverless.config.servicePath,
'.serverless',
serviceStateFileName
);
if (!this.serverless.utils.fileExistsSync(serviceStateFilePath)) {
throw new this.serverless.classes.Error(
`No ${serviceStateFileName} file found in the package path you provided.`
);
}
const state = this.serverless.utils.readFileSync(serviceStateFilePath);
const selfReferences = findReferences(state.service, '${self:}');
_.forEach(selfReferences, ref => _.set(state.service, ref, this.serverless.service));
_.assign(this.serverless.service, state.service);
this.serverless.service.package.artifactDirectoryName = state.package.artifactDirectoryName;
// only restore the default artifact path if the user is not using a custom path
if (!_.isEmpty(state.package.artifact) && this.serverless.service.artifact) {
this.serverless.service.package.artifact = path.join(
this.serverless.config.servicePath,
'.serverless',
state.package.artifact
);
}
// Check function's attached to API Gateway timeout
if (!_.isEmpty(this.serverless.service.functions)) {
this.serverless.service.getAllFunctions().forEach(functionName => {
const functionObject = this.serverless.service.getFunction(functionName);
// Check if function timeout is greater than API Gateway timeout
if (functionObject.timeout > 30 && functionObject.events) {
functionObject.events.forEach(event => {
if (Object.keys(event)[0] === 'http') {
const warnMessage = [
`WARNING: Function ${functionName} has timeout of ${functionObject.timeout} `,
"seconds, however, it's attached to API Gateway so it's automatically ",
'limited to 30 seconds.',
].join('');
this.serverless.cli.log(warnMessage);
}
});
}
});
}
if (!_.isEmpty(this.serverless.service.functions)) {
this.serverless.service.getAllFunctions().forEach(functionName => {
// By default assume service-level package
let artifactFileName = this.provider.naming.getServiceArtifactName();
let artifactFilePath = path.join(this.packagePath, artifactFileName);
const functionObject = this.serverless.service.getFunction(functionName);
const individually =
(_.has(functionObject, ['package', 'individually']) &&
functionObject.package.individually) ||
this.serverless.service.package.individually;
if (_.has(functionObject, ['package', 'artifact'])) {
// Use function-level artifact
artifactFilePath = functionObject.package.artifact;
artifactFileName = path.basename(artifactFilePath);
} else if (this.serverless.service.package.artifact) {
// Use service-levle artifact
artifactFileName = artifactFilePath = this.serverless.service.package.artifact;
} else if (individually) {
// Use function-level generated artifact
artifactFileName = this.provider.naming.getFunctionArtifactName(functionName);
artifactFilePath = path.join(this.packagePath, artifactFileName);
}
if (!this.serverless.utils.fileExistsSync(artifactFilePath)) {
throw new this.serverless.classes.Error(
`No ${artifactFileName} file found in the package path you provided.`
);
}
});
}
return BbPromise.resolve();
},
};