-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
packageService.js
299 lines (267 loc) · 9.99 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
const path = require('path');
const fsp = require('fs').promises;
const globby = require('globby');
const _ = require('lodash');
const micromatch = require('micromatch');
const ServerlessError = require('../../../serverless-error');
const parseS3URI = require('../../aws/utils/parse-s3-uri');
module.exports = {
defaultExcludes: [
'.git/**',
'.gitignore',
'.DS_Store',
'npm-debug.log',
'yarn-*.log',
'.serverless/**',
'.serverless_plugins/**',
],
getIncludes(include) {
const packageIncludes = [
...(this.serverless.service.package.include || []),
...(this.serverless.service.package.patterns || []),
];
return _.union(packageIncludes, include);
},
getRuntime(runtime) {
const defaultRuntime = 'nodejs12.x';
return runtime || this.serverless.service.provider.runtime || defaultRuntime;
},
getExcludes(exclude, excludeLayers) {
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 = this.serverless.configurationFilename
? [this.serverless.configurationFilename]
: [];
const configurationInput = this.serverless.configurationInput;
const envFilesExclude = configurationInput && configurationInput.useDotenv ? ['.env*'] : [];
return _.union(
this.defaultExcludes,
serverlessConfigFileExclude,
localPathExcludes,
packageExcludes,
layerExcludes,
envFilesExclude,
exclude
);
},
async packageService() {
this.serverless.cli.log('Packaging service...');
let shouldPackageService = false;
const allFunctions = this.serverless.service.getAllFunctions();
let packagePromises = allFunctions.map(async (functionName) => {
const functionObject = this.serverless.service.getFunction(functionName);
if (functionObject.image) return;
functionObject.package = functionObject.package || {};
if (functionObject.package.disable) {
this.serverless.cli.log(`Packaging disabled for function: "${functionName}"`);
return;
}
if (functionObject.package.artifact) {
if (parseS3URI(functionObject.package.artifact)) return;
try {
await fsp.access(
path.resolve(this.serverless.serviceDir, functionObject.package.artifact)
);
return;
} catch (error) {
throw new ServerlessError(
'Cannot access package artifact at ' +
`"${functionObject.package.artifact}" (for "${functionName}"): ${error.message}`,
'INVALID_PACKAGE_ARTIFACT_PATH'
);
}
}
if (functionObject.package.individually || this.serverless.service.package.individually) {
await this.packageFunction(functionName);
return;
}
shouldPackageService = true;
});
const allLayers = this.serverless.service.getAllLayers();
packagePromises = packagePromises.concat(
allLayers.map(async (layerName) => {
const layerObject = this.serverless.service.getLayer(layerName);
layerObject.package = layerObject.package || {};
if (layerObject.package.artifact) return;
await this.packageLayer(layerName);
})
);
await Promise.all(packagePromises);
if (shouldPackageService) {
if (this.serverless.service.package.artifact) {
if (parseS3URI(this.serverless.service.package.artifact)) return;
try {
await fsp.access(
path.resolve(this.serverless.serviceDir, this.serverless.service.package.artifact)
);
return;
} catch (error) {
throw new ServerlessError(
'Cannot access package artifact at ' +
`"${this.serverless.service.package.artifact}": ${error.message}`,
'INVALID_PACKAGE_ARTIFACT_PATH'
);
}
}
await this.packageAll();
}
},
async packageAll() {
const zipFileName = `${this.serverless.service.service}.zip`;
return this.resolveFilePathsAll().then((filePaths) =>
this.zipFiles(filePaths, zipFileName).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;
})
);
},
async packageFunction(functionName) {
const functionObject = this.serverless.service.getFunction(functionName);
if (functionObject.image) return null;
const funcPackageConfig = functionObject.package || {};
// use the artifact in function config if provided
if (funcPackageConfig.artifact) {
const filePath = path.resolve(this.serverless.serviceDir, funcPackageConfig.artifact);
functionObject.package.artifact = filePath;
return 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.resolve(
this.serverless.serviceDir,
this.serverless.service.package.artifact
);
funcPackageConfig.artifact = filePath;
return filePath;
}
const zipFileName = `${functionName}.zip`;
const filePaths = await this.resolveFilePathsFunction(functionName);
const artifactPath = await this.zipFiles(filePaths, zipFileName);
funcPackageConfig.artifact = artifactPath;
return artifactPath;
},
async 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;
}
)
);
},
async resolveFilePathsAll() {
return this.resolveFilePathsFromPatterns(
await this.excludeDevDependencies({
exclude: this.getExcludes([], true),
include: this.getIncludes(),
})
);
},
async resolveFilePathsFunction(functionName) {
const functionObject = this.serverless.service.getFunction(functionName);
const funcPackageConfig = functionObject.package || {};
return this.resolveFilePathsFromPatterns(
await this.excludeDevDependencies({
exclude: this.getExcludes(funcPackageConfig.exclude, true),
include: this.getIncludes([
...(funcPackageConfig.include || []),
...(funcPackageConfig.patterns || []),
]),
})
);
},
async resolveFilePathsLayer(layerName) {
const layerObject = this.serverless.service.getLayer(layerName);
const layerPackageConfig = layerObject.package || {};
return this.resolveFilePathsFromPatterns(
await this.excludeDevDependencies({
exclude: this.getExcludes(layerPackageConfig.exclude, false),
include: this.getIncludes([
...(layerPackageConfig.include || []),
...(layerPackageConfig.patterns || []),
]),
}),
layerObject.path
);
},
async resolveFilePathsFromPatterns(params, prefix) {
const patterns = [];
const devDependencyExcludeSet = params.devDependencyExcludeSet || new Set();
params.exclude.forEach((pattern) => {
// Ensure to apply dev dependency exclusion as last
if (devDependencyExcludeSet.has(pattern)) return;
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);
});
for (const pattern of devDependencyExcludeSet) {
if (pattern.charAt(0) !== '!') {
patterns.push(`!${pattern}`);
} else {
patterns.push(pattern.substring(1));
}
}
// 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.serviceDir, 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 = Object.entries(filePathStates)
.filter((r) => r[1] === true)
.map((r) => r[0]);
if (filePaths.length !== 0) return filePaths;
throw new ServerlessError('No file matches include / exclude patterns', 'NO_MATCHED_FILES');
});
},
};