/
packageService.js
277 lines (249 loc) · 9.66 KB
/
packageService.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
'use strict';
const BbPromise = require('bluebird');
const path = require('path');
const globby = require('globby');
const _ = require('lodash');
const micromatch = require('micromatch');
const serverlessConfigFileUtils = require('../../../../lib/utils/getServerlessConfigFile');
module.exports = {
defaultExcludes: [
'.git/**',
'.gitignore',
'.DS_Store',
'npm-debug.log',
'yarn-*.log',
'.serverless/**',
'.serverless_plugins/**',
],
getIncludes(include) {
const packageIncludes = this.serverless.service.package.include || [];
return _.union(packageIncludes, include);
},
getRuntime(runtime) {
const defaultRuntime = 'nodejs12.x';
return runtime || this.serverless.service.provider.runtime || defaultRuntime;
},
getExcludes(exclude, excludeLayers) {
return serverlessConfigFileUtils
.getServerlessConfigFilePath(this.serverless)
.then(configFilePath => {
const packageExcludes = this.serverless.service.package.exclude || [];
// add local service plugins Path
const pluginsLocalPath = this.serverless.pluginManager.parsePluginsObject(
this.serverless.service.plugins
).localPath;
const localPathExcludes = pluginsLocalPath ? [pluginsLocalPath] : [];
// add layer paths
const layerExcludes = excludeLayers
? this.serverless.service
.getAllLayers()
.map(layer => `${this.serverless.service.getLayer(layer).path}/**`)
: [];
// add defaults for exclude
const serverlessConfigFileExclude = configFilePath ? [path.basename(configFilePath)] : [];
return _.union(
this.defaultExcludes,
serverlessConfigFileExclude,
localPathExcludes,
packageExcludes,
layerExcludes,
exclude
);
});
},
packageService() {
this.serverless.cli.log('Packaging service...');
let shouldPackageService = false;
const allFunctions = this.serverless.service.getAllFunctions();
let packagePromises = allFunctions.map(functionName => {
const functionObject = this.serverless.service.getFunction(functionName);
functionObject.package = functionObject.package || {};
if (functionObject.package.disable) {
this.serverless.cli.log(`Packaging disabled for function: "${functionName}"`);
return BbPromise.resolve();
}
if (functionObject.package.artifact) {
return BbPromise.resolve();
}
if (functionObject.package.individually || this.serverless.service.package.individually) {
return this.packageFunction(functionName);
}
shouldPackageService = true;
return BbPromise.resolve();
});
const allLayers = this.serverless.service.getAllLayers();
packagePromises = packagePromises.concat(
allLayers.map(layerName => {
const layerObject = this.serverless.service.getLayer(layerName);
layerObject.package = layerObject.package || {};
if (layerObject.package.artifact) {
return BbPromise.resolve();
}
return this.packageLayer(layerName);
})
);
return BbPromise.all(packagePromises).then(() => {
if (shouldPackageService && !this.serverless.service.package.artifact) {
return this.packageAll();
}
return BbPromise.resolve();
});
},
packageAll() {
const zipFileName = `${this.serverless.service.service}.zip`;
/*
* crosscompiled GoLang binaries on windows don't have their execute bit set correctly.
* This is nearly impossible to actually set on a windows machine, so find all the Go handler
* files and pass them into zipFiles as files to add with the execute bit in the zip file
*/
const filesToChmodPlusX =
process.platform !== 'win32'
? []
: Object.values(this.serverless.service.functions)
.map(f =>
Object.assign(
{
runtime: this.getRuntime(),
},
f
)
)
.filter(f => f.runtime && f.runtime.startsWith('go'))
.map(f => path.normalize(f.handler));
return this.resolveFilePathsAll().then(filePaths =>
this.zipFiles(filePaths, zipFileName, undefined, filesToChmodPlusX).then(filePath => {
// only set the default artifact for backward-compatibility
// when no explicit artifact is defined
if (!this.serverless.service.package.artifact) {
this.serverless.service.package.artifact = filePath;
this.serverless.service.artifact = filePath;
}
return filePath;
})
);
},
packageFunction(functionName) {
const functionObject = this.serverless.service.getFunction(functionName);
const funcPackageConfig = functionObject.package || {};
// use the artifact in function config if provided
if (funcPackageConfig.artifact) {
const filePath = path.join(this.serverless.config.servicePath, funcPackageConfig.artifact);
functionObject.package.artifact = filePath;
return BbPromise.resolve(filePath);
}
// use the artifact in service config if provided
// and if the function is not set to be packaged individually
if (this.serverless.service.package.artifact && !funcPackageConfig.individually) {
const filePath = path.join(
this.serverless.config.servicePath,
this.serverless.service.package.artifact
);
funcPackageConfig.artifact = filePath;
return BbPromise.resolve(filePath);
}
const zipFileName = `${functionName}.zip`;
const filesToChmodPlusX = [];
if (process.platform === 'win32') {
const runtime = this.getRuntime(functionName.runtime);
if (runtime.startsWith('go')) {
filesToChmodPlusX.push(functionObject.handler);
}
}
return this.resolveFilePathsFunction(functionName).then(filePaths =>
this.zipFiles(filePaths, zipFileName, undefined, filesToChmodPlusX).then(artifactPath => {
funcPackageConfig.artifact = artifactPath;
return artifactPath;
})
);
},
packageLayer(layerName) {
const layerObject = this.serverless.service.getLayer(layerName);
const zipFileName = `${layerName}.zip`;
return this.resolveFilePathsLayer(layerName)
.then(filePaths => filePaths.map(f => path.resolve(path.join(layerObject.path, f))))
.then(filePaths =>
this.zipFiles(filePaths, zipFileName, path.resolve(layerObject.path)).then(artifactPath => {
layerObject.package = {
artifact: artifactPath,
};
return artifactPath;
})
);
},
resolveFilePathsAll() {
return this.getExcludes([], true)
.then(exclude => {
const params = { exclude, include: this.getIncludes() };
return params;
})
.then(params => this.excludeDevDependencies(params))
.then(params => this.resolveFilePathsFromPatterns(params));
},
resolveFilePathsFunction(functionName) {
const functionObject = this.serverless.service.getFunction(functionName);
const funcPackageConfig = functionObject.package || {};
return this.getExcludes(funcPackageConfig.exclude, true)
.then(exclude => {
const params = { exclude, include: this.getIncludes(funcPackageConfig.include) };
return params;
})
.then(params => this.excludeDevDependencies(params))
.then(params => this.resolveFilePathsFromPatterns(params));
},
resolveFilePathsLayer(layerName) {
const layerObject = this.serverless.service.getLayer(layerName);
const layerPackageConfig = layerObject.package || {};
return this.getExcludes(layerPackageConfig.exclude, true)
.then(exclude => {
const params = { exclude, include: this.getIncludes(layerPackageConfig.include) };
return params;
})
.then(params => this.excludeDevDependencies(params))
.then(params => this.resolveFilePathsFromPatterns(params, layerObject.path));
},
resolveFilePathsFromPatterns(params, prefix) {
const patterns = [];
params.exclude.forEach(pattern => {
if (pattern.charAt(0) !== '!') {
patterns.push(`!${pattern}`);
} else {
patterns.push(pattern.substring(1));
}
});
// push the include globs to the end of the array
// (files and folders will be re-added again even if they were excluded beforehand)
params.include.forEach(pattern => {
patterns.push(pattern);
});
// NOTE: please keep this order of concatenating the include params
// rather than doing it the other way round!
// see https://github.com/serverless/serverless/pull/5825 for more information
return globby(['**'].concat(params.include), {
cwd: path.join(this.serverless.config.servicePath, prefix || ''),
dot: true,
silent: true,
follow: true,
nodir: true,
expandDirectories: false,
}).then(allFilePaths => {
const filePathStates = allFilePaths.reduce((p, c) => Object.assign(p, { [c]: true }), {});
patterns
// micromatch only does / style path delimiters, so convert them if on windows
.map(p => {
return process.platform === 'win32' ? p.replace(/\\/g, '/') : p;
})
.forEach(p => {
const exclude = p.startsWith('!');
const pattern = exclude ? p.slice(1) : p;
micromatch(allFilePaths, [pattern], { dot: true }).forEach(key => {
filePathStates[key] = !exclude;
});
});
const filePaths = _.toPairs(filePathStates)
.filter(r => r[1] === true)
.map(r => r[0]);
if (filePaths.length !== 0) return filePaths;
throw new this.serverless.classes.Error('No file matches include / exclude patterns');
});
},
};