-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·86 lines (69 loc) · 2.65 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
'use strict';
class DependsOn {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
this.hooks = {
'before:package:finalize': this.addDependsOn.bind(this)
};
}
isEnabled() {
const cliOption = this.options['dependson-plugin'];
if (cliOption === 'disabled' || cliOption === 'off' || cliOption === 'false') {
return false;
}
const custom = this.serverless.service.custom;
if (custom && custom.dependsOn && (custom.dependsOn.enabled === false || custom.dependsOn.enabled === 'false')) {
return false;
}
return true;
}
getChains() {
const custom = this.serverless.service.custom;
if (custom && custom.dependsOn && custom.dependsOn.chains) {
const chains = custom.dependsOn.chains;
if ((typeof chains === 'number') && (chains > 1)) {
return Math.trunc(chains);
}
}
return 1;
}
addDependsOn() {
if (!this.isEnabled()) {
this.serverless.cli.log('dependson-plugin disabled');
return;
}
const fnList = [];
// Find function definitions and their CloudFormation logical ids
const functions = this.serverless.service.functions;
for (let name in functions) {
if (functions.hasOwnProperty(name)) {
const logicalId = this.provider.naming.getLambdaLogicalId(name);
fnList.push({ name: name, logicalId: logicalId });
}
}
const chains = this.getChains();
// Generate dependsOn chain(s)
for (let i = chains; i < fnList.length; i++) {
const parent = i - chains;
fnList[i].dependsOn = fnList[parent].logicalId;
this.serverless.cli.log(fnList[i].name + ' dependsOn ' + fnList[parent].name);
}
// Update CloudFormation template
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
for (const fn of fnList) {
if (resources.hasOwnProperty(fn.logicalId)) {
const resource = resources[fn.logicalId];
if (resource.Type === 'AWS::Lambda::Function') {
if (fn.dependsOn) {
resource.DependsOn = (resource.DependsOn === undefined)
? fn.dependsOn
: [fn.dependsOn].concat(resource.DependsOn);
}
}
}
}
}
}
module.exports = DependsOn;