-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
packageService.js
157 lines (136 loc) · 4.92 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
'use strict';
const BbPromise = require('bluebird');
const path = require('path');
const globby = require('globby');
const _ = require('lodash');
module.exports = {
defaultExcludes: [
'.git/**',
'.gitignore',
'.DS_Store',
'npm-debug.log',
'serverless.yml',
'serverless.yaml',
'serverless.json',
'serverless.js',
'.serverless/**',
'.serverless_plugins/**',
],
getIncludes(include) {
const packageIncludes = this.serverless.service.package.include || [];
return _.union(packageIncludes, include);
},
getExcludes(exclude) {
const packageExcludes = this.serverless.service.package.exclude || [];
// add defaults for exclude
return _.union(this.defaultExcludes, packageExcludes, exclude);
},
packageService() {
this.serverless.cli.log('Packaging service...');
let shouldPackageService = false;
const allFunctions = this.serverless.service.getAllFunctions();
const packagePromises = _.map(allFunctions, 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();
});
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`;
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;
})
);
},
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
if (this.serverless.service.package.artifact) {
const filePath = path.join(this.serverless.config.servicePath,
this.serverless.service.package.artifact);
funcPackageConfig.artifact = filePath;
return BbPromise.resolve(filePath);
}
const zipFileName = `${functionName}.zip`;
return this.resolveFilePathsFunction(functionName).then(filePaths =>
this.zipFiles(filePaths, zipFileName).then(artifactPath => {
functionObject.package = {
artifact: artifactPath,
};
return artifactPath;
})
);
},
resolveFilePathsAll() {
const params = { exclude: this.getExcludes(), include: this.getIncludes() };
return this.excludeDevDependencies(params).then(() =>
this.resolveFilePathsFromPatterns(params));
},
resolveFilePathsFunction(functionName) {
const functionObject = this.serverless.service.getFunction(functionName);
const funcPackageConfig = functionObject.package || {};
const params = {
exclude: this.getExcludes(funcPackageConfig.exclude),
include: this.getIncludes(funcPackageConfig.include),
};
return this.excludeDevDependencies(params).then(() =>
this.resolveFilePathsFromPatterns(params));
},
resolveFilePathsFromPatterns(params) {
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);
});
return globby(patterns, {
cwd: this.serverless.config.servicePath,
dot: true,
silent: true,
follow: true,
nodir: true,
}).then(filePaths => {
if (filePaths.length !== 0) return filePaths;
throw new this.serverless.classes.Error('No file matches include / exclude patterns');
});
},
};