-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathinclude-dependencies.js
153 lines (117 loc) · 4.57 KB
/
include-dependencies.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
'use strict';
const path = require('path');
const semver = require('semver');
const micromatch = require('micromatch');
const glob = require('glob');
const getDependencyList = require('./get-dependency-list');
function union(a = [], b = []) {
const existing = [].concat(a);
const set = new Set(existing);
[].concat(b).forEach(p => {
if (set.has(p)) {
return;
}
set.add(p);
existing.push(p);
});
return existing;
}
module.exports = class IncludeDependencies {
constructor(serverless, options) {
if (!semver.satisfies(serverless.version, '>= 2.32')) {
throw new Error('serverless-plugin-include-dependencies requires serverless 2.32 or higher!');
}
this.serverless = serverless;
this.options = options;
this.cache = new Set();
this.checkedFiles = new Set();
const service = this.serverless.service;
this.individually = service.package && service.package.individually;
this.hooks = {
'before:deploy:function:packageFunction': this.functionDeploy.bind(this),
'before:package:createDeploymentArtifacts': this.createDeploymentArtifacts.bind(this)
};
}
functionDeploy() {
return this.processFunction(this.options.function);
}
createDeploymentArtifacts() {
const { service = {} } = this.serverless;
const { functions = {} } = service;
for (const functionName in functions) {
this.processFunction(functionName);
}
const files = [...new Set(this.getPatterns().filter(pattern => !pattern.startsWith('!') && !pattern.includes('node_modules'))
.map(modulePath => glob.sync(modulePath, {
nodir: true,
ignore: path.join(modulePath, 'node_modules', '**'),
absolute: true
})
).flat().map(file => file.replaceAll('\\', '/')))];
files.forEach(fileName => {
if (!this.checkedFiles.has(fileName)) {
const dependencies = this.getDependencies(fileName, service.package.patterns);
service.package.patterns = union(service.package.patterns, dependencies);
}
});
this.checkedFiles.clear();
}
processFunction(functionName) {
const { service = {} } = this.serverless;
service.package = service.package || {};
service.package.patterns = union(['!node_modules/**'], service.package.patterns);
const functionObject = service.functions[functionName];
const runtime = this.getFunctionRuntime(functionObject);
if (/(provided|nodejs)+/.test(runtime)) {
this.processNodeFunction(functionObject);
}
}
getPatterns() {
const service = this.serverless.service;
return (service.package && service.package.patterns) || [];
}
getPluginOptions() {
const service = this.serverless.service;
return (service.custom && service.custom.includeDependencies) || {};
}
processNodeFunction(functionObject) {
const { service } = this.serverless;
functionObject.package = functionObject.package || {};
const fileName = this.getHandlerFilename(functionObject.handler);
const dependencies = this.getDependencies(fileName, service.package.patterns);
const target = this.individually ? functionObject : service;
target.package.patterns = union(target.package.patterns, dependencies);
}
getFunctionRuntime(functionObject) {
const { service } = this.serverless;
const functionRuntime = functionObject.runtime;
const providerRuntime = service.provider && service.provider.runtime;
return functionRuntime || providerRuntime;
}
getHandlerFilename(handler) {
const lastDotIndex = handler.lastIndexOf('.');
const handlerPath = lastDotIndex !== -1 ? handler.slice(0, lastDotIndex) : 'index';
return require.resolve((path.join(this.serverless.config.servicePath, handlerPath)));
}
getDependencies(fileName, patterns) {
const servicePath = this.serverless.config.servicePath;
const dependencies = this.getDependencyList(fileName) || [];
const relativeDependencies = dependencies.map(p => path.relative(servicePath, p));
const exclusions = patterns.filter(p => {
return !(p.indexOf('!node_modules') !== 0 || p === '!node_modules' || p === '!node_modules/**');
});
if (exclusions.length > 0) {
return micromatch(relativeDependencies, exclusions);
}
return relativeDependencies;
}
getDependencyList(fileName) {
if (!this.individually) {
const options = this.getPluginOptions();
if (options && options.enableCaching) {
return getDependencyList(fileName, this.serverless, this.checkedFiles, this.cache);
}
}
return getDependencyList(fileName, this.serverless, this.checkedFiles);
}
};